这与我之前提出的一个问题有关:我是否使事件/侦听器/通知过于复杂?。
根据收到的有关该问题的反馈,我修改了尝试的方法,以执行以下操作:
所以首先我发送数据库通知:
Notification::send($this->requestedBy, new SendMonthlySummaryCreatedNotification($fileDownloadUrl, $fileName));
通知类如下所示:
class SendMonthlySummaryCreatedNotification extends Notification implements ShouldQueue
{
use Queueable;
public $fileDownloadUrl;
public $fileName;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($fileDownloadUrl, $fileName)
{
$this->fileDownloadUrl = $fileDownloadUrl;
$this->fileName = $fileName;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database'];
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
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
];
}
}
我在文档中找到了这个示例,介绍了如何将
$dispatchesEvents
属性添加到模型中,我对其进行了修改,将其应用到我创建的扩展 DatabaseNotification
类的新模型中,这是我在 this 上了解到的所以问题。
class Notification extends DatabaseNotification
{
use HasFactory;
protected $dispatchesEvents = [
'created' => NotificationCreatedEvent::class
];
public function users()
{
return $this->belongsTo(User::class, 'notifiable_id');
}
}
理论上,上面应该在发送通知时调度一个事件,然后我就有了
NotificationCreatedEvent
,我想用它来广播给推送者:
class NotificationCreatedEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
protected $notification;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Notification $notification)
{
$this->notification = $notification;
Log::debug($this->notification);
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('users.' . $this->notification->notifiable_id);
}
}
问题是一切都在正常进行,直到
NotificationCreatedEvent
它似乎没有被解雇。我不知道除了将其映射到我的新 Notification
模型中之外是否还需要做任何事情,或者它是否应该正常工作。
我的目标是添加数据库通知,然后每当发生这种情况时将其发送到推送器,以便我可以实时通知用户。这看起来应该可行,但我没有看到 Pusher 中有任何变化。
由于我面临着完全相同的问题,所以我想回答这个问题,即使我可能会迟到。
由于您使用数据库驱动程序进行通知,因此您可以轻松实现 Eloquent 模型广播功能。
模型的所有更改都会自动触发所选频道上的广播。
只需忘记
NotificationCreatedEvent
并直接使用 BroadcastsEvents
类中的 Notification
特性即可。您可以在方法 broadcastOn
和 broadcastAs
中定义要寻址的通道。
class Notification extends DatabaseNotification
{
use HasFactory, BroadcastsEvents;
public function users()
{
return $this->belongsTo(User::class, 'notifiable_id');
}
/**
* Define the model broadcast events and the broadcast channel name.
*/
public function broadcastOn(string $event): array
{
// only broadcast on 'created' and 'updated' events
return match ($event) {
'deleted', 'restored', 'trashed' => [],
default => [new Channel('lima-notifications')],
};
}
/**
* The model event's broadcast name.
*/
public function broadcastAs(string $event): string|null
{
return match ($event) {
'created' => 'notification-created',
'updated' => 'notification-updated',
default => null,
};
}
}