ahmed-hussain / nova-notification-feed
一个Laravel Nova扩展包,可以在您的Nova应用程序中添加通知流。
Requires
- php: >=7.1.0
- coreproc/nova-echo: ^0.2.0
- pusher/pusher-php-server: ^4.1.1
This package is not auto-updated.
Last update: 2024-09-21 14:49:04 UTC
README
一个在您的Nova应用程序中添加通知流并使用Laravel Echo和WebSocket接收和广播通知的Laravel Nova扩展包。
安装
您可以通过composer将此包安装到使用Nova的Laravel应用程序中
composer require ahmed-hussain/nova-notification-feed
此包利用Laravel的数据库通知功能和Nova Echo来接收和广播通知。
通过使用Nova Echo,我们已经在JS中有一个预先配置好的Laravel Echo实例。
以下是使用WebSocket广播/接收的推荐选项
请确保您已经设置好其中任何一个选项,并准备您的Laravel项目使用它来广播通知。
您可以在官方文档中找到有关Laravel广播的说明。
按照文档操作,确保运行以下命令以获取notifications
表(如果您尚未这样做)
php artisan notifications:table
php artisan migrate
在广播任何事件之前,您首先需要注册App\Providers\BroadcastServiceProvider
。在新的Laravel应用程序中,您只需要在config/app.php
配置文件的providers
数组中取消注释此提供程序。此提供程序将允许您注册广播授权路由和回调。
请确保您已经在.env
文件中配置了正确的环境变量
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=xxxxxxx
PUSHER_APP_KEY=xxxxxxx
PUSHER_APP_SECRET=xxxxxx
PUSHER_APP_CLUSTER=xxx
您还需要确保您已经在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 AhmedHussain\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://ahmed-hussain.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通知流依赖于在通知类的toArray()
方法中传递三个变量:level
、message
和url
,以及可选的target
(默认:'_blank'
)。
此外,您可以在toBroadcast()
方法中使用NovaBroadcastMessage
类来确保广播的格式可以被前端读取。
路线图
- 区分新通知的背景颜色
- 检查URL是否是路由的JSON表示
{ name: 'index', params: {} }
- 更好的设计?
变更日志
请参阅变更日志获取关于最近更改的更多信息。
贡献
请参阅贡献指南获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件chris.bautista@ahmed-hussain.ph联系,而不是使用问题跟踪器。
致谢
关于CoreProc
CoreProc是一家软件开发公司,为初创公司、数字/广告机构和企业提供软件开发服务。
在我们的网站上了解更多关于我们的信息。
许可证
MIT许可证(MIT)。请参阅许可证文件获取更多信息。