0
0
Laravelframework~5 mins

Queued notifications in Laravel

Choose your learning style9 modes available
Introduction

Queued notifications let your app send messages in the background. This keeps your app fast and smooth for users.

When sending emails or SMS that might take time to deliver
When you want to avoid slowing down user actions by waiting for notifications
When sending many notifications at once to avoid server overload
When you want to retry sending notifications if they fail
When you want to schedule notifications to be sent later
Syntax
Laravel
use Illuminate\Notifications\Notification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class InvoicePaid extends Notification implements ShouldQueue
{
    use Queueable;

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('Your invoice has been paid!')
                    ->action('View Invoice', url('/invoices'))
                    ->line('Thank you for using our app!');
    }
}

Implementing ShouldQueue tells Laravel to queue the notification.

Use the Queueable trait to get helpful queue features like delay and retry.

Examples
A simple queued notification that sends a welcome email.
Laravel
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;

class WelcomeUser extends Notification implements ShouldQueue
{
    use Queueable;

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('Welcome to our app!')
                    ->line('We are happy to have you.');
    }
}
Edge case: Notification with no delivery channels. It will not send anything but still queues.
Laravel
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Bus\Queueable;

class EmptyNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function via($notifiable)
    {
        return [];
    }
}
Queued notification sending SMS via Nexmo channel.
Laravel
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\NexmoMessage;

class SmsNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function via($notifiable)
    {
        return ['nexmo'];
    }

    public function toNexmo($notifiable)
    {
        return (new NexmoMessage)
                    ->content('Your SMS notification.');
    }
}
Sample Program

This example shows a complete queued notification class OrderShipped. It sends an email with order details. The notification is queued because it implements ShouldQueue.

In the usage example, a user is found and notified. The notification is added to the queue, so the app does not wait for the email to send.

Laravel
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Contracts\Queue\ShouldQueue;

class OrderShipped extends Notification implements ShouldQueue
{
    use Queueable;

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->subject('Your Order Has Shipped')
                    ->line('Good news! Your order has been shipped.')
                    ->action('Track Order', url('/orders/track'))
                    ->line('Thank you for shopping with us!');
    }
}

// Usage example in a controller or command:

use App\Notifications\OrderShipped;
use App\Models\User;

$user = User::find(1); // Find user to notify

// Before sending notification
echo "Sending notification to user: {$user->email}\n";

$user->notify(new OrderShipped());

// The notification is queued and sent in background

echo "Notification queued successfully.\n";
OutputSuccess
Important Notes

Queued notifications run in the background, so your app stays fast.

Make sure to run a queue worker (like php artisan queue:work) to process queued notifications.

Time complexity depends on queue system but sending notifications asynchronously is faster for user requests.

Summary

Queued notifications send messages without slowing down your app.

Implement ShouldQueue and use Queueable trait to queue notifications.

Run a queue worker to process and send queued notifications.