Laravel 5.8:管理来自队列的失败邮件通知

问题描述

我对管理队列中的邮件通知失败有点困惑。

我创建了一个邮件通知类,用于向多个用户发送相同的通知。 该过程运行良好,但我正在尝试为失败的通知设置管理(例如向管理员用户发送邮件以提醒他们通知失败)。

这里是邮件通知类:

class MyCustomMailNotification extends Notification implements
ShouldQueue {
use Queueable;

/**
 * The number of times the job may be attempted.
 *
 * @var int
 */
public $tries = 3;

/**
 * The number of seconds the job can run before timing out.
 *
 * @var int
 */
//public $timeout = 90;

/**
 * Create a new notification instance.
 *
 * @return void
 */
public function __construct()
{
    //
}

/**
 * Get the notification's delivery channels.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return ['mail'];
}

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    return (new MailMessage)
        ->subject('My Subject')
        ->greeting('My greeting')
        ->line('My mail body')
        ->salutation('My salutations');
}

/**
 * Get the array representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function toArray($notifiable)
{
    return [
        //
    ];
}

public function Failed(Exception $e)
{
    dd('Entered Failed from MyCustomMailNotification : ' . $e));
}
 }

我已经设置了一个侦听器“LogNotification”来到达通知事件的句柄,并带有生成失败的特定指令:

事件服务提供者:

 /**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    'Illuminate\Notifications\Events\NotificationSent' => [
        'App\Listeners\LogNotification',],];

听众:

namespace App\Listeners;

use Illuminate\Notifications\Events\NotificationSent; use
Illuminate\Queue\InteractsWithQueue; use
Illuminate\Contracts\Queue\ShouldQueue;

class LogNotification {
/**
 * Create the event listener.
 *
 * @return void
 */
public function __construct()
{
    //
}

/**
 * Handle the event.
 *
 * @param  NotificationSent  $event
 * @return void
 */
public function handle(NotificationSent $event)
{
    $result = 1/0;
}

}

邮件在这样的控制器中进行的:

    $when = Carbon::Now()->addSeconds(5);
    foreach ($users as $user) {
        $user->notify((new MyCustomMailNotification())->delay($when));
    }

在失败的函数中,我没有得到失败通知的任何信息,我的问题是: 如何将失败与失败的通知关联起来?

目标是能够获取因此未收到通知电子邮件用户的信息。

感谢您的任何帮助、想法或解决方案!

解决方法

好吧,那太简单了,我没看到...

由于失败的函数在通知类中,我可以简单地与通知相关联:

$this->id

然后当然会从模型/表中获取有关通知的所有信息,例如用户 ID (notifiable_id) 和具有自定义信息的数据字段。