Laravel Snooze Documentation

repository·master·Indexed 21 days ago

https://github.com/thomasjohnkane/snooze

A Laravel package for scheduling future notifications and reminders, such as email drips, birthday reminders, and follow-up surveys. It provides the SnoozeNotifiable trait for Eloquent models, a ScheduledNotification helper, and the snooze:send command to dispatch notifications via the Laravel task scheduler. Features include notification cancellation, rescheduling, metadata storage, send tolerance, and automatic pruning of sent or cancelled messages.

Tokens
7.2K
Snippets
29
Records
36
Agent score
75%

What's inside Laravel Snooze

  1. How the snooze:send command works

    master

    Creating a scheduled notification only adds it to the database. To actually send them, you must run the snooze:send command.

    By default, the package attempts to schedule this command to run every minute. Ensure your Laravel task scheduler (schedule:run) is running.

    Configuration Options:

    • scheduleCommands: Set to false in snooze.php if you want to manually manage the command scheduling in your Console/Kernel.php.
    • sendFrequency: Defines how often the command runs. Options include everyMinute, everyFiveMinutes, everyTenMinutes, everyFifteenMinutes, everyThirtyMinutes, hourly, and daily.
  2. Publish the Snooze configuration file

    master

    To customize settings like send frequency, tolerance, or pruning, publish the configuration file using the following command.

    php artisan vendor:publish --provider="Thomasjohnkane\Snooze\ServiceProvider" --tag="config"
  3. Implement basic delayed notifications

    master

    To schedule a notification to be sent at a specific future date, follow these steps:

    1. Prepare the Model: Add the SnoozeNotifiable trait to your model (e.g., the User model) to enable snooze capabilities.
    2. Create the Notification: Generate a standard Laravel notification class using php artisan make:notification <NotificationName>.
    3. Calculate the Delay: Use Carbon\Carbon to determine the future timestamp when the notification should be sent.
    4. Schedule the Notification: Call the notifyAt method on your notifiable model instance, passing the notification object and the calculated timestamp.

    The notification is stored in the scheduled_notifications table and will be dispatched the next time the php artisan snooze:send command is executed after the scheduled time.

    use Carbon\Carbon;
    
    // 1. Calculate the date (e.g., 7 days from now)
    $sendAt = Carbon::now()->addDays(7);
    
    // 2. Schedule the notification on the user
    Auth::user()->notifyAt(new OneWeekAfterNotice(), $sendAt);
  4. Register the ScheduledNotification Facade

    master

    To use the ScheduledNotification facade for a cleaner API, manually register it in the aliases section of config/app.php. Note that this step is optional if you are using Laravel 5.5+ with auto-discovery enabled.

    // config/app.php
    'aliases' => [
        // ... other aliases
        'ScheduledNotification' => Thomasjohnkane\ScheduledNotifications\Facades\ScheduledNotification::class,
    ],
  5. Register the Snooze Service Provider

    master

    If you are using a version of Laravel older than 5.5 (which does not support package auto-discovery), you must manually register the service provider in your application's configuration. Add Thomasjohnkane\ScheduledNotifications\ServiceProvider::class to the providers array in config/app.php.

    // config/app.php
    'providers' => [
        // ... other providers
        Thomasjohnkane\ScheduledNotifications\ServiceProvider::class,
    ],
  6. Implement interruption and rescheduling logic in notifications

    master

    The ScheduledNotification model checks for two specific methods on your notification class to determine its lifecycle during the send() process:

    1. Interruption (shouldInterrupt)

    If your notification class implements a shouldInterrupt(?object $notifiable): bool method, the scheduler will call it. If it returns true, the scheduled notification is automatically cancelled.

    2. Rescheduling (shouldRescheduleFor)

    If your notification class implements a shouldRescheduleFor(?object $notifiable): ext{DateTimeInterface}| ext{string}| ext{null} method, the scheduler will call it. If it returns a date/time, the notification is automatically rescheduled to that time instead of being sent immediately.

    class MyNotification extends Notification
    {
        public function shouldInterrupt($notifiable): bool
        {
            // Return true to cancel the notification
            return $notifiable->is_do_not_disturb;
        }
    
        public function shouldRescheduleFor($notifiable)
        {
            // Return a new time to delay the notification
            return now()->addHours(2);
        }
    }
  7. Disable the scheduler or use onOneServer

    master

    Control the execution environment via environment variables:

    • SCHEDULED_NOTIFICATIONS_DISABLED=true: Disables sending. Notifications will still be scheduled and stored in the database, but will not be sent until this is set to false. Useful for multi-server setups where only one specific server should handle sending.
    • SCHEDULED_NOTIFICATIONS_ONE_SERVER=true: Enables the use of Laravel's onOneServer() functionality for the snooze commands.
  8. Configure send tolerance and pruning

    master

    Use these settings to manage the volume and cleanup of scheduled notifications.

    • Send Tolerance: Prevents a backlog of old notifications from being sent all at once if the scheduler stops. Only notifications scheduled within this window will be sent.

      • Config: snooze.php (send_tolerance) or SCHEDULED_NOTIFICATION_SEND_TOLERANCE (seconds).
      • Default: 24 hours.
    • Pruning: Automatically deletes sent or cancelled messages older than a certain age.

      • Config: snooze.php (prune_age) or SCHEDULED_NOTIFICATION_PRUNE_AGE (days).
      • Default: Disabled.