To customize the notification content for failed jobs in Laravel, you can create a custom notification class that implements the `ShouldQueue` interface and the `Queueable` trait. This will allow the notification to be queued and delivered asynchronously.
Here is an example of how you can create a custom notification class:
php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class JobFailedNotification extends Notification implements ShouldQueue
{
use Queueable;
protected $exception;
public function __construct($exception)
{
$this->exception = $exception;
}
public function via($notifiable)
{
return ['mail', 'slack'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Job Failed')
->line('A job has failed with the following exception:')
->line($this->exception->getMessage());
}
public function toSlack($notifiable)
{
return (new \Illuminate\Notifications\Messages\SlackMessage)
->error()
->content('A job has failed with the following exception:')
->attachment(function ($attachment) {
$attachment->title('Exception Message')
->content($this->exception->getMessage());
});
}
}
In this example, the `JobFailedNotification` class is responsible for sending the notification via both email and Slack. The `via()` method specifies the notification channels, and the `toMail()` and `toSlack()` methods define the content of the notifications.
To dispatch this notification when a job fails, you can override the `failed()` method in your job class:
php
namespace App\Jobs;
use App\Notifications\JobFailedNotification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class MyJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function handle()
{
// Your job logic here
}
public function failed(Exception $exception)
{
Notification::route('mail', '
<script language='JavaScript' type='text/javascript'>
<!--
var prefix = 'mailto:';
var suffix = '';
var attribs = '';
var path = 'hr' + 'ef' + '=';
var addy91590 = 'admin' + '@';
addy91590 = addy91590 + 'example' + '.' + 'com';
document.write( '<a ' + path + '\'' + prefix + addy91590 + suffix + '\'' + attribs + '>' );
document.write( addy91590 );
document.write( '<\/a>' );
//-->
</script><script language='JavaScript' type='text/javascript'>
<!--
document.write( '<span style=\'display: none;\'>' );
//-->
</script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
<script language='JavaScript' type='text/javascript'>
<!--
document.write( '</' );
document.write( 'span>' );
//-->
</script>')
->route('slack', 'https://hooks.slack.com/services/...')
->notify(new JobFailedNotification($exception));
}
}
In this example, the `failed()` method in the `MyJob` class dispatches the `JobFailedNotification` notification to the configured notifiable routes (email and Slack webhook URL).
This approach allows you to customize the notification content and delivery channels for failed jobs, providing more detailed and actionable information to the relevant parties[1][2][4].
Citations:[1] https://vox.veritas.com/t5/Backup-Exec/Email-notification-for-only-failed-job/td-p/513239
[2] https://kb.sos-berlin.com/display/JS7/JS7%2B-%2BHow%2Bto%2Bset%2Bup%2Be-mail%2Bnotification%2Bfor%2Bfailed%2Bjobs
[3] https://frappeframework.com/docs/user/en/guides/deployment/email-notifications-for-failed-background-jobs
[4] https://github.com/talvbansal/laravel-throttled-failed-jobs
[5] https://stackoverflow.com/questions/58105705/how-to-handle-failed-jobs-on-queueable-notifications