yassir3wad/nova-realtime-notification

此包为 Laravel Nova 通知增加了实时广播功能。

v1.0.2 2024-07-22 07:15 UTC

This package is auto-updated.

Last update: 2024-09-22 07:43:20 UTC


README

此包为 Laravel Nova 通知增加了实时广播功能,无缝集成于 Laravel Nova。

功能

  • 使用 Laravel Echo 进行实时通知广播。
  • 无缝集成 Laravel Nova。
  • 可配置广播通道和事件。
  • 接收通知时可选播放声音。

安装

要求

  • Laravel Nova 4.x 或更高版本

步骤

  1. 通过 Composer 安装此包

    composer require yassir3wad/nova-realtime-notification
  2. 发布包配置

    php artisan vendor:publish --provider="Yassir3wad\NovaRealtimeNotification\ToolServiceProvider"
  3. config/broadcasting.php 中配置你的广播设置。确保你已配置必要的驱动(例如,Pusher、Redis 等)。

配置

包配置可在 config/nova-realtime-notification.php 中找到。你可以自定义以下设置

  • enabled:启用/禁用实时通知。
  • broadcast_driver:要使用的广播驱动(例如,pusherreverb)。
  • broadcast_channel:通知的私有广播通道。支持的值有 App.Models.UserApp.Models.Provider,和 App.Models.Customer
  • enable_sound:启用/禁用通知声音。
  • sound_path:公共目录中声音文件的路径。

示例配置(config/nova-realtime-notification.php

return [
    'enabled' => true,
    'broadcast_driver' => 'pusher',
    'broadcast_channel' => 'App.Models.User',
    'enable_sound' => true,
    'sound_path' => 'sounds/sound1.mp3',
];

使用方法

广播通知

  1. 创建一个新的通知

    php artisan make:notification AdminNotification
  2. 在你的通知类中实现 viatoNovatoBroadcast 方法,其中包含 NovaChannel,并将 broadcast 添加到 via 方法中

    namespace App\Notifications;
    
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Notifications\Messages\BroadcastMessage;
    use Illuminate\Notifications\Notification;
    use Laravel\Nova\Notifications\NovaChannel;
    use Laravel\Nova\Notifications\NovaNotification;
    
    class AdminNotification extends Notification implements ShouldQueue
    {
        use Queueable;
    
        private $message;
        private $actionPath;
        private $actionLabel;
        private $level;
    
        public function __construct($message, $actionPath, $actionLabel, $level)
        {
            $this->message = $message;
            $this->actionPath = $actionPath;
            $this->actionLabel = $actionLabel;
            $this->level = $level;
        }
    
        public function via($notifiable)
        {
            return [NovaChannel::class, 'broadcast'];
        }
    
        public function toNova()
        {
            return (new NovaNotification)
                ->message($this->message)
                ->action($this->actionLabel, $this->actionPath)
                ->icon('bell')
                ->type($this->level);
        }
    
        public function toBroadcast(object $notifiable): BroadcastMessage
        {
            return new BroadcastMessage([
                'message' => $this->message,
                'action_label' => $this->actionLabel,
                'action_path' => $this->actionPath,
                'level' => $this->level,
                'duration' => 7000,
            ]);
        }
    }
  3. 从应用程序逻辑触发通知

    use App\Notifications\AdminNotification;
    use App\Models\User;
    
    $user = User::find(1);
    $user->notify(new AdminNotification('You have a new admin message!', '/admin/messages', 'View', 'info'));

贡献

感谢您考虑为 Laravel Nova 实时通知广播包做出贡献!请阅读贡献指南以获取详细信息。

许可证

此包是开源软件,使用MIT 许可证授权。

截图

Nova Real-time Notification Nova Real-time Notification