thundev / matrix-notification-channel
Matrix notification channel for Laravel.
Installs: 4 791
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- guzzlehttp/guzzle: ^7.4
Requires (Dev)
- orchestra/testbench: ^6.22
README
This package adds support for Matrix notifications to Laravel applications.
Installation
You can install the package via composer:
composer require thundev/matrix-notification-channel
Publish the configurations:
php artisan vendor:publish --tag="matrix-config"
Usage
Routing Matrix notification
To route Matrix notifications to the proper room, define a routeNotificationForMatrix()
method on your notifiable entity which should return the Matrix room ID to which the notification should be delivered. Make sure to invite your bot into the room first. The bot will automatically accept the invitation upon sending the very first message.
<?php namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notification; class User extends Authenticatable { use Notifiable; /** * @param Notification $notification */ public function routeNotificationForMatrix($notification): string { return 'your_room_id'; } }
Creating Notifications
If a notification supports being sent as a Matrix message, your notification should implement Thundev\MatrixNotificationChannel\Contracts\MatrixNotificationContract
which defines a toMatrix()
method. This method will receive a $notifiable
entity and should return an Thundev\MatrixNotificationChannel\Message\MatrixMessage
instance:
<?php namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; use Thundev\MatrixNotificationChannel\Contracts\MatrixNotificationContract; use Thundev\MatrixNotificationChannel\Message\MatrixMessage; class InvoiceCreatedNotification extends Notification implements MatrixNotificationContract { use Queueable; public function via(mixed $notifiable): array { return ['matrix']; } public function toMatrix(mixed $notifiable): MatrixMessage { return (new MatrixMessage())->message('My awesome message!'); } }
Dispatching notification
To dispatch a message for your notifiable instance, simply call the notify()
method.
(new User())->notify(new InvoiceCreatedNotification());