Laravel 7 事件未在推送器上广播
Laravel 7 events are not broadcasting on pusher
大家好我想发送通知然后广播。我按照所有步骤成功发送和接收通知,但我不知道发生了什么它停止广播我的事件只有通知仍在数据库中保存。谁能告诉我问题出在哪里?提前致谢。
下面是我的 SendNotiffication 控制器
<?php
namespace App\Http\Controllers\API;
use App\Events\AttachmentEvent;
use App\Events\MessageEvent;
use App\Http\Controllers\Controller;
use App\Notifications\AttachmentNotification;
use App\Notifications\MessageNotification;
use Illuminate\Http\Request;
use Notification;
class SendNotificationController extends Controller
{
public function sendNotification( $recipients_list, $type, $message, $data_params ){
$recipients = $recipients_list;
if (isset($recipients) && $recipients != NULL) {
$data = $data_params;
$message = $message;
Notification::send($recipients, new MessageNotification($data,$type,$message));
foreach ($recipients as $key => $usr) {
event(new MessageEvent($usr, $data));
}
}
}
}
这是我的消息事件
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class MessageEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public $user;
public $newTask;
public function __construct($user,$newTask)
{
$this->user = $user;
$this->newTask = $newTask;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastWith(){
$newNotification = $this->user->unreadNotifications->first();
return [
'newNotification' => $newNotification
];
}
public function broadcastOn()
{
// return new PrivateChannel('message');
return new Channel('user.'. $this->user->id);
}
}
在我的 TasksController 中,我包含了这样的 SendNotificationController
protected $AttachmentsController;
protected $SendNotificationController;
public function __construct(AttachmentsController $AttachmentsController, SendNotificationController $SendNotificationController)
{
$this->AttachmentsController = $AttachmentsController;
$this->SendNotificationController = $SendNotificationController;
}
下面是我的 taskPriority 函数,用于更改优先级并发送通知
public function TaskPriority($id){
$data = preg_split('/\d+\K/', $id);
$id = $data[0];
$priority = $data[1];
$logedInuser = auth('sanctum')->user();
$user_id = $logedInuser->id;
if (isset($user_id) && $user_id != NULL) {
$task = Task::find($id);
$task->priority = $priority;
$task->save();
}
// ========================================================================================================== //
// ========================================== ACTIVITY PERFORMED ========================================== //
// ========================================================================================================== //
$this->saveActivity('task', $task->id,'priority',$task->assignee_id,'Changed task Priority');
// ========================================================================================================== //
// ========================================== SENDING NOTIFICATION ========================================== //
// ========================================================================================================== //
$recipients = [];
$recipients = $this->getRecipients($task->id);
$recipients = array_filter($recipients);
$type = "task_priority";
$message = $logedInuser->name.' has changed the priority of the task '.$task->title.' to '.$task->priority;
$data = [
'task_id' => $task->id,
'route' =>'task',
'data' => $task,
];
$this->SendNotificationController->sendNotification( $recipients, $type, $message, $data );
return response()->json('The Priority successfully updated');
}
在通知视图中,我得到了与用户相关的所有通知。
<template>
<div class="notifications ">
<div class="title">
<a @click="notificationsShowHide"
>Notifications {{ unReadNotifications.length }} <i class="fa fa-bell"></i
></a>
</div>
<div class="noti_panel" v-if="show">
<div class="notif_listing">
<div class="top_panel">
<div class="row">
<div class="col-6">
<button class="text-left">See all notifications</button>
</div>
<div class="col-6">
<button class="text-right">Mark all as read</button>
</div>
</div>
</div>
<ul>
<li
v-for="(notif, key) in unReadNotifications"
:key="key"
>
<a v-if="notif.data.type == 'task_status'" href="javascript:;" @click="markAsUnread(notif)">{{ notif.data.message }} <br><span class="icon"><i class="fa fa-battery-half"></i>Task status changed</span></a>
<a v-if="notif.data.type == 'attachment'" href="javascript:;" @click="markAsUnread(notif)">{{ notif.data.message }}<br><span class="icon"><i class="fa fa-paperclip"></i>New Attachment</span></a>
<a v-if="notif.data.type == 'task_update'" href="javascript:;" @click="markAsUnread(notif)">{{ notif.data.message }}<br><span class="icon"><i class="fa fa-pencil"></i>Task updated</span></a>
<a v-if="notif.data.type == 'task_priority'" href="javascript:;" @click="markAsUnread(notif)">{{ notif.data.message }}<br><span class="icon"><i class="fa fa-align-left"></i>Task Priority changed</span></a>
<a v-if="notif.data.type == 'task_created'" href="javascript:;" @click="markAsUnread(notif)">{{ notif.data.message }}<br><span class="icon"><i class="fa fa-plus-circle"></i>Task ceated</span></a>
<a v-if="notif.data.type == 'new_comment'" href="javascript:;" @click="markAsUnread(notif)">{{ notif.data.message }}<br><span class="icon"><i class="fa fa-comment"></i>New Comment</span></a>
<a v-if="notif.data.type == 'task_delete'" href="javascript:;" @click="markAsUnread(notif)">{{ notif.data.message }}<br><span class="icon"><i class="fa fa-trash"></i>Task Deleted</span></a>
<a v-if="notif.data.type == 'discussion'" href="javascript:;" @click="markAsUnread(notif)">{{ notif.data.message }}<br><span class="icon"><i class="fa fa-comment"></i>Discussion Created</span></a>
<button @click="removeNotification(notif)"><i class="fa fa-close"></i></button>
</li>
</ul>
</div>
</div>
</div>
</template>
<script>
import User from "../apis/User";
import { mapGetters } from "vuex";
export default {
data() {
return {
show: "",
unReadNotifications: [],
current_notif:'',
message:'',
user: "",
type:null,
};
},
computed:{
// ...mapGetters(['user'])
},
methods: {
notificationsShowHide() {
if (this.show == true) {
this.show = false;
} else {
this.show = true;
}
},
markAsUnread(notif){
this.$store.dispatch("relatedTasks", notif.data.data.data.project_id);
User.markAsUnread(notif)
.then(response => {
if (notif.data.data.route == 'task') {
this.$store.dispatch({
type: 'getRelatedAttachments',
referenceId: notif.data.data.task_id, // reference ID
attachmentType: notif.data.data.route // task
});
this.$store.dispatch({
type: 'getCommentById',
referenceId: notif.data.data.task_id, // reference ID
entityName: notif.data.data.route // task
});
this.$router.push('/taskDetail/'+notif.data.data.task_id).catch(()=>{})
}
if (notif.data.data.route == "subtask") {
this.$store.dispatch({
type: 'getRelatedAttachments',
referenceId: notif.data.data.subtask_id, // reference ID
attachmentType: notif.data.data.route // subtask
});
this.$store.dispatch({
type: 'getCommentById',
referenceId: notif.data.data.subtask_id, // reference ID
entityName: notif.data.data.route // task
});
this.$router.push('/SubTaskDetail/'+notif.data.data.subtask_id).catch(()=>{})
}
this.unReadNotifications.splice(notif, 1);
})
},
removeNotification(notif) {
User.removeNotification(notif)
.then(response => {
this.notif_count--;
this.unReadNotifications.splice(notif, 1);
if (this.unReadNotifications.length == 0) {
this.show = false
}
})
},
getAuthUser() {
User.getAuthUser()
.then(response => {
this.user = response.data
// console.log(response.data.id)
window.Echo.channel("user." + response.data.id).listen(
"MessageEvent",
e => {
console.log(e)
this.unReadNotifications.unshift(e.newNotification);
this.message = e.message
this.type = e.type
// this.$toastr.s("New notification");
}
);
});
},
getUserUnReadNotifications(){
User.getUserUnReadNotifications()
.then(response => {
this.unReadNotifications = response.data
})
}
},
created() {
this.getAuthUser();
this.getUserUnReadNotifications()
}
};
</script>
通过 运行
找到了适合自己的解决方案
php artisan queue:work
大家好我想发送通知然后广播。我按照所有步骤成功发送和接收通知,但我不知道发生了什么它停止广播我的事件只有通知仍在数据库中保存。谁能告诉我问题出在哪里?提前致谢。
下面是我的 SendNotiffication 控制器
<?php
namespace App\Http\Controllers\API;
use App\Events\AttachmentEvent;
use App\Events\MessageEvent;
use App\Http\Controllers\Controller;
use App\Notifications\AttachmentNotification;
use App\Notifications\MessageNotification;
use Illuminate\Http\Request;
use Notification;
class SendNotificationController extends Controller
{
public function sendNotification( $recipients_list, $type, $message, $data_params ){
$recipients = $recipients_list;
if (isset($recipients) && $recipients != NULL) {
$data = $data_params;
$message = $message;
Notification::send($recipients, new MessageNotification($data,$type,$message));
foreach ($recipients as $key => $usr) {
event(new MessageEvent($usr, $data));
}
}
}
}
这是我的消息事件
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class MessageEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public $user;
public $newTask;
public function __construct($user,$newTask)
{
$this->user = $user;
$this->newTask = $newTask;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastWith(){
$newNotification = $this->user->unreadNotifications->first();
return [
'newNotification' => $newNotification
];
}
public function broadcastOn()
{
// return new PrivateChannel('message');
return new Channel('user.'. $this->user->id);
}
}
在我的 TasksController 中,我包含了这样的 SendNotificationController
protected $AttachmentsController;
protected $SendNotificationController;
public function __construct(AttachmentsController $AttachmentsController, SendNotificationController $SendNotificationController)
{
$this->AttachmentsController = $AttachmentsController;
$this->SendNotificationController = $SendNotificationController;
}
下面是我的 taskPriority 函数,用于更改优先级并发送通知
public function TaskPriority($id){
$data = preg_split('/\d+\K/', $id);
$id = $data[0];
$priority = $data[1];
$logedInuser = auth('sanctum')->user();
$user_id = $logedInuser->id;
if (isset($user_id) && $user_id != NULL) {
$task = Task::find($id);
$task->priority = $priority;
$task->save();
}
// ========================================================================================================== //
// ========================================== ACTIVITY PERFORMED ========================================== //
// ========================================================================================================== //
$this->saveActivity('task', $task->id,'priority',$task->assignee_id,'Changed task Priority');
// ========================================================================================================== //
// ========================================== SENDING NOTIFICATION ========================================== //
// ========================================================================================================== //
$recipients = [];
$recipients = $this->getRecipients($task->id);
$recipients = array_filter($recipients);
$type = "task_priority";
$message = $logedInuser->name.' has changed the priority of the task '.$task->title.' to '.$task->priority;
$data = [
'task_id' => $task->id,
'route' =>'task',
'data' => $task,
];
$this->SendNotificationController->sendNotification( $recipients, $type, $message, $data );
return response()->json('The Priority successfully updated');
}
在通知视图中,我得到了与用户相关的所有通知。
<template>
<div class="notifications ">
<div class="title">
<a @click="notificationsShowHide"
>Notifications {{ unReadNotifications.length }} <i class="fa fa-bell"></i
></a>
</div>
<div class="noti_panel" v-if="show">
<div class="notif_listing">
<div class="top_panel">
<div class="row">
<div class="col-6">
<button class="text-left">See all notifications</button>
</div>
<div class="col-6">
<button class="text-right">Mark all as read</button>
</div>
</div>
</div>
<ul>
<li
v-for="(notif, key) in unReadNotifications"
:key="key"
>
<a v-if="notif.data.type == 'task_status'" href="javascript:;" @click="markAsUnread(notif)">{{ notif.data.message }} <br><span class="icon"><i class="fa fa-battery-half"></i>Task status changed</span></a>
<a v-if="notif.data.type == 'attachment'" href="javascript:;" @click="markAsUnread(notif)">{{ notif.data.message }}<br><span class="icon"><i class="fa fa-paperclip"></i>New Attachment</span></a>
<a v-if="notif.data.type == 'task_update'" href="javascript:;" @click="markAsUnread(notif)">{{ notif.data.message }}<br><span class="icon"><i class="fa fa-pencil"></i>Task updated</span></a>
<a v-if="notif.data.type == 'task_priority'" href="javascript:;" @click="markAsUnread(notif)">{{ notif.data.message }}<br><span class="icon"><i class="fa fa-align-left"></i>Task Priority changed</span></a>
<a v-if="notif.data.type == 'task_created'" href="javascript:;" @click="markAsUnread(notif)">{{ notif.data.message }}<br><span class="icon"><i class="fa fa-plus-circle"></i>Task ceated</span></a>
<a v-if="notif.data.type == 'new_comment'" href="javascript:;" @click="markAsUnread(notif)">{{ notif.data.message }}<br><span class="icon"><i class="fa fa-comment"></i>New Comment</span></a>
<a v-if="notif.data.type == 'task_delete'" href="javascript:;" @click="markAsUnread(notif)">{{ notif.data.message }}<br><span class="icon"><i class="fa fa-trash"></i>Task Deleted</span></a>
<a v-if="notif.data.type == 'discussion'" href="javascript:;" @click="markAsUnread(notif)">{{ notif.data.message }}<br><span class="icon"><i class="fa fa-comment"></i>Discussion Created</span></a>
<button @click="removeNotification(notif)"><i class="fa fa-close"></i></button>
</li>
</ul>
</div>
</div>
</div>
</template>
<script>
import User from "../apis/User";
import { mapGetters } from "vuex";
export default {
data() {
return {
show: "",
unReadNotifications: [],
current_notif:'',
message:'',
user: "",
type:null,
};
},
computed:{
// ...mapGetters(['user'])
},
methods: {
notificationsShowHide() {
if (this.show == true) {
this.show = false;
} else {
this.show = true;
}
},
markAsUnread(notif){
this.$store.dispatch("relatedTasks", notif.data.data.data.project_id);
User.markAsUnread(notif)
.then(response => {
if (notif.data.data.route == 'task') {
this.$store.dispatch({
type: 'getRelatedAttachments',
referenceId: notif.data.data.task_id, // reference ID
attachmentType: notif.data.data.route // task
});
this.$store.dispatch({
type: 'getCommentById',
referenceId: notif.data.data.task_id, // reference ID
entityName: notif.data.data.route // task
});
this.$router.push('/taskDetail/'+notif.data.data.task_id).catch(()=>{})
}
if (notif.data.data.route == "subtask") {
this.$store.dispatch({
type: 'getRelatedAttachments',
referenceId: notif.data.data.subtask_id, // reference ID
attachmentType: notif.data.data.route // subtask
});
this.$store.dispatch({
type: 'getCommentById',
referenceId: notif.data.data.subtask_id, // reference ID
entityName: notif.data.data.route // task
});
this.$router.push('/SubTaskDetail/'+notif.data.data.subtask_id).catch(()=>{})
}
this.unReadNotifications.splice(notif, 1);
})
},
removeNotification(notif) {
User.removeNotification(notif)
.then(response => {
this.notif_count--;
this.unReadNotifications.splice(notif, 1);
if (this.unReadNotifications.length == 0) {
this.show = false
}
})
},
getAuthUser() {
User.getAuthUser()
.then(response => {
this.user = response.data
// console.log(response.data.id)
window.Echo.channel("user." + response.data.id).listen(
"MessageEvent",
e => {
console.log(e)
this.unReadNotifications.unshift(e.newNotification);
this.message = e.message
this.type = e.type
// this.$toastr.s("New notification");
}
);
});
},
getUserUnReadNotifications(){
User.getUserUnReadNotifications()
.then(response => {
this.unReadNotifications = response.data
})
}
},
created() {
this.getAuthUser();
this.getUserUnReadNotifications()
}
};
</script>
通过 运行
找到了适合自己的解决方案php artisan queue:work