ratno / nova-notification-feed
一个为Laravel Nova应用添加通知流的Laravel包。
Requires
- php: >=7.1.0
- ratno/nova-echo: ^1.0
README
一个在Nova应用中添加通知流并使用Laravel Echo和websockets接收和广播通知的Laravel Nova包。
安装
您可以通过composer将此包安装到使用Nova的Laravel应用中
composer require coreproc/nova-notification-feed
此包利用Laravel的数据库通知功能和Nova Echo来接收和广播通知。
通过使用Nova Echo,我们已经在JS中配置了一个现成的Laravel Echo实例。
以下是使用websockets广播/接收的建议选项
请确保您已经设置好了这些选项之一,并准备好您的Laravel项目以用于广播通知。
您可以在官方文档中找到有关在Laravel中使用广播的说明。
遵循文档,确保运行以下命令以获取notifications
表(如果您尚未这样做)
php artisan notifications:table
php artisan migrate
在广播任何事件之前,您首先需要注册App\Providers\BroadcastServiceProvider
。在新的Laravel应用中,您只需要在config/app.php
配置文件的providers
数组中取消注释此提供器。此提供器将允许您注册广播授权路由和回调。
确保您在.env
文件中配置了正确的环境变量
BROADCAST_DRIVER=redis
您还需要确保在routes/channels.php
中添加了授权广播路由
Broadcast::channel('App.User.{id}', function ($user, $id) { return (int)$user->id === (int)$id; });
接收通知将取决于您的User
模型是否有Notifiable
特性。您可以将receivesBroadcastNotificationsOn
添加到使用不同的通道名称而不是用户模型的命名空间。
class User extends Authenticatable { use Notifiable; ... /** * The channels the user receives notification broadcasts on. * * @return string */ public function receivesBroadcastNotificationsOn() { return 'users.' . $this->id; } }
最后,一旦您确保了这一点,您还需要覆盖Nova的layout.blade.php
。在resources/views/vendor/nova/layout.blade.php
中创建一个布局文件,并将内容从vendor/laravel/nova/resources/views/layout.blade.php
复制过来。
将这些两行添加到布局模板中
// file: resources/views/vendor/nova/layout.blade.php
<!DOCTYPE html>
<html lang="en" class="h-full font-sans antialiased">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=1280">
<meta name="csrf-token" content="{{ csrf_token() }}">
@include('nova-echo::meta') <!-- INCLUDE THIS LINE HERE -->
<title>
...
<dropdown class="ml-auto h-9 flex items-center dropdown-right">
@include('nova::partials.user')
</dropdown>
@include('nova_notification_feed::notification_feed') <!-- AND THIS LINE HERE -->
...
现在您应该能够在Nova UI的右上角看到通知铃。
用法
要向Nova应用中的通知流广播通知,您可以创建一个通过database
和broadcast
发送通知的通知类。以下是一个示例通知类
use Coreproc\NovaNotificationFeed\Notifications\NovaBroadcastMessage; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Messages\BroadcastMessage; use Illuminate\Notifications\Notification; class TestNotification extends Notification { use Queueable; protected $level = 'info'; protected $message = ''; /** * Create a new notification instance. * * @param $level * @param $message */ public function __construct($level, $message = 'Test message') { $this->level = $level; $this->message = $message; } /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable) { return [ 'database', 'broadcast', ]; } /** * Get the array representation of the notification. * * @param mixed $notifiable * @return array */ public function toArray($notifiable) { return [ 'level' => $this->level, 'message' => $this->message, 'url' => 'https://coreproc.com', 'target' => '_self' ]; } /** * Get the broadcastable representation of the notification. * * @param mixed $notifiable * @return BroadcastMessage */ public function toBroadcast($notifiable) { return new NovaBroadcastMessage($this->toArray($notifiable)); } }
Nova Notification Feed依赖于通知类中toArray()
方法传递的三个变量:level
、message
和url
,以及可选的target
(默认:'_blank'
)。
此外,您还可以在toBroadcast()
方法中使用NovaBroadcastMessage
类来确保广播的格式可以被前端读取。
路线图
- 区分新通知的背景色
- 检查URL是否是路由的JSON表示
{ name: 'index', params: {} }
- 更好的设计?
更新日志
有关最近更改的更多信息,请参阅更新日志。
贡献
有关详细信息,请参阅贡献。
安全
如果您发现任何与安全相关的问题,请通过电子邮件chris.bautista@coreproc.ph联系,而不是使用问题跟踪器。
鸣谢
关于CoreProc
CoreProc是一家软件开发公司,为初创公司、数字/广告机构和企业提供软件开发服务。
了解我们更多信息,请访问我们的网站。
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件。