Laravel 7
我正在尝试在我的项目中添加Laravel Notification,但仍然停留在侦听通知上,主要是在广播/身份验证时收到错误。我已经解决了这个问题一次,但仍然没有收到通知。这是我的代码
应用\通知\收到的消息
class MessageReceived extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*
*/
public $notification;
public function __construct($notify)
{
//
$this->notification=$notify;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toDatabase($notifiable){
return [
'MessageSender'=>$this->notification,
];
}
public function toArray($notifiable)
{
return [
'MessageSender'=>$this->notification,
];
}
public function toBroadcast($notifiable)
{
return new BroadcastMessage([
'MessageSender'=>$this->notification,
]);
}我的守卫。管理模型
class Admin extends Authenticatable
{
use Notifiable;
protected $guard = 'admin';
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function receivesBroadcastNotificationsOn()
{
return 'admin.'.$this->id;
}
}route/channel.php
Broadcast::channel('App.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
Broadcast::channel('App.Admin.{id}', function ($admin, $id) {
return (int) $admin->id === (int) $id;
},['guards' => ['admin']]);public/ js /listner.js (我在js/app.js文件的链接下面使用了这个js文件链接,并测试了它的工作情况)
Echo.private('App.Admin.${id}' )
.notification((notification) =>{
window.alert(notification.type)
console.log(notification)
});触发通知的控制器方法
public function store(Request $request)
{
//
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required',
'message' => 'required'
]);
if ($validator->fails()) {
return response()->json(['error' => $validator->errors()], 422);
}
$admin = Admin::find(1);
$admin->notify(new MessageReceived($request->email));
return response()->json([
'success'=>'Message sent successfully. we will get to you soon as possible',
// 'data'=>$noty,
], 200);
}resource/js/boostrap.js
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
forceTLS: true,
authEndpoint: "/broadcasting/auth",
auth: {
headers: {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
},
},
// csrfToken:document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
});
window.Pusher.log = function(message ,err){
console.log(message);
console.log(err)
}这是我所有的代码,但我仍然不能监听我触发的通知,我的环境文件中所有的值都是正确的,如BROADCAST_DRIVER=pusher和pusher键等
我需要一些关于这方面的指导,我哪里错了,我应该怎么做才能让这件事起作用
发布于 2020-11-09 14:23:24
Notification类中的via方法应该返回['broadcast']。
还要从通知类中删除use Queueable;特征。
不要忘记在Pusher初始化器中添加namespace属性作为App.Events。
window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
forceTLS: true,
authEndpoint: "/broadcasting/auth",
namespace: 'App.Events'
});https://stackoverflow.com/questions/64726579
复制相似问题