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

创建数据库通知时向Pusher广播事件失败排查

问题:数据库通知创建后未触发广播事件

我按照需求实现了三个流程:触发数据库通知、监听新通知创建、触发事件并通过Pusher广播,但目前NotificationCreatedEvent完全没触发,Pusher收不到任何消息。

相关代码如下:

发送数据库通知的代码

Notification::send($this->requestedBy, new SendMonthlySummaryCreatedNotification($fileDownloadUrl, $fileName));

通知类代码

class SendMonthlySummaryCreatedNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public $fileDownloadUrl;
    public $fileName;

    public function __construct($fileDownloadUrl, $fileName)
    {
        $this->fileDownloadUrl = $fileDownloadUrl;
        $this->fileName = $fileName;
    }

    public function via($notifiable)
    {
        return ['database'];
    }

    public function toArray($notifiable)
    {
        return [
            'title' => 'Monthly Summary Complete',
            'message' => "{$this->fileName} is ready. ",
            'link' => $this->fileDownloadUrl,
            'link_text' => 'Click here to download',
            'show_toast' => true,
            'user_id' => $notifiable->id
        ];
    }
}

自定义Notification模型代码

class Notification extends DatabaseNotification
{
    use HasFactory;

    protected $dispatchesEvents = [
        'created' => NotificationCreatedEvent::class
    ];

    public function users()
    {
        return $this->belongsTo(User::class, 'notifiable_id');
    }
}

事件类代码

class NotificationCreatedEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    protected $notification;

    public function __construct(Notification $notification)
    {
        $this->notification = $notification;
        Log::debug($this->notification);
    }

    public function broadcastOn()
    {
        return new PrivateChannel('users.' . $this->notification->notifiable_id);
    }
}

排查与修复方案

1. 自定义通知模型未被系统使用

Laravel的通知系统默认使用Illuminate\Notifications\DatabaseNotification,你自定义的模型没有被关联到Notifiable模型上,导致配置的$dispatchesEvents无效。

修复:
在你的User模型(即$requestedBy对应的模型)中重写notifications关联方法,指定使用自定义模型:

use App\Models\Notification;

public function notifications()
{
    return $this->morphMany(Notification::class, 'notifiable')->orderBy('created_at', 'desc');
}

2. 队列未执行导致通知延迟

你的通知类实现了ShouldQueue,如果队列进程没启动,通知会被存入队列而不会立即写入数据库,自然不会触发created事件。

修复:

  • 启动队列进程:php artisan queue:work
  • 测试阶段可临时移除ShouldQueue接口,让通知同步执行,验证事件是否触发。

3. 广播配置与频道授权检查

即使事件触发,还要确保广播链路正常:

  • 确认.env中广播驱动为Pusher:BROADCAST_DRIVER=pusher
  • 核对Pusher的APP ID、密钥、集群等配置是否正确
  • 确保私有频道users.{id}的授权逻辑正常(在routes/channels.php中配置),前端能成功订阅。

4. 增加调试日志验证

在AppServiceProvider的boot方法中注册模型观察者,确认自定义模型是否被使用:

use App\Models\Notification;

public function boot()
{
    Notification::created(function ($notification) {
        \Log::info('自定义通知模型已创建', ['notification_id' => $notification->id]);
    });
}

运行后查看日志,如果能看到这条记录,说明模型已生效,再排查事件广播的问题。


内容的提问来源于stack exchange,提问作者hyphen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 19:02:12