Appearance
From Birth to Death: A Notification’s (Magnificent) Journey
A notification is a means of communication achieved by different types of channels like email, postcard, sms etc. An entity, usually a model class, which can be notified is called notifiable. Any model class can be notifiable as long as it follows the rules explained here: Working with Notifications
In this document, we are going to investigate the journey of a notification called SignUpCompleted. The notifiable of this notification is a Member model. Let’s begin.
1. A person called Arthur Dent has signed up as a member, now what?
As soon as we have a Member model for Arthur, the code piece below gets executed:
php
/** @var Member $arthurDent */
$arthurDent->notify(new SignUpCompleted());The method notify is a helper method for notifiables. It gets a class extending the Notification class, and sets the notification’s recipient as the notifiable the method notify is called upon. Then, this notification instance having the recipient information, in this case SignUpCompleted, is queued in the notification queue on Redis.
End of Chapter 1: What happened?
Arthur Dent has signed up as a member. A notification called SignUpCompleted is queued on notifications queue to be sent to aurthor. NotificationQueued event is dispatched, and as a result, the notification log is created in pending state.
2. The notification SignUpCompleted is immediately ready to be processed
The horizon worker goes through the notification queue and fetches the job to send the SignUpCompleted notification.
It restores the job from the queue, unserializes the job, checks preconditions (there are none in this case), and sends the actual notification using \Illuminate\Notifications\Channels\MailChannel.
End of Chapter 2: What happened?
A \Illuminate\Notifications\Events\NotificationSent event is dispatched after sending the notification, the pending notification log is updated & marked as sent, the content of the notification is cached (\App\Notifications\Support\Example\Factory\NotificationExampleViewModelFactory).