【问题标题】:Can't Listen To Notifications with Laravel Echo无法使用 Laravel Echo 收听通知
【发布时间】:2017-11-27 11:35:07
【问题描述】:

我正在构建一个带有实时通知的网站。但是我无法使用 Laravel Echo 收听通知。我不明白为什么它不起作用。

聆听

this.$echo.private('App.User.' + this.user.id)
    .notification(notification => {
        console.log(notification);
    });

通知:

user->notify(new \App\Notifications\SomeoneRepliedYourPikir([
        'pikir_id' => request()->get('pikir_id'),
        'username' => request()->user()->username
    ]));

频道:

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

我想我粘贴了所有必要的东西来了解我的情况。其他方面工作正常,例如出席频道。

我的 Laravel-echo-server.json

{
"authHost": "http://pikirler.dev",
"authEndpoint": "/broadcasting/auth",
"clients": [
    {
        "appId": "2051a3c22fdd8a43",
        "key": "500c9e2934af4f6e93892f56abfcdc27"
    }
],
"database": "redis",
"databaseConfig": {
    "redis": {
        "port": "6379",
        "host": "127.0.0.1"
    },
    "sqlite": {
        "databasePath": "/database/laravel-echo-server.sqlite"
    }
},
"devMode": true,
"host": "pikirler.dev",
"port": "6001",
"protocol": "http",
"socketio": {},
"sslCertPath": "",
"sslKeyPath": ""}

【问题讨论】:

  • 我们可以看看你的 laravel-echo-server.json 吗?
  • @Ohgodwhy 它正在加入频道并成功触发事件 BroadcastNotificationCreated,但在控制台中有任何内容
  • @Ohgodwhy 哦,我发现了我的愚蠢错误。我的错误是我将所有模型都移到了模型目录中,并且在 channels.php 中没有更改。愚蠢的错误花了我几个小时才解决:D
  • 是的,这是一个非常常见的错误!很高兴你解决了。
  • 我可以看看你的 channels.php 文件吗?无法更改名称空间。我还更改了模型目录,通知停止工作

标签: laravel real-time laravel-echo


【解决方案1】:

将此功能放入您的用户模型中。它将正常工作

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

【讨论】:

    猜你喜欢
    • 2020-02-10
    • 2017-06-20
    • 2020-02-13
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 2018-01-21
    • 2018-01-02
    • 2019-09-21
    相关资源
    最近更新 更多