NativeScript / plugins

@nativescript plugins to help with your developments.
https://docs.nativescript.org/plugins/index.html
Apache License 2.0
187 stars 104 forks source link

feat(local-notifications): Improved interval scheduling #590

Closed insytes closed 2 weeks ago

insytes commented 3 weeks ago

Closes #73

What's new?

Allows notifications to be repeated with more precise intervals.

ScheduleOptions.interval can now be defined as either a ScheduleInterval or an object describing an interval { [ScheduleInterval]: number }

Example: interval: 'day' *Existing functionality interval: { 'second' 90 } // on iOS a minimum of 60 seconds is still required. interval: { 'minute': 15 } interval: { 'hour: 6 }

A new boolean option is added ScheduleOptions.displayImmediately

This can only be used when an interval is provided. It cannot be used when a trigger is defined as an exact date using at.

When ScheduleOptions.displayImmediately is true, a separate notification will be scheduled that will be displayed ASAP.

Known limitations

The limitations on the new iOS Notification API (iOS 10+) means that notifications cannot be scheduled at an exact repeating interval after a precise date is defined.

In other words, we cannot define a notification like this:

// Can we trigger a notification at this exact time and repeat every 15 minutes? No.
LocalNotifications.schedule([{
  // ...
  at: new Date(new Date().getTime() + 10 * 1000), // 10 seconds from now
  interval: {
    'minute': 15
  },
}]);

The only option when using at is the existing behaviour of the plugin, a calendar unit.

// The only option is to provide a calendar unit for repeating this notification.
LocalNotifications.schedule([{
  // ...
  at: new Date(new Date().getTime() + 10 * 1000), // 10 seconds from now
  interval: 'hour'
  // display this notification at this time and repeat on the hour.
}]);

Using the new stuff

With the newly available options we can schedule a notification to repeat on a calendar unit, multiplied by a given amount.

For example:

// Display notification & repeat every 15 minutes
LocalNotifications.schedule([{
  // ...
  interval: {
    'minute': 15
  },
  displayImmediately: true,
}]);
// Display notification in 90 minutes and repeat every 90 minutes
LocalNotifications.schedule([{
  // ...
  interval: {
    'minutes': 90
  },
}]);

Using the existing options as before:

// Display notification at specific date and repeat in 1 hour
LocalNotifications.schedule([{
  // ...
  interval: 'hour',
  at: new Date(new Date().getTime() + 10 * 1000), // 10 seconds from now
}]);

Proceed?

Some more eyes on manual testing would be great.

Given the limitations on iOS I feel this change provides the best quick wins for slightly more control over repeating intervals.

Any thoughts?

cla-bot[bot] commented 3 weeks ago

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign the CLA at https://www.nativescript.org/cla. CLA has not been signed by users: @insytes. After signing the CLA, you can ask me to recheck this PR by posting @cla-bot check as a comment to the PR.

cla-bot[bot] commented 3 weeks ago

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign the CLA at https://www.nativescript.org/cla. CLA has not been signed by users: @insytes. After signing the CLA, you can ask me to recheck this PR by posting @cla-bot check as a comment to the PR.

insytes commented 3 weeks ago

@cla-bot check

cla-bot[bot] commented 3 weeks ago

The cla-bot has been summoned, and re-checked this pull request!

NathanWalker commented 3 weeks ago

Excellent work @insytes 😍

NathanWalker commented 3 weeks ago

Adding the description notes into the README to document the new ability would be good. It can also make note of the iOS minimum on seconds, we can then publish this with a minor bump.

insytes commented 2 weeks ago

@NathanWalker README updated, I added the iOS note to the ScheduleOptions table.

NathanWalker commented 2 weeks ago

Released with 6.2.0