Automate Future Notifications and Reminders with Laravel Snooze

News

November 22nd, 2019

laravel-snooze.png

Laravel Snooze is a package by Thomas Kane, which simplifies automating future notifications and reminders in Laravel.

The package’s readme has some typical use-cases for this package:

  • Reminder system (1 week before appt, 1 day before, 1 hour before, etc)
  • Follow-up surveys (2 days after purchase)
  • On-boarding Email Drips (Welcome email after sign-up, additional tips after 3 days, upsell offer after 7 days)
  • Short-Term Recurring reports (send every week for the next 4 weeks)

Snooze provides a trait for your models called SnoozeNotifiable that adds a notifyAt() method:

1use Thomasjohnkane\Snooze\Traits\SnoozeNotifiable;
2 
3class User extends Model {
4 use SnoozeNotifiable;
5 
6 // …
7}
8 
9// Schedule a birthday notification
10$user->notifyAt(new BirthdayNotification, Carbon::parse($user->birthday));
11 
12// Schedule for a week from now
13$user->notifyAt(new NextWeekNotification, Carbon::now()->addDays(7));
14 
15// Schedule for new years eve
16$user->notifyAt(new NewYearNotification, Carbon::parse('last day of this year'));

You can even send anonymous notifications using the ScheduledNotification::create() method:

1$target = (new AnonymousNotifiable)
2 ->route('mail', 'hello@example.com')
3 ->route('sms', '56546456566');
4 
5ScheduledNotification::create(
6 $target, // Target
7 new ScheduledNotificationExample($order), // Notification
8 Carbon::now()->addDay() // Send At
9);

If you have some logic that should stop the notification from sending, you can define logic in the notification class:

1public function shouldinterrupt($notifiable) {
2 return $this->invoice->is_paid;
3}

For more detailed info, check out Thomas Kane’s detailed article Scheduling Laravel notifications using Snooze.

The options and code API for this package provide a delightful experience for sending notifications in the future, so be sure to read through all the documentation.

You can learn more about this package, get full installation instructions, and view the source code on GitHub at thomasjohnkane/snooze.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.