Laravel echo不监听私有通知回调。尝试了互联网上的所有内容

问题描述 投票:1回答:1

我一直在调试laravel Echo。

这是我的bootstrap.js中的内容。我不使用vue.js,而仅使用本地服务器localhost:8000

import EchoObj from 'laravel-echo';

window.Echo = new EchoObj({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    encrypted: true
});

Echo.private('App.User.'+ loggedUser.id)
.notification((notification) => {
    console.log(notification.type);
    alert("listening")
})

我的NotificationEvent控制器:

Class NotificationEvent extends Notification{
    public function via($notifiable)
    {
        return ['database', 'broadcast', 'mail'];
    }

   public function toBroadcast($notifiable){
        return new BroadcastMessage([
            "test" => "nesting"
        ]);
    }

    public function toArray($notifiable){}
    public function toDatabase($notifiable){}
}

Channel.php

Broadcast::channel('App.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});

我知道我的回声为什么不起作用,因为每当我触发类似event(new NotificationEvent)的事件时,我都会检查推送的调试控制台,我在推送程序上收到此消息

{
  "test" : "nesting"
  "id"  : "ecc9912f-0ae2-44dc-b905-677f93bfc38b"
  "type" : "App\\Notifications\\WorkflowUpdateNotification"
}

尽管我的公共频道运行良好。我的私人频道上缺少什么。

laravel echo pusher laravel-echo
1个回答
0
投票

解决方案

出于某种原因,我广播的频道无法在私人频道上播放。因此,我必须确切指定要接收的频道通知。因此,我像这样更新了User模型,它开始起作用。

 public function receivesBroadcastNotificationsOn()
    {
        return 'App.User.'.$this->id;
    }
© www.soinside.com 2019 - 2024. All rights reserved.