我正在尝试使用带有 laravel api 的推送器发送在私人频道上广播的通知。发送通知时一切正常,推送服务器正在接收事件。但是,当我尝试收听这些事件时,控制台中没有显示任何内容。pusher 中的错误日志显示如下:
这是我的 main.js 代码来收听广播
let userId=document.head.querySelector('meta[name="user-id"').content;
console.log(userId)
window.Echo.private('App.User.' + userId)
.notification((notification) => {
console.log(notification.type);
});
这是我的 bootstrap.js 代码
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
forceTLS: true,
});
这是来自welcome.blade.php 的元标记
<meta name="csrf-token" content="{{ csrf_token() }}">
<meta name="user-id" content="{{Auth::check() ? Auth::user()->id:''}}">
这是我的 channels.php 文件代码
Broadcast::channel('App.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
},['guards'=>['api']]);
最后是来自 BroadcastServiceProvider.php 的启动函数
public function boot()
{
Broadcast::routes(['middleware' => ['auth:api']]);
require base_path('routes/channels.php');
}
任何帮助将不胜感激。提前致谢。