laravel - Laravel Livewire Echo 配置
问题描述
我在我的 Laravel 8 应用程序上设置了一个带有 Pusher 和 Echo 的通知系统。它工作正常,我可以在 VanillaJS 中检索通知事件
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',
];
}
但似乎没有任何效果,我也没有错误消息..你知道可能发生什么吗?
非常感谢 !:)
解决方案
在 livewire 中获取 Echo 通知的正确方法:
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)
{
}
推荐阅读
- reactjs - 从登录名通过useContext传递图像时,图像未显示
- spring-boot - 使用spring boot创建下载功能以从服务器下载文件
- ios - 有什么方法可以让按钮在第一次按下时做某事,而在 Swift 中的第二次按下时做其他事情
- datetime - 如何使用 Ms Access Date/Time Extended 数据类型
- java - 删除选定 Spinner 上的所有 liveData 和 Observer
- r - 函数返回数字而不是团队?
- makefile - 如何在 Windows 上的 hunspell 搜索路径中添加字典路径?
- java - 来自多个活动的意图
- reactjs - 为什么 scrollToEnd() 在 Android 上不起作用?
- ms-access - 以 Access 连续形式对计算控件求和