thundev/matrix-notification-channel

Laravel 的 Matrix 通知通道。

v2.1.0 2023-01-26 07:50 UTC

This package is auto-updated.

Last update: 2024-09-29 06:09:00 UTC


README

此包为 Laravel 应用程序添加了对 Matrix 通知的支持。

安装

您可以通过 composer 安装此包

composer require thundev/matrix-notification-channel

发布配置

php artisan vendor:publish --tag="matrix-config"

用法

路由 Matrix 通知

要将 Matrix 通知路由到正确的房间,请在您的可通知实体上定义一个 routeNotificationForMatrix() 方法,该方法应返回应发送通知的 Matrix 房间 ID。请确保首先邀请您的机器人进入房间。机器人会在发送第一条消息时自动接受邀请。

<?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';
    }
}

创建通知

如果通知支持作为 Matrix 消息发送,则您的通知应实现 Thundev\MatrixNotificationChannel\Contracts\MatrixNotificationContract 接口,该接口定义了一个 toMatrix() 方法。此方法将接收一个 $notifiable 实体,并应返回一个 Thundev\MatrixNotificationChannel\Message\MatrixMessage 实例

<?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!');
    }
}

分发通知

要为您的可通知实例分发消息,只需调用 notify() 方法。

(new User())->notify(new InvoiceCreatedNotification());