coreproc/nova-notification-feed

一个为 Laravel Nova 应用添加通知流的功能包。


README

Latest Version on Packagist Quality Score Total Downloads

一个为 Laravel Nova 应用添加通知流的功能包,并使用 Laravel Echo 和 WebSocket 接收和广播通知。

安装

您可以通过 composer 将此包安装到使用 Nova 的 Laravel 应用中

composer require coreproc/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 应用中的通知流广播通知,您可以通过 databasebroadcast 发送通知的类。以下是一个示例通知类

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() 方法中传递三个变量:levelmessageurl,以及可选的 target(默认:'_blank')。

此外,您可以在 toBroadcast() 方法中使用 NovaBroadcastMessage 类来确保广播的格式可以被前端读取。

路线图

  • 区分新通知的背景颜色
  • 检查 URL 是否是路由的 JSON 表示形式 { name: 'index', params: {} }
  • 更好的设计?

更新日志

请参阅变更日志以获取关于最近更改的更多信息。

贡献

请参阅贡献指南获取详细信息。

安全性

如果您发现任何与安全相关的问题,请发送电子邮件至chris.bautista@coreproc.ph而不是使用问题跟踪器。

致谢

关于CoreProc

CoreProc是一家软件开发公司,为初创企业、数字/广告机构和企业提供软件开发服务。

在我们的网站上了解更多关于我们的信息。

许可证

MIT许可证(MIT)。请参阅许可证文件获取更多信息。