如何在Laravel 7中更改通知广播频道名称?

时间:2020-05-15 18:07:33

标签: php laravel

我的laravel应用程序具有3个通知渠道,例如main_notificationconference_notificationevent_notification

我希望使用laravel echo服务器和socket.io实时发送它。我已经创建了UserCreated通知,该通知应该在main_notification频道中。脚本如下所示:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class UserCreated extends Notification implements ShouldQueue
{
    use Queueable;

    public $title;
    public $event;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($title, $event)
    {
        $this->title = $title;
        $this->event = $event;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['broadcast', 'database'];
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }

    public function toDatabase()
    {
        return [
            'title' => $this->title,
            'event' => $this->event,
        ];
    }

    public function toBroadcast()
    {
        return new BroadcastMessage([
            'title' => $this->title,
            'event' => $this->event,
        ]);
    }

    public function broadcastOn()
    {
        return [
            new \Illuminate\Broadcasting\Channel('main_notification')
        ];
    }

    public function broadcastType()
    {
        return 'main_notification';
    }
}

如您所见,我已经尝试同时使用broadcastOnbroadcastType方法来更改频道名称,但是我仍然得到默认名称,如下所示:

Channel: myapp_database_private-App.User.1011
Event: Illuminate\Notifications\Events\BroadcastNotificationCreated

如何正确更改?我希望它像这样:

Channel: myapp_database_main_notification
Event: Illuminate\Notifications\Events\BroadcastNotificationCreated

0 个答案:

没有答案