Laravel Echo+Pusher+Livewire:客户端无法接收通知
问题:通知已广播到Pusher,但Livewire组件无法接收响应
我能把通知广播到Pusher,但没法在Livewire组件里收到响应。以下是我的代码和尝试过的操作:
1. Notification类代码
<?php namespace App\Notifications; use App\Models\Statement; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; class StatementCompletedNotification extends Notification implements ShouldQueue, ShouldBroadcast { use Queueable; public $statement; /** * Create a new notification instance. * * @return void */ public function __construct(Statement $statement) { $this->statement = $statement; } /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable) { return ['database', 'broadcast']; } /** * Get the array representation of the notification. * * @param mixed $notifiable * @return array */ public function toArray($notifiable) { return [ 'user_id' => $this->statement->uploadedBy->id, 'statement_id' => $this->statement->id, 'file_name' => $this->statement->original_file_name ]; } /** * Get the channels the event should broadcast on. * * @return \Illuminate\Broadcasting\Channel|array */ public function broadcastOn() { return new PrivateChannel('users.' . $this->statement->uploadedBy->id); } }
2. Livewire组件中尝试过的两种getListeners()写法
写法一:
public function getListeners() { return [ "echo-private:users.{$this->user->id},StatementCompletedNotification" => 'refreshNotifications' ]; }
写法二:
注意到Pusher中的事件类型为Illuminate\Notifications\Events\BroadcastNotificationCreated,参考资料后尝试:
public function getListeners() { return [ "echo-private:users.{$this->user->id},.Illuminate\\Notifications\\Events\\BroadcastNotificationCreated" => 'refreshNotifications' ]; }
两种写法都没生效。
3. 前端JavaScript尝试接收
Echo.private('users.1') .notification((notification) => { console.log(notification); });
同样没有任何响应,同时发现队列中的通知被多次处理。
4. 业务流程
触发未队列的StatementCompleted事件,由队列化的监听器调用StatementCompletedNotification类:
public function handle($event) { $event->statement->uploadedBy->notify(new StatementCompletedNotification($event->statement)); }
内容的提问来源于stack exchange,提问作者hyphen
相关产品推荐
相关产品推荐

