Skip to content

Next Schedule #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,21 @@ public function shouldInterrupt($notifiable) {

If this method is not present on your notification, the notification will *not* be interrupted. Consider creating a shouldInterupt trait if you'd like to repeat conditional logic on groups of notifications.


**Setting Next Schedule**

If you want to send a reoccuring notification, you can add the `nextSchedule()` method to any notification. This method will be used to tell snooze if and when to send another notifiaction.

For example, you might want to send a reminder every 7 days

```php
public function nextSchedule($notifiable) {
return now()->addDays();
}
```

If this method is present on your notification, The time it returns will be used to schedule a notification again for that particular notification.

**Scheduled Notification Meta Information**

It's possible to store meta information on a scheduled notification, and then query the scheduled notifications by this meta information at a later stage.
Expand Down
26 changes: 26 additions & 0 deletions src/Models/ScheduledNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ public function send(): void
$this->save();

event(new NotificationSent($this));

if ($nextSchedule = $this->nextSchedule($notification, $notifiable)) {
$this->scheduleAgainAt($nextSchedule);
}
}

/**
* @param object|null $notification
* @param object|null $notifiable
* @return \DateTimeInterface|string|null
*/
public function nextSchedule($notification, $notifiable)
{
if (!$notification) {
$notification = $this->serializer->unserialize($this->notification);
}

if (!$notifiable) {
$notifiable = $this->serializer->unserialize($this->target);
}

if (method_exists($notification, 'nextSchedule')) {
return $notification->nextSchedule($notifiable);
}

return null;
}

/**
Expand Down