通知
广播通知
简介
默认情况下,Filament 会通过 Laravel 会话发送 Flash 通知。但是,你可能希望将通知实时“广播”给用户。这可用于在队列任务处理完成后发送临时成功通知。
我们与 Laravel Echo 进行了原生集成。请确保已安装 Echo 以及类似 Pusher 的 服务器端 WebSocket 集成。
发送广播通知
发送广播通知有多种方式,请根据实际情况选择。
可以使用 fluent API:
use Filament\Notifications\Notification;
$recipient = auth()->user();
Notification::make()
->title('Saved successfully')
->broadcast($recipient);
或者使用 notify()
方法:
use Filament\Notifications\Notification;
$recipient = auth()->user();
$recipient->notify(
Notification::make()
->title('Saved successfully')
->toBroadcast(),
)
或者,也可以使用传统的 Laravel 通知类,通过 toDatabase()
方法返回通知:
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
面板构建器内置了对实时广播和数据库通知的支持。但是,你需要安装和配置一些组件才能连接并使其正常工作。
- 如果你还没有安装,请阅读 Laravel 文档中关于广播的相关内容。
- 安装并配置广播,以便使用服务器端 WebSocket 集成,例如 Pusher。
- 如果你还没有安装,则需要发布 Filament 包配置:
php artisan vendor:publish --tag=filament-config
- 编辑
config/filament.php
中的配置,并取消broadcasting.echo
注释 - 确保这些设置已根据你的广播安装正确配置。 - 确保
.env
文件中存在相关的VITE_*
条目。 - 使用
php artisan route:clear
和php artisan config:clear
清除相关缓存,以确保新配置生效。
你的面板现在应该可以连接到广播服务了。例如,如果你登录 Pusher 调试控制台,每次加载页面时都应该看到一个传入的连接。
Edit on GitHubStill need help? Join our Discord community or open a GitHub discussion