一个随机的 Codica Studio 包。

1.0.0 2020-09-25 01:54 UTC

This package is auto-updated.

Last update: 2024-09-25 13:30:34 UTC


README

Nova 包,为您的 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 通知流依赖于通知类 toArray() 方法中的三个变量:levelmessageurl,以及可选的 target(默认:'_blank')。

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

路线图

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

许可证

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