我正在我的 Laravel 项目中实现实时通知。我非常接近实现我的目标。到目前为止,我所做的是将通知保存到数据库中,现在想使用 Pusher 向用户呈现相同的通知。
我收到了 Pusher 的回复,但问题是我无法在我的控制台和另一个用户的控制台下接收通知,使其像实时一样工作。
目前,通知正在发送到 Pusher,保存到数据库中,但通知没有显示到控制台中。
应用程序.js
require('./bootstrap');
//getting the ID's of all logged-in users
let userId = document.head.querySelector('meta[name="user-id"]').content;
Echo.private('App.Models.User.' + userId)
.notification((notification) => {
console.log(notification.type);
});
Vue.component('example', require('./components/Example.vue'));
const app = new Vue({
el: '#app',
});
引导程序.js
window._ = require('lodash');
window.$ = window.jQuery = require('jquery');
require('bootstrap-sass');
window.Vue = require('vue');
require('vue-resource');
Vue.http.interceptors.push((request, next) => {
request.headers.set('X-CSRF-TOKEN', Laravel.csrfToken);
next();
});
import Echo from "laravel-echo"
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key:'8c1d04bb8d1782b874b1',
cluster: 'ap2',
encrypted: false
});
数据库通知.php
<?php
class DatabaseNotification extends Notification
{
use Queueable;
private $subscription;
public function __construct($letter)
{
$this->subscription = $letter;
}
public function via($notifiable)
{
return ['database','broadcast'];
}
public function toDatabase($notifiable)
{
return [
'letter' => $this->subscription,
];
}
public function toBroadcast($notifiable)
{
return [
'letter' => $this->subscription,
'count' => $notifiable->unreadNotifications->count(),
];
}
}