Laravel Echo没有实时接收推送通知-想法?
我正试图利用本网站的想法实现一种用户消息传递方法: 其关键思想是使用laravel notifications功能来更新通知表(用于将消息标记为已读),同时将其作为专用频道广播给pusher,并通过laravel Echo监听客户端 我想在添加新练习时发送通知,因此我使用EventServiceProvider侦听数据库创建事件,这就是触发通知的地方:Laravel Echo没有实时接收推送通知-想法?,laravel,laravel-5,pusher,laravel-echo,Laravel,Laravel 5,Pusher,Laravel Echo,我正试图利用本网站的想法实现一种用户消息传递方法: 其关键思想是使用laravel notifications功能来更新通知表(用于将消息标记为已读),同时将其作为专用频道广播给pusher,并通过laravel Echo监听客户端 我想在添加新练习时发送通知,因此我使用EventServiceProvider侦听数据库创建事件,这就是触发通知的地方: Exercise::created(function ($exercise) { foreach ($users as $us
Exercise::created(function ($exercise) {
foreach ($users as $user) {
$user->notify(new NewExercisePosted($user, $exercise));
}
class NewExercisePosted extends Notification implements ShouldBroadcast
{
//use Queueable;
protected $exercise;
protected $user;
public function __construct(User $user, Exercise $exercise)
{
$this->user = $user;
$this->exercise = $exercise;
}
public function via($notifiable)
{
return ['database', 'broadcast'];
}
public function toArray($notifiable)
{
return [
'id' => $this->id,
'read_at' => null,
'data' => [
'user_id' => $this->user->id,
'ex_id' => $this->exercise->id,
],
];
}
}
通知:
Exercise::created(function ($exercise) {
foreach ($users as $user) {
$user->notify(new NewExercisePosted($user, $exercise));
}
class NewExercisePosted extends Notification implements ShouldBroadcast
{
//use Queueable;
protected $exercise;
protected $user;
public function __construct(User $user, Exercise $exercise)
{
$this->user = $user;
$this->exercise = $exercise;
}
public function via($notifiable)
{
return ['database', 'broadcast'];
}
public function toArray($notifiable)
{
return [
'id' => $this->id,
'read_at' => null,
'data' => [
'user_id' => $this->user->id,
'ex_id' => $this->exercise->id,
],
];
}
}
这只是填充通知表并向pusher广播
这是我的主视图文件:
<!DOCTYPE html>
<html>
<head>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link rel="stylesheet" href="/css/app.css")/>
<script src='https://www.google.com/recaptcha/api.js'></script>
<script>
window.Laravel = <?php echo json_encode([
'csrfToken' => csrf_token(),
]); ?>
</script>
<!-- This makes the current user's id available in javascript -->
@if(!auth()->guest())
<script>
window.Laravel.userId = <?php echo auth()->user()->id; ?>
</script>
@endif
</head>
<body>
@include('partials/header')
@if(Session::has('message'))
<div class="alert alert-info">
{{Session::get('message')}}
</div>
@endif
@yield('content')
@include('partials/footer')
@include('partials/analytics')
<script src="/js/app.js"></script>
</body>
</html>
window.Laravel=
@如果(!auth()->guest())
window.Laravel.userId=
@恩迪夫
@包括('部分/标题')
@if(会话::has('message'))
{{Session::get('message')}
@恩迪夫
@产量(‘含量’)
@包括('partials/footer')
@包括('部分/分析')
以下是标题视图的相关部分,其中显示了消息:
<li class="dropdown">
<a class="dropdown-toggle" id="notifications" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<span class="glyphicon glyphicon-user"></span>
</a>
<ul class="dropdown-menu" aria-labelledby="notificationsMenu" id="notificationsMenu">
<li class="dropdown-header">No notifications</li>
</ul>
</li>
';
}
功能路由化(通知){
//console.log(notification.data.data.ex_id);
var to=`?read=${notification.id}`;
if(notification.type==通知类型。follow){
to='用户'+to;
}else if(notification.type==notification\u TYPES.newEx){
const exId=notification.data.data.ex_id;
to=`吉他课ex/${exId}`+to;
}
返回“/”+到;
}
函数makeNotificationText(通知){
var text='';
if(notification.type==通知类型。follow){
const name=notification.data.follower\u name;
text+=`${name}跟着你`;
}else if(notification.type==notification\u TYPES.newEx){
text+=`新练习已发布';
}
返回文本;
}
事情有些进展,但不是很顺利。在我创建一个新练习后,消息立即出现在数据库和Pusher中,当您单击MarkAsRead通知时,通知被标记为已读。问题是:
创建新练习时,客户端不会实时更新。它似乎只在刷新页面时产生更改。
根据我上面所说的,关于如何解决问题有什么建议吗?我对javascript一无所知,尤其是关于变量的范围、执行顺序等。因此我怀疑我忽略了一些细节。在我成为开发人员之前,我是一名吉他手
谢谢
布莱恩我昨天花了一整天的时间试图弄明白这一点,最后归结为*vs{id} 问题出在channels.php文件中,在该文件中完成了通道授权。我使用的是App.User.{id}没有意识到这是按照5.4指令执行的,而实际上5.3需要的是App.User* 我根本没想过要考虑这个问题!现在一切都按预期进行
谢谢,布莱恩,你好,我也在努力学习这个教程。它不起作用,我首先一步一步地跟踪和修改文件,什么都没有,没有推送。然后我决定克隆rep并安装它,用我的推键修改env,等等,但仍然不起作用。我使用的是5.4.16。不知道发生了什么事。