From cf15cb8e498f49e027fde3a0da4d3cac1f1bc386 Mon Sep 17 00:00:00 2001 From: Joseph Pender Date: Tue, 2 Feb 2021 14:48:09 -0600 Subject: [PATCH 1/3] Testing setExactAndAllowWhileIdle --- .../localnotifications/LocalNotificationManager.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java b/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java index ca0e36b0a..8201736e8 100644 --- a/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java +++ b/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java @@ -328,7 +328,11 @@ private void triggerScheduledNotification(Notification notification, LocalNotifi long interval = at.getTime() - new Date().getTime(); alarmManager.setRepeating(AlarmManager.RTC, at.getTime(), interval, pendingIntent); } else { - alarmManager.setExact(AlarmManager.RTC, at.getTime(), pendingIntent); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC, at.getTime(), pendingIntent); + } else { + alarmManager.setExact(AlarmManager.RTC, at.getTime(), pendingIntent); + } } return; } From fd239a8cd1478c6753621bfe26f6293cf83ce1d9 Mon Sep 17 00:00:00 2001 From: Joseph Pender Date: Wed, 3 Feb 2021 10:49:08 -0600 Subject: [PATCH 2/3] Adding `allowWhileIdle` boolean to notification Schedule interface --- local-notifications/README.md | 15 ++++++++------- local-notifications/src/definitions.ts | 11 +++++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/local-notifications/README.md b/local-notifications/README.md index caeb60db2..2fbdc5bbd 100644 --- a/local-notifications/README.md +++ b/local-notifications/README.md @@ -312,13 +312,14 @@ Represents a schedule for a notification. Use either `at`, `on`, or `every` to schedule notifications. -| Prop | Type | Description | Since | -| ------------- | ------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- | -| **`at`** | Date | Schedule a notification at a specific date and time. | 1.0.0 | -| **`repeats`** | boolean | Repeat delivery of this notification at the date and time specified by `at`. Only available for iOS and Android. | 1.0.0 | -| **`on`** | ScheduleOn | Schedule a notification on particular interval(s). This is similar to scheduling [cron](https://en.wikipedia.org/wiki/Cron) jobs. Only available for iOS and Android. | 1.0.0 | -| **`every`** | ScheduleEvery | Schedule a notification on a particular interval. | 1.0.0 | -| **`count`** | number | Limit the number times a notification is delivered by the interval specified by `every`. | 1.0.0 | +| Prop | Type | Description | Since | +| -------------------- | ------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- | +| **`at`** | Date | Schedule a notification at a specific date and time. | 1.0.0 | +| **`repeats`** | boolean | Repeat delivery of this notification at the date and time specified by `at`. Only available for iOS and Android. | 1.0.0 | +| **`allowWhileIdle`** | boolean | Allow this notification to fire while in [Doze](https://developer.android.com/training/monitoring-device-state/doze-standby) Only available for Android 23+. Note that these notifications can only fire [once per 9 minutes, per app](https://developer.android.com/training/monitoring-device-state/doze-standby#assessing_your_app). | 1.0.0 | +| **`on`** | ScheduleOn | Schedule a notification on particular interval(s). This is similar to scheduling [cron](https://en.wikipedia.org/wiki/Cron) jobs. Only available for iOS and Android. | 1.0.0 | +| **`every`** | ScheduleEvery | Schedule a notification on a particular interval. | 1.0.0 | +| **`count`** | number | Limit the number times a notification is delivered by the interval specified by `every`. | 1.0.0 | #### Date diff --git a/local-notifications/src/definitions.ts b/local-notifications/src/definitions.ts index 35e6075d9..e3e94fcf3 100644 --- a/local-notifications/src/definitions.ts +++ b/local-notifications/src/definitions.ts @@ -652,6 +652,17 @@ export interface Schedule { */ repeats?: boolean; + /** + * Allow this notification to fire while in [Doze](https://developer.android.com/training/monitoring-device-state/doze-standby) + * + * Only available for Android 23+. + * + * Note that these notifications can only fire [once per 9 minutes, per app](https://developer.android.com/training/monitoring-device-state/doze-standby#assessing_your_app). + * + * @since 1.0.0 + */ + allowWhileIdle?: boolean; + /** * Schedule a notification on particular interval(s). * From d6233d566f52274392a81ecc87431911ee9af04f Mon Sep 17 00:00:00 2001 From: Joseph Pender Date: Wed, 3 Feb 2021 10:56:33 -0600 Subject: [PATCH 3/3] [android] set notification alarms with `setExactAndAllowWhileIdle` if `allowWhileIdle` is set --- .../localnotifications/LocalNotificationManager.java | 9 +++++++-- .../localnotifications/LocalNotificationSchedule.java | 9 +++++++++ local-notifications/src/definitions.ts | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java b/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java index 8201736e8..a15ee065e 100644 --- a/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java +++ b/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java @@ -328,7 +328,7 @@ private void triggerScheduledNotification(Notification notification, LocalNotifi long interval = at.getTime() - new Date().getTime(); alarmManager.setRepeating(AlarmManager.RTC, at.getTime(), interval, pendingIntent); } else { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && schedule.allowWhileIdle()) { alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC, at.getTime(), pendingIntent); } else { alarmManager.setExact(AlarmManager.RTC, at.getTime(), pendingIntent); @@ -354,7 +354,12 @@ private void triggerScheduledNotification(Notification notification, LocalNotifi long trigger = on.nextTrigger(new Date()); notificationIntent.putExtra(TimedNotificationPublisher.CRON_KEY, on.toMatchString()); pendingIntent = PendingIntent.getBroadcast(context, request.getId(), notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); - alarmManager.setExact(AlarmManager.RTC, trigger, pendingIntent); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && schedule.allowWhileIdle()) { + alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC, trigger, pendingIntent); + } else { + alarmManager.setExact(AlarmManager.RTC, trigger, pendingIntent); + } + SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Logger.debug(Logger.tags("LN"), "notification " + request.getId() + " will next fire at " + sdf.format(new Date(trigger))); } diff --git a/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationSchedule.java b/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationSchedule.java index 0843db7cd..ade6c554e 100644 --- a/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationSchedule.java +++ b/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationSchedule.java @@ -18,6 +18,8 @@ public class LocalNotificationSchedule { private DateMatch on; + private Boolean whileIdle; + public LocalNotificationSchedule(JSObject jsonNotification) throws ParseException { JSObject schedule = jsonNotification.getJSObject("schedule"); if (schedule != null) { @@ -29,6 +31,9 @@ public LocalNotificationSchedule(JSObject jsonNotification) throws ParseExceptio buildAtElement(schedule); // Build on - recurring times. For e.g. every 1st day of the month at 8:30. buildOnElement(schedule); + + // Schedule this notification to fire even if app is idled (Doze) + this.whileIdle = schedule.getBoolean("allowWhileIdle", false); } } @@ -105,6 +110,10 @@ public void setCount(int count) { this.count = count; } + public boolean allowWhileIdle() { + return this.whileIdle; + } + public boolean isRepeating() { return Boolean.TRUE.equals(this.repeats); } diff --git a/local-notifications/src/definitions.ts b/local-notifications/src/definitions.ts index e3e94fcf3..773b58703 100644 --- a/local-notifications/src/definitions.ts +++ b/local-notifications/src/definitions.ts @@ -656,7 +656,7 @@ export interface Schedule { * Allow this notification to fire while in [Doze](https://developer.android.com/training/monitoring-device-state/doze-standby) * * Only available for Android 23+. - * + * * Note that these notifications can only fire [once per 9 minutes, per app](https://developer.android.com/training/monitoring-device-state/doze-standby#assessing_your_app). * * @since 1.0.0