我的laravel应用程序具有3个通知渠道,例如main_notification
,conference_notification
和event_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';
}
}
如您所见,我已经尝试同时使用broadcastOn
和broadcastType
方法来更改频道名称,但是我仍然得到默认名称,如下所示:
Channel: myapp_database_private-App.User.1011
Event: Illuminate\Notifications\Events\BroadcastNotificationCreated
如何正确更改?我希望它像这样:
Channel: myapp_database_main_notification
Event: Illuminate\Notifications\Events\BroadcastNotificationCreated