我想制作实时的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));
这是用于绑定和接收推送事件的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>
在Debug Console Pusher中成功订阅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无法接收它。
答案 0 :(得分:2)
通过以下操作,我的问题得以解决:
1-在第18行的config / broadcasting.php文件中,我将'default' => env('BROADCAST_DRIVER', 'pusher'),
更改为'default' => 'pusher',
。
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'
答案 1 :(得分:1)
您是否可以登录Pusher仪表板,转到调试控制台,并在从Laravel代码触发事件时粘贴出现在那里的事件的JSON?
我怀疑你没有看到它们的原因是因为你需要绑定到private-App.User.1
而不只是App.User.1
。
答案 2 :(得分:-2)
简单地将此方法添加到事件类
public function broadcastAs()
{
return 'event-name';
}