Let's say you want to inform your user about upcoming event, or remind him one week before his account is expired, or remind him that his invoice should be paid in 5 days. How to do it in Laravel?

 

Basically you could achieve this by crating a cron job which should every 5 min, 1 hour or one day... or any interval what fits to your project and on every request check what notifications should be sent.

 

Another way is to use package called snooze which can do a similar thang but on a different way.

This package will create DB table for upcoming notifications with time and day when should be sent. It will be triggered by cron, in interval as you define and it will send notifications at defined time.

 

This approach gives you additional flexibility to send notifications at defined time. For example, if the user is active in the morning - you can create his notifications to be sent at that time.

 

How to create notification

Let's say we have user appointment and we want to send notification 15 min before the meeting:

                    ScheduledNotification::create(
            $appointment->user, // whom to sent
            new MeetingReminder( // custom notification
                $message
            ),
            Carbon::parse($appointment->appointment_time)->subMinute(15) // Send 15 min before appointment starts
        );
                  

 

This will create a record in DB and it will be triggered via cron job:

                    php artisan schedule:run
                  

 

Send tolerance (from documentation)

If your scheduler stops working, a backlog of scheduled notifications will build up. To prevent users receiving all of the old scheduled notifications at once, the command will only send mail within the configured tolerance. By default this is set to 24 hours, so only mail scheduled to be sent within that window will be sent. This can be configured (in seconds) using the SCHEDULED_NOTIFICATION_SEND_TOLERANCE environment variable or in the snooze.php config file

 

For more details please check Snooze package on this link: