Skip to content
全部文档

广播通知

简介

默认情况下,Filament 通过 Laravel session 发送闪存通知。但你可能希望改为将通知实时「广播」给用户。例如,可在队列任务处理完成后发送临时成功通知。

我们与 Laravel Echo 有原生集成。请确保已安装 Echo,以及如 Pusher 等服务端 WebSocket 集成

发送广播通知

发送广播通知有多种方式,可按最适合你的选择。

可使用我们的流式 API:

php
use Filament\Notifications\Notification;

$recipient = auth()->user();

Notification::make()
    ->title('Saved successfully')
    ->broadcast($recipient);

或使用 notify() 方法:

php
use Filament\Notifications\Notification;

$recipient = auth()->user();

$recipient->notify(
    Notification::make()
        ->title('Saved successfully')
        ->toBroadcast(),
)

也可使用传统的 Laravel 通知类,在 toBroadcast() 方法中返回通知:

php
use App\Models\User;
use Filament\Notifications\Notification;
use Illuminate\Notifications\Messages\BroadcastMessage;

public function toBroadcast(User $notifiable): BroadcastMessage
{
    return Notification::make()
        ->title('Saved successfully')
        ->getBroadcastMessage();
}

在面板中设置 WebSocket

面板构建器对实时广播与数据库通知有一定内置支持。但仍需安装并配置若干部分,才能把一切接通并运行起来。

  1. 若尚未阅读,请先了解 Laravel 文档中的 [broadcasting](https://laravel.com/docs/broadcasting)。
  2. 安装并配置 broadcasting,以使用如 Pusher 等[服务端 WebSocket 集成](https://laravel.com/docs/broadcasting#server-side-installation)。
  3. 若尚未发布,需发布 Filament 包配置:
bash
php artisan vendor:publish --tag=filament-config
  1. 编辑 `config/filament.php` 中的配置,取消注释 `broadcasting.echo` 部分,并确保设置与你的 broadcasting 安装正确对应。
  2. 确保 `.env` 中存在[相关的 `VITE_*` 条目](https://laravel.com/docs/broadcasting#client-pusher-channels)。
  3. 用 `php artisan route:clear` 与 `php artisan config:clear` 清除相关缓存,确保新配置生效。

面板现在应已连接到 broadcasting 服务。例如,登录 Pusher 调试控制台后,每次加载页面都应看到传入连接。