laravel - laravel Echo 没有收到带有推送器的私人频道通知
问题描述
我使用 laravel 通知来广播通知,使用自定义用户模型 App\Models\TUser,一切正常,推送器调试控制台接收到广播消息,但我无法在项目的客户端使用 Laravel回声。
<?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;
});
解决方案
添加gurad
Broadcast::channel('App.Models.TUser.{id}', function ($user, $classID) {;
return true;
},['guards' => ['web', 'auth']]);
推荐阅读
- node.js - 链接令牌如何返回公共令牌?公共令牌 = request.body.publictoken?
- firebase - 如何在flutter中成功检测firebase用户注册和登录
- c# - Wpf 上的音频截断 (C#)
- c# - 使用 MaterialDesign 在 DatePicker WPF 中更改样式
- reactjs - 计算嵌套对象数组中的键
- python - 如何使用python将许多(n)个数据框保存到一个Excelsheet中而不删除前一个数据框
- firebase - 我应该如何构建此代码?我在颤动时收到以下错误
- lua-api - 元表作为表字段的值
- python - GridSearchCV 中的 best_score_ 和 r2_score() 之间的区别
- html - 如何通过将鼠标悬停在按钮上来更改图像的 src?