From 391f1e225643c099af461c9d4045c6a08ebfa8bd Mon Sep 17 00:00:00 2001 From: Adinnu Benedict Date: Thu, 13 Jun 2024 02:55:29 +0300 Subject: [PATCH 1/3] Next Schedule --- README.md | 15 +++++++++++++++ src/Models/ScheduledNotification.php | 26 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/README.md b/README.md index 75d3008..d12b152 100644 --- a/README.md +++ b/README.md @@ -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 Schedult** + +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 not 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. diff --git a/src/Models/ScheduledNotification.php b/src/Models/ScheduledNotification.php index 67306e7..4dd4ba0 100644 --- a/src/Models/ScheduledNotification.php +++ b/src/Models/ScheduledNotification.php @@ -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; } /** From 9a61e4101624454089bf1029d65bb34af3a3c810 Mon Sep 17 00:00:00 2001 From: Adinnu Benedict Date: Thu, 13 Jun 2024 02:59:50 +0300 Subject: [PATCH 2/3] Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d12b152..644fed4 100644 --- a/README.md +++ b/README.md @@ -169,7 +169,7 @@ 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 Schedult** +**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. From 925ac0c94962749dc6621dbc263caba3f8b20250 Mon Sep 17 00:00:00 2001 From: Adinnu Benedict Date: Sun, 30 Jun 2024 08:34:22 +0300 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 644fed4..1868efc 100644 --- a/README.md +++ b/README.md @@ -181,7 +181,7 @@ public function nextSchedule($notifiable) { } ``` -If this method is not present on your notification, The time it returns will be used to schedule a notification again for that particular notification. +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**