带有 Pusher 和 Livewire 的 Laravel Echo - 客户端未收到通知

问题描述 投票:0回答:2

我可以向 Pusher 广播通知,但无法在 Livewire 组件中收到响应。

这是我的通知类:

<?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);
    }
}

这是我的 Livewire 组件上的

getListeners()
方法。我在这里尝试了几种不同的方法,首先我尝试了文档中显示的方式,只需在侦听器中引用我的 StatementCompletedNotification,如下所示:

    public function getListeners()
    {
        return [
            "echo-private:users.{$this->user->id},StatementCompletedNotification" => 'refreshNotifications'
        ];
    }

我注意到在 Pusher 中,我的事件类型被列为

Illuminate\Notifications\Events\BroadcastNotificationCreated
,并且我在网上找到了 this post,所以我尝试了这样的方法:

    public function getListeners()
    {
        return [
            "echo-private:users.{$this->user->id},.Illuminate\\Notifications\\Events\\BroadcastNotificationCreated" => 'refreshNotifications'
        ];
    }

这两种方法都不适合我。

这是我试图在客户端的 javascript 中取回一些内容的地方:

    Echo.private('users.1')
        .notification((notification) => {
            console.log(notification);
        });

我没有得到任何回复,我不知道问题是什么。我在这上面花了很多时间,但似乎无法弄清楚。

此外,我的通知似乎在队列中被多次选取:

更多背景知识,我现在设置的流程基本上是:

StatementCompleted 事件被触发(未排队),有一个侦听器处理排队的 StatementCompleted 事件,并像这样调用我的 StatementCompletedNotification 类:

public function handle($event)
    {
        $event->statement->uploadedBy->notify(new StatementCompletedNotification($event->statement));
    }
laravel laravel-livewire pusher laravel-9 laravel-echo
2个回答
2
投票

试试这个,用“\”代替“\”

public function getListeners()
{
    return [
        "echo-private:users.{$this->user->id},.Illuminate\Notifications\Events\BroadcastNotificationCreated" => 'refreshNotifications'
    ];
}

0
投票

没有记录,但您可以尝试在逗号(,)后添加点(.)

对我真的很有帮助

public function getListeners()
{
    return [
        "echo-private:channel.{$this->user->id},.App\\Events\\SomeEvent" => '$refresh'
                                                ↑
                                                Here
    ];
}
© www.soinside.com 2019 - 2024. All rights reserved.