我使用laravel通知通过自定义用户模型App \ Models \ TUser广播该通知,并且一切正常,push调试控制台收到了广播的消息,但是我无法在客户端Laravel echo进行项目。
<?php
namespace App\Notifications;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Queue\SerializesModels;
class NotifySubscribers extends Notification implements ShouldBroadcast
{
// use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
protected $class;
public function __construct($class)
{
$this->class = $class;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['broadcast'];
}
public function toBroadcast($notifiable)
{
return new BroadcastMessage([
'lesson_name' => 'test'
]);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'lesson_name' => 'test'
];
}
}
客户端
Echo.private('App.Models.TUser.' + "{{ auth('front')->id() }}")
.notification(function (notification) {
alert('test');
});
此处触发代码的功能
Route::get('notify/user', function() {
$user = App\Models\TUser::find(8);
$user->notify(new \App\Notifications\NotifySubscribers(App\Models\TClass::first()));
});
// channels file
Broadcast::channel('App.Models.TUser.{id}', function ($user, $classID) {;
return true;
});
答案 0 :(得分:0)
添加gurad
Broadcast::channel('App.Models.TUser.{id}', function ($user, $classID) {;
return true;
},['guards' => ['web', 'auth']]);