DEV Community

Sesha
Sesha

Posted on

Add Snooze functionality to Notification channels.

The simple way to add snooze functionality to your notification channels in your Laravel application.


use Carbon\Carbon;

class SnoozeData
{
    public Carbon $snooze_until;

    public function __construct(
        public int $duration,
        public string $type,
    ) {
        $this->getSnoozeUntilTime();
    }

    private function getSnoozeUntilTime(): void
    {
        $time = Carbon::now();

        $this->snooze_until = match ($this->type) {
            'minutes' => $time->addMinutes($this->duration),
            'hours' => $time->addHours($this->duration),
            'days' => $time->addDays($this->duration),
            'weeks' => $time->addWeeks($this->duration),
            default => $time,
        };
    }
}

Enter fullscreen mode Exit fullscreen mode
$details = new SnoozeData(...Request::validate([
   'duration' => ['required', 'integer', 'between:0,1000'],
   'type' => ['required', 'string'],
]));


$notifyUser->update([
  'snooze_until' => $details->snooze_until,
]);


Enter fullscreen mode Exit fullscreen mode

Top comments (0)