DEV Community

Sesha
Sesha

Posted on

2 2

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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more