问题描述
Laravel 7
我试图在我的项目中添加Laravel Notification,但是卡在了监听通知中,大多数在广播/身份验证时出错。我已经解决了这个问题,但是仍然没有收到通知,这是我的代码
App \ Notifications \ MessageReceived
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 =推动器和推键等
需要有关此问题的一些指南,我在哪里错了以及应该怎么做才能使这项工作
解决方法
Notification类中的via方法应返回['broadcast']
。
还要从Notifications类中删除use Queueable;
特征。
别忘了在namespace
初始化程序中将Pusher
属性添加为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'
});