我有一个应用程序,我在其中发送推送通知,如果用户登录到该应用程序,那就没问题 - 但是,如果他们没有登录/如果他们在 X 分钟内没有阅读通知,我想向他们发送一封电子邮件。
我的方法是使用 Laravel 通知来创建邮件、广播和数据库通知。上toMail()
方法我延迟返回可邮寄的邮件 -
public function toMail($notifiable)
{
return (new \App\Mail\Order\NewOrder($this->order))
->delay(now()->addMinutes(10));
}
分钟结束后,电子邮件将发送but,在发送继续之前,我想执行检查以查看推送/数据库通知是否已标记为已读以及是否已取消电子邮件发送。我能想到的唯一方法就是绑定到MessageSending
被烘焙到 Laravel 中的事件 -
// listen for emails being sent
'Illuminate\Mail\Events\MessageSending' => [
'App\Listeners\Notification\SendingEmail'
],
唯一的问题是这个监听器收到一个 Swift 邮件事件,而不是我发送的原始邮件事件,所以我不知道如何取消它。有什么想法并提前致谢吗?
类扩展通知
public function via($notifiable)
{
if($this->dontSend($notifiable)) {
return [];
}
return ['mail'];
}
public function dontSend($notifiable)
{
return $this->appointment->status === 'cancelled';
}
类 EventServiceProvider
protected $listen = [
NotificationSending::class => [
NotificationSendingListener::class,
],
];
类 通知发送监听器
public function handle(NotificationSending $event)
{
if (method_exists($event->notification, 'dontSend')) {
return !$event->notification->dontSend($event->notifiable);
}
return true;
}
更多详情请看文章在 Laravel 中处理延迟通知 https://medium.com/@hotmeteor/handling-delayed-notifications-in-laravel-b6699ec30649
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)