Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 9e563df

Browse files
authored
Merge pull request #959 from hshiraiwa/feat/allow-while-idle
[Android] Allow to set the notification to executes on idle
2 parents 79eb61c + 6cf060a commit 9e563df

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ PushNotification.localNotification({
272272
priority: "high", // (optional) set notification priority, default: high
273273
visibility: "private", // (optional) set notification visibility, default: private
274274
importance: "high", // (optional) set notification importance, default: high
275+
allowWhileIdle: false, // (optional) set notification to work while on doze, default: false
275276

276277
/* iOS only properties */
277278
alertAction: 'view', // (optional) default: view
@@ -373,6 +374,17 @@ Available options:
373374

374375
More information: https://developer.android.com/reference/android/app/NotificationManager#IMPORTANCE_DEFAULT
375376

377+
## Notification while idle ##
378+
379+
(optional) Specify `allowWhileIdle` to set if the notification should be allowed to execute even when the system is on low-power idle modes.
380+
381+
On Android 6.0 (API level 23) and forward, the Doze was introduced to reduce battery consumption when the device is unused for long periods of time. But while on Doze the AlarmManager alarms (used to show scheduled notifications) are deferred to the next maintenance window. This may cause the notification to be delayed while on Doze.
382+
383+
This can significantly impact the power use of the device when idle. So it must only be used when the notification is required to go off on a exact time, for example on a calendar notification.
384+
385+
More information:
386+
https://developer.android.com/training/monitoring-device-state/doze-standby
387+
376388
#### IOS
377389

378390
The `userInfo` parameter for `PushNotification.localNotification` is required for this operation and must contain an `id` parameter. The id supplied will then be used for the cancel operation.

android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public void sendNotificationScheduled(Bundle bundle) {
121121

122122
public void sendNotificationScheduledCore(Bundle bundle) {
123123
long fireDate = (long) bundle.getDouble("fireDate");
124+
boolean allowWhileIdle = bundle.getBoolean("allowWhileIdle");
124125

125126
// If the fireDate is in past, this will fire immediately and show the
126127
// notification to the user
@@ -129,7 +130,11 @@ public void sendNotificationScheduledCore(Bundle bundle) {
129130
Log.d(LOG_TAG, String.format("Setting a notification with id %s at time %s",
130131
bundle.getString("id"), Long.toString(fireDate)));
131132
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
132-
getAlarmManager().setExact(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
133+
if(allowWhileIdle && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
134+
getAlarmManager().setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
135+
} else {
136+
getAlarmManager().setExact(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
137+
}
133138
} else {
134139
getAlarmManager().set(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
135140
}

0 commit comments

Comments
 (0)