代码之家  ›  专栏  ›  技术社区  ›  Chris

Laravel通知-延迟电子邮件发送并在满足条件时取消

  •  1
  • Chris  · 技术社区  · 8 年前

    我将使用Laravel Notifications创建邮件、广播和;数据库通知。上 toMail() 方法我正在返回一个延迟的邮件-

    public function toMail($notifiable)
    {
        return (new \App\Mail\Order\NewOrder($this->order))
            ->delay(now()->addMinutes(10));
    }
    

    会议结束后,电子邮件将发送 但是 ,在发送之前,我想执行一个检查,看看推送/数据库通知是否已标记为已读,以及是否已取消电子邮件发送。我唯一能想到的办法就是和 MessageSending

    // listen for emails being sent
    'Illuminate\Mail\Events\MessageSending' => [
        'App\Listeners\Notification\SendingEmail'
    ],
    

    唯一的问题是这个侦听器接收到一个Swift邮件事件,而不是我发送的原始邮件,所以我不知道如何取消它。事先有什么想法和感谢吗?

    1 回复  |  直到 8 年前
        1
  •  3
  •   ilyar    7 年前

    类扩展通知

    public function via($notifiable)
    {
        if($this->dontSend($notifiable)) {
            return [];
        }
        return ['mail'];
    }
    
    public function dontSend($notifiable)
    {
        return $this->appointment->status === 'cancelled';
    }
    

    protected $listen = [
        NotificationSending::class => [
            NotificationSendingListener::class,
        ],
    ];
    

    类通知SendingListener

    public function handle(NotificationSending $event)
    {
        if (method_exists($event->notification, 'dontSend')) {
            return !$event->notification->dontSend($event->notifiable);
        }
        return true;
    }
    

    有关更多详细信息,请参阅本文 Handling delayed notifications in Laravel

    推荐文章