【发布时间】:2017-01-20 16:44:00
【问题描述】:
我想制作实时 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 有 Broadcasting 事件,但它没有发送到 pusher 或者 Pusher 无法接收。
【问题讨论】:
标签: javascript php pusher laravel-5.3