Laravel Livewire回声配置

Laravel Livewire回声配置,laravel,events,echo,pusher,laravel-livewire,Laravel,Events,Echo,Pusher,Laravel Livewire,我已经在我的Laravel 8应用程序上设置了一个带有Pusher和Echo的通知系统。它工作得很好,我可以用 window.Echo.private('App.Models.User.' + User.id) .notification((notification) => { if (notification.type === 'App\\Notifications\\JobLiked') { let count = document.getElementById

我已经在我的Laravel 8应用程序上设置了一个带有Pusher和Echo的通知系统。它工作得很好,我可以用

window.Echo.private('App.Models.User.' + User.id)
.notification((notification) => {
    if (notification.type === 'App\\Notifications\\JobLiked') {
        let count = document.getElementById('count');
        let number = count.innerHTML;
        number++;
        count.innerHTML = number;
    }
});
但现在我想使用Livewire侦听器触发我的函数,然后我设置:

public function getListeners()
{
    return [
        "echo-private:App.Models.User.{$this->authId},NotificationSent" => 'notifyNewJobLiked',
    ];
}
但似乎什么都不起作用,我也没有错误信息。。你知道可能发生什么事吗


非常感谢!:)

尝试使用以下特定事件名称配置侦听器:

public function getListeners()
{
    return [
        "echo-private:App.Models.User.{$this->authId},.Illuminate\\Notifications\\Events\\BroadcastNotificationCreated" => 'notifyNewJobLiked',
    ];
}
由于您使用的是Laravel通知来触发广播而不是可广播事件,因此触发时的事件名称默认为
light\\Notifications\\Events\\BroadcastNotificationCreated

在Echo中,有两种方法可以侦听传入消息:
通知
侦听
。它与vanilla js配合使用的原因是您使用的是
通知
方法,而livewire事件侦听器仅与Echo的
侦听
配合使用,后者需要调用事件的名称

如果您使用的是pusher,那么可以在pusher调试控制台中看到调用事件的名称


另外,请注意,在名称空间事件前面添加一个点,如中所述。

在livewire中获取回音通知的正确方法:

public function getListeners()
    {
        $user_id = auth()->user()->id;

        return [
            "echo-notification:App.Models.User.{$user_id}, notification" => 'gotNotification'
        ];
    }
如您所见,通道类型为通知,而非专用:

回显通知

另外,不要忘记在您的频道路由中有一个验证用户身份的记录,就像您对私人频道所做的一样:

Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});
我学到的另一件事是,您可以从返回的字段中获取通知通道数据。 因此,您可以:

public function gotNotification($notification)
{
    
}