首页 > 解决方案 > Laravel echo 不监听私人通知回调。尝试了互联网上的所有内容

问题描述

我一直在调试我的 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){}
}

频道.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"
}

不过,我的公共频道运行良好。我的私人频道缺少什么。

标签: laravelechopusherlaravel-echo

解决方案


解决方案

由于某种原因,我的广播频道不适用于私人频道。因此,我必须准确指定要接收哪个频道通知。所以,我User像这样更新模型并且它起作用了。

 public function receivesBroadcastNotificationsOn()
    {
        return 'App.User.'.$this->id;
    }

推荐阅读