gpt4 book ai didi

javascript - 广播 Laravel 通知时应使用什么事件名称

转载 作者:行者123 更新时间:2023-11-28 05:34:23 25 4
gpt4 key购买 nike

我想发出实时 Laravel 通知。

为此我做了所有configurations described in docs .

我的通知类别是:

class NewChangeRegisterStatusNotif extends Notification
{
use Queueable;


public function __construct ($status_id, $course_id)
{
$this->status = CourseUserStatus::findOrFail($status_id);
$this->course = Course::findOrFail($course_id)->first(['title']);
}


public function via ($notifiable)
{
return ['database', 'broadcast'];
}


public function toMail ($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', 'https://laravel.com')
->line('Thank you for using our application!');
}


public function toArray ($notifiable)
{
return [
'message' =>
'Some Messages'
,
'action' => '#'
];
}
}

在下面的代码中,创建一个新的通知:

$user = User::findOrFail($user_id);
$user->notify(new NewChangeRegisterStatusNotif($value, $course_id));

这是绑定(bind)和接收推送事件的 JavaScript 客户端代码:

<script src="{{asset('link/to/pusher.min.js')}}"></script>
<script>
Pusher.log = function (msg) {
console.log(msg);
};
var pusher = new Pusher("{{env("PUSHER_KEY")}}");
var channel = pusher.subscribe('App.User.1');
channel.bind('Illuminate\Notifications\Events\BroadcastNotificationCreated', function (data) {
console.log(data);
});
</script>

在调试控制台推送器中订阅 App.User.1 成功。

Laravel 日志中还显示以下消息:

[11:08:31] LOG.info: Broadcasting [Illuminate\Notifications\Events\BroadcastNotificationCreated] on channels [private-App.User.1] with payload:
{
"message": "Some Message",
"action": "#",
"id": "57d9e4dd-09cd-4cd2-ba3d-06f3fcf2af84",
"type": "App\\Notifications\\NewChangeRegisterStatusNotif",
"socket": null
}

但问题是 channel.bind() 没有被触发。

如您所见,我将 Illuminate\Notifications\Events\BroadcastNotificationCreated 传递给它并尝试转义它 Illuminate\\Notifications\\Events\\BroadcastNotificationCreated 但没有它们不起作用。

问题是什么?

更新:

我发现尽管laravel有广播事件,但它没有发送到pusher或者Pusher无法接收它。

最佳答案

通过执行以下操作,我的问题得到解决:

1- 在 config/broadcasting.php 文件第 18 行中,我将 'default' => env('BROADCAST_DRIVER', 'pusher'), 更改为 'default' = >“插入者”,

2- 将 var channel = Pusher.subscribe('App.User.1'); 更改为 var channel = Pusher.subscribe('private-App.User.{{$ AuthUser->user_id}}');

3- 将 channel.bind('Illuminate\Notifications\Events\BroadcastNotificationCreated' 转义为 channel.bind('Illuminate\\Notifications\\Events\\BroadcastNotificationCreated')

我还遵循了https://stackoverflow.com/a/30396900/498504中描述的所有说明。

关于javascript - 广播 Laravel 通知时应使用什么事件名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39463967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com