Queued notifications let your app send messages in the background. This keeps your app fast and smooth for users.
Queued notifications in Laravel
Start learning this pattern below
Jump into concepts and practice - no test required
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.
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.'); } }
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 []; } }
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.'); } }
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.
<?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";
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.
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.
Practice
Solution
Step 1: Understand queued notifications purpose
Queued notifications delay sending to avoid blocking app processes.Step 2: Identify the main benefit
This means the app stays fast because sending happens in the background.Final Answer:
They send messages without slowing down your app. -> Option CQuick Check:
Queued notifications improve app speed = B [OK]
- Thinking queued notifications encrypt messages
- Assuming they only send during specific hours
- Believing they retry indefinitely without limits
Solution
Step 1: Recall Laravel notification queue setup
Notifications must implement the ShouldQueue interface to be queued.Step 2: Differentiate interface and trait
Queueable is a trait to help with queue features, not an interface.Final Answer:
ShouldQueue -> Option DQuick Check:
Implement ShouldQueue to queue notifications = A [OK]
- Confusing Queueable trait with interface
- Using non-existent interfaces like ShouldNotify
- Assuming QueueInterface is required
notify() is called?
use Illuminate\Notifications\Notification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
class InvoicePaid extends Notification implements ShouldQueue
{
use Queueable;
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)->line('Invoice paid.');
}
}Solution
Step 1: Check if notification implements ShouldQueue
The class implements ShouldQueue, so it will be queued.Step 2: Confirm use of Queueable trait and mail channel
Queueable trait is used, and via() returns 'mail', so mail will be sent asynchronously.Final Answer:
The notification will be queued and sent asynchronously. -> Option BQuick Check:
ShouldQueue + Queueable = queued notification = D [OK]
- Assuming Queueable trait is optional
- Thinking notification sends immediately despite ShouldQueue
- Confusing mail channel with SMS
use Illuminate\Notifications\Notification;
use Illuminate\Bus\Queueable;
class OrderShipped extends Notification
{
use Queueable;
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)->line('Order shipped.');
}
}Solution
Step 1: Check interface implementation for queuing
The class uses Queueable trait but does not implement ShouldQueue interface.Step 2: Understand queuing requirements
Without ShouldQueue, Laravel sends notification immediately, ignoring queue.Final Answer:
It is missing the ShouldQueue interface implementation. -> Option AQuick Check:
Must implement ShouldQueue to queue notifications = A [OK]
- Thinking Queueable trait alone queues notifications
- Believing via() channel affects queuing
- Renaming toMail() method incorrectly
class PaymentFailed extends Notification implements ShouldQueue
{
use Queueable;
public $tries = 3;
public function via($notifiable) { return ['mail']; }
}
B) class PaymentFailed extends Notification implements ShouldQueue
{
use Queueable;
public function via($notifiable) { return ['mail']; }
public function retryTimes() { return 3; }
}
C) class PaymentFailed extends Notification
{
use Queueable;
public $tries = 3;
public function via($notifiable) { return ['mail']; }
}
D) class PaymentFailed extends Notification implements ShouldQueue
{
use Queueable;
public function via($notifiable) { return ['mail']; }
public $maxRetries = 3;
}Solution
Step 1: Identify queuing and retry setup
Laravel uses public $tries property to set retry attempts on queued jobs/notifications.Step 2: Check interface and property correctness
Correctly sets retry attempts with public $tries property. implements ShouldQueue and sets public $tries = 3 correctly.Final Answer:
Correctly sets retry attempts with public $tries property. -> Option AQuick Check:
Use public $tries for retries in queued notifications = A [OK]
- Using non-existent retryTimes() method
- Forgetting to implement ShouldQueue interface
- Using wrong property name like $maxRetries
