Javascript 广播laravel通知时应使用什么事件名称
我想做实时的laravel通知 为此我做了一切 我的通知类是:Javascript 广播laravel通知时应使用什么事件名称,javascript,php,pusher,laravel-5.3,Javascript,Php,Pusher,Laravel 5.3,我想做实时的laravel通知 为此我做了一切 我的通知类是: class NewChangeRegisterStatusNotif extends Notification { use Queueable; public function __construct ($status_id, $course_id) { $this->status = CourseUserStatus::findOrFail($status_id);
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));
这是用于绑定和接收推送器事件的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>
但问题是,channel.bind()
并没有被激发
正如您所看到的,我将illumb\Notifications\Events\BroadcastNotificationCreated
传递给它,并尝试转义它illumb\\Notifications\\Events\\BroadcastNotificationCreated
,但它们都不起作用
问题是什么
更新:
我发现,尽管laravel广播了事件,但它没有发送到pusher,或者pusher无法接收它。您可以登录pusher仪表板,转到调试控制台,粘贴从laravel代码触发事件时出现的事件的JSON吗
我怀疑您没有看到它们的原因是因为您需要绑定到
私有App.User.1
,而不仅仅是App.User.1
,通过执行以下操作,我的问题得到了解决:
1-在第18行的config/broadcasting.php文件中,我将'default'=>env('BROADCAST\u DRIVER','pusher'),
更改为'default'=>'pusher',
2-更改var频道=pusher.subscribe('App.User.1')
tovar channel=pusher.subscribe('private-App.User.{{{$AuthUser->User_id}')代码>
3-转义channel.bind('light\Notifications\Events\BroadcastNotificationCreated'
到channel.bind('light\\Notifications\\Events\\BroadcastNotificationCreated'
此外,我还遵循了在“简单将此方法添加到事件类”中描述的所有说明
public function broadcastAs()
{
return 'event-name';
}
您可以在laravel文档中阅读更多内容
在“广播名称”部分
默认情况下,Laravel将使用事件的类名广播事件。但是,您可以通过在事件上定义broadcastAs方法自定义广播名称:
/**
* The event's broadcast name.
*
* @return string
*/
public function broadcastAs()
{
return 'server.created';
}
.listen('.server.created', function (e) {
//....
});
如果使用broadcastAs方法自定义广播名称,则应确保使用前导.character注册侦听器。这将指示Echo不要将应用程序的命名空间前置到事件:
/**
* The event's broadcast name.
*
* @return string
*/
public function broadcastAs()
{
return 'server.created';
}
.listen('.server.created', function (e) {
//....
});