tomatophp/filament-alerts

使用通知模板和多个通知渠道向用户发送通知,支持使用宏的 Filament 原生通知服务,以及与 FCM 服务工作者通知的完全集成

This package is auto-updated.

Last update: 2024-09-17 17:07:26 UTC


README

Screenshot

Filament Alerts 发送者

Latest Stable Version License Downloads

使用通知模板和多个通知渠道向用户发送通知,支持使用宏的 Filament 原生通知服务,以及与 FCM 服务工作者通知的完全集成

功能

  • 向用户发送通知
  • 使用 Filament 原生通知
  • 使用通知模板
  • 完全 FCM 服务工作者集成
  • 使用多个通知渠道
  • API 获取通知
  • 隐藏通知资源
  • 使用 Slack 驱动器
  • 使用 Discord 驱动器
  • 使用 Reverb 驱动器
  • 使用 SMS Misr 驱动器
  • 使用 Email 驱动器
  • 使用 数据库 驱动器
  • 使用 MessageBird 驱动器

屏幕截图

Screenshot Screenshot Screenshot Screenshot

安装

在使用此包之前,请确保您已安装

composer require tomatophp/filament-alerts

现在您需要发布和迁移设置表

php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations"

安装您的包后,请运行此命令

php artisan filament-alerts:install

如果您不是作为插件使用此包,请在 /app/Providers/Filament/AdminPanelProvider.php 上注册插件

->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
)

用法

要设置任何模型以获取通知,您

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;
use TomatoPHP\FilamentAlerts\Traits\InteractsWithNotifications;

class User extends Authenticatable
{
    use HasApiTokens;
    use HasFactory;
    use HasProfilePhoto;
    use Notifiable;
    use TwoFactorAuthenticatable;
    use HasRoles;
    use InteractsWithNotifications;
    ...

并且您必须设置 FCM 的设置以获取实时通知

队列

通知在队列上运行,因此您必须运行队列工作者以发送通知

php artisan queue:work

使用 Filament 原生通知

您可以使用 Filament 原生通知,我们为您添加了一些 macro

use Filament\Notifications\Notification;

Notification::make('send')
    ->title('Test Notifications')
    ->body('This is a test notification')
    ->icon('heroicon-o-bell')
    ->color('success')
    ->actions([
        \Filament\Notifications\Actions\Action::make('view')
            ->label('View')
            ->url('https://google.com')
            ->markAsRead()
    ])
    ->sendToDiscord(auth()->user())
    ->sendToEmail(auth()->user())
    ->broadcast(auth()->user())
    ->sendToDatabase(auth()->user())
    ->sendToSlack(auth()->user())
    ->sendToFCM(auth()->user())

通知服务

要创建新模板,您可以使用模板 CRUD,并确保模板键是唯一的,因为您将在每个单独的通知中使用它。

发送通知

要发送通知,您必须使用我们的助手 SendNotification::class,如下所示

SendNotification::make($template->providers)
    ->template($template->key)
    ->findTitle($matchesTitle)
    ->replaceTitle($titleFill)
    ->findBody($matchesBody)
    ->replaceBody($titleBody)
    ->model(User::class)
    ->id(User::first()->id)
    ->privacy('private')
    ->fire();

其中 $template 是通过键选择的模板,$matchesTitle 和 $matchesBody 是替换模板的匹配数组,$titleFill 和 $titleBody 是替换匹配的值数组

通知渠道

您可以使用多个通知渠道,如

  • 电子邮件
  • 短信
  • FCM
  • Reverb
  • 数据库
  • Slack
  • Discord

它可以直接使用用户方法工作

$user->notifySMSMisr(string $message);
$user->notifyEmail(string $message, ?string $subject = null, ?string $url = null);
$user->notifyFCMSDK(string $message, string $type='web', ?string $title=null, ?string $url=null, ?string $image=null, ?string $icon=null, ?array $data=[]);
$user->notifyDB(string $message, ?string $title=null, ?string $url =null);
$user->notifySlack(string $title,string $message=null,?string $url=null, ?string $image=null, ?string $webhook=null);
$user->notifyDiscord(string $title,string $message=null,?string $url=null, ?string $image=null, ?string $webhook=null);

使用 FCM 通知提供者

要使 FCM 通知工作,您需要安装 Filament 设置中心 并允许在插件上使用设置中心

->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
    ->useSettingsHub()
    ->useFCM()
)

然后您需要通过使用此命令安装 filament-fcm

composer require tomatophp/filament-fcm

并添加服务提供者插件

->plugin(\TomatoPHP\FilamentFcm\FilamentFcmPlugin::make())

现在您需要更新配置

# Firebase Project
FIREBASE_API_KEY=
FIREBASE_AUTH_DOMAIN=
FIREBASE_DATABASE_URL=
FIREBASE_PROJECT_ID=
FIREBASE_STORAGE_BUCKET=
FIREBASE_MESSAGING_SENDER_ID=
FIREBASE_APP_ID=
FIREBASE_MEASUREMENT_ID=

# Firebase Cloud Messaging
FIREBASE_VAPID=

# Firebase Alert Sound
FCM_ALERT_SOUND=

然后运行此命令

php artisan filament-fcm:install

它将为您生成 FCM 工作者,以便在后台使通知工作。

隐藏通知资源

要隐藏侧边栏中的通知资源,您可以使用插件方法 hideNotificationsResources,如下所示

->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
    ->hideNotificationsResources()
)

使用 Slack 驱动器

要使用 Slack 驱动器,您必须在设置中心设置 Slack webhook 并使用插件方法 useSlack,如下所示

->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
    ->useSlack()
)

现在在您的 .env 文件中添加一个 SLACK_WEBHOOK 键,并使用 webhook URL

使用 Discord 驱动器

要使用 Discord 驱动器,您必须在设置中心设置 Discord webhook 并使用插件方法 useDiscord,如下所示

->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
    ->useDiscord()
)

现在在您的 .env 文件中添加一个 DISCORD_WEBHOOK 键,并使用 webhook URL

API

我们支持一些API来获取通知并进行一些操作,您可以在api/notifications路由下找到它们

您可以通过使用插件方法apiModel来更改用户模型,例如

->plugin(\TomatoPHP\FilamentAlerts\FilamentAlertsPlugin::make()
    ->apiModel(User::class)
)

发布资产

您可以使用此命令来发布配置文件

php artisan vendor:publish --tag="filament-alerts-config"

您可以使用此命令来发布视图文件

php artisan vendor:publish --tag="filament-alerts-views"

您可以使用此命令来发布语言文件

php artisan vendor:publish --tag="filament-alerts-lang"

您可以使用此命令来发布迁移文件

php artisan vendor:publish --tag="filament-alerts-migrations"

其他Filament包

查看我们的Awesome TomatoPHP