我试图使用简单的测试通知向Laravel应用广播 Pusher.Within我的网页,我已经有了这个JavaScript来听取 在bootstrap.js中广播
import Echo from 'laravel-echo'
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'notmyrealpusherkey',
encrypted: true
});
这是我的Notification.vue
Echo.private('App.User.' + this.id)
.notification((notification) => {
console.log(notification);
toastr.info(notification.message, notification.subject);
});
我的BroadcastServiceProvider配置为验证私人频道请求:
Broadcast::channel('App.User.{id}', function ($user, $id) {
return (int)$user->id === (int)$id;
});
NewFriendRequest只是一个简单的测试通知:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class NewFriendRequest extends Notification implements ShouldQueue
{
use Queueable;
public $user;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($user)
{
$this->user = $user;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail','broadcast','database'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('You received a new friend request from ' . $this->user->name)
->action('View Profile', route('profile', ['slug' => $this->user->slug]))
->line('Thank you for using our Tictac!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'name' => $this->user->name,
'message' => $this->user->name . ' sent you friend request.'
];
}
}
我也试过从Laravel方面触发它,但它仍然无法到达用户的浏览器:
$resp = Auth::user()->add_friend($id);
User::find($id)->notify(new \App\Notifications\NewFriendRequest(Auth::user()));
return $resp;
答案 0 :(得分:0)
您在通知as stated by the documentation中错过了toBroadcast()
方法。
class NewFriendRequest extends Notification implements ShouldQueue
{
public function toBroadcast($notifiable)
{
return new BroadcastMessage([
'message' => 'Insert your message',
'subject' => 'Insert your subject',
]);
}
}