Laravel通知-延迟电子邮件发送并在满足条件时取消 [英] Laravel Notifications - delay email sending and cancel if condition met
问题描述
我有一个应用程序,我要在其中发送一个推送通知,如果用户已登录该应用程序就可以了-但是,如果不是,或者他们没有在X分钟内阅读通知,我想发送给他们发电子邮件.
I have an app where I am sending a push notification which is fine if the user is logged into the application - however, if they're not / if they have not read the notification within X minutes I'd like to send them an email.
我要解决的方法是使用Laravel Notifications创建邮件,广播和数据库通知.在toMail()
方法上,我将延迟返回一个可邮寄邮件-
The way I am going about this is to use Laravel Notifications to create a mail, broadcast & database notification. On the toMail()
method I'm returning a mailable with a delay -
public function toMail($notifiable)
{
return (new \App\Mail\Order\NewOrder($this->order))
->delay(now()->addMinutes(10));
}
几分钟后,电子邮件将发送但是,在发送继续之前,我想检查一下是否已将推送/数据库通知标记为已读和如果已取消电子邮件发送.我能想到的唯一方法是绑定到Laravel中烘焙的MessageSending
事件-
After the minutes are up, the email will send but, before the send goes ahead I'd like to perform a check to see if the push/database notification has already been marked as read and if it has cancel the email send. The only way I can think to do this is to bind to the MessageSending
event that is baked into Laravel -
// listen for emails being sent
'Illuminate\Mail\Events\MessageSending' => [
'App\Listeners\Notification\SendingEmail'
],
唯一的问题是此侦听器收到一个Swift邮件事件,而不是我发送的原始邮件,因此我不知道如何取消它.有什么想法要先感谢吗?
The only problem is this listener receives a Swift mail event and not the original mailable I was dispatching so I don't know how to cancel it. Any ideas and thanks in advance?
推荐答案
类扩展了通知
public function via($notifiable)
{
if($this->dontSend($notifiable)) {
return [];
}
return ['mail'];
}
public function dontSend($notifiable)
{
return $this->appointment->status === 'cancelled';
}
EventServiceProvider类
Class EventServiceProvider
protected $listen = [
NotificationSending::class => [
NotificationSendingListener::class,
],
];
NotificationSendingListener类
Class NotificationSendingListener
public function handle(NotificationSending $event)
{
if (method_exists($event->notification, 'dontSend')) {
return !$event->notification->dontSend($event->notifiable);
}
return true;
}
有关更多详细信息,请参见文章在Laravel中处理延迟的通知
For more details look article Handling delayed notifications in Laravel
这篇关于Laravel通知-延迟电子邮件发送并在满足条件时取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!