问题描述
我正在我的 Laravel 项目中实现实时通知。我非常接近实现我的目标。到目前为止,我所做的是将通知保存到数据库中,现在想使用 Pusher 向用户呈现相同的通知。
我收到了 Pusher 的响应,但问题是我无法在我的控制台和另一个用户的控制台下接收通知,使其像实时一样工作。
目前,通知正在发送到 Pusher,保存到数据库中,但通知没有显示在控制台中。
App.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',});
Bootstrap.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
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(),];
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)