Posted: 31-12-2020

Add delay at On-Demand Notifications in Laravel 8

Add delay at On-Demand Notifications in Laravel 8

Laravel provides support for sending notifications across a variety of delivery channels, including email, SMS,Slack...

Typically, notifications should be short, informational messages that notify users of something that occurred in your application.

But sometimes you may need to send an On-Demand Notifications to someone who is not stored as a "user" of your application for example the "user" that as just subscribe to the app newsletters.

In this case you can use the Notification facade's route method to send to the "user" the link to confirm his subscription

Notification::route('mail',"email@example.com")
->notify(new NewsletterSubscribeUserNotification($newsletter));


If for some reason you need to delay sending your notification you call the delay method inside the constructor of your notification class.


class NewsletterSubscribeUserNotification extends Notification implements ShouldQueue
{
use Queueable;

public $data;

/**
* Create a new notification instance.
*
* @param Newsletter $data
*/
public function __construct(Newsletter $data)
{

$this->data = $data;
$this->delay(now()->addMinute(1));
}

........
}