You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 17:30:45