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

Commit e42a8e9

Browse files
author
Henrique Shiraiwa
committed
[Android] Allow to set the notification to executes on idle
On Android 6.0 (API level 23) and above in order to reduce battery consumption, the system enters in Doze mode whilethe device is unused for long periods of time. While on Doze mode the AlarmManager alarms dont execute exactly on the scheduled time, they are deferred to the next maintenance window. This cause the notifications to show after their scheduled time. This change adds a new attribute `allowWhileIdle` to allow the notification to be displayed while on doze
1 parent ae4f9fd commit e42a8e9

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ PushNotification.localNotification({
249249
priority: "high", // (optional) set notification priority, default: high
250250
visibility: "private", // (optional) set notification visibility, default: private
251251
importance: "high", // (optional) set notification importance, default: high
252+
allowWhileIdle: false, // (optional) set notification to work while on doze, default: false
252253

253254
/* iOS only properties */
254255
alertAction: // (optional) default: view
@@ -348,6 +349,17 @@ Available options:
348349

349350
More information: https://developer.android.com/reference/android/app/NotificationManager#IMPORTANCE_DEFAULT
350351

352+
## Notification while idle ##
353+
354+
(optional) Specify `allowWhileIdle` to set if the notification should be allowed to execute even when the system is on low-power idle modes.
355+
356+
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.
357+
358+
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.
359+
360+
More information:
361+
https://developer.android.com/training/monitoring-device-state/doze-standby
362+
351363
#### IOS
352364
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.
353365
```javascript

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public void sendNotificationScheduled(Bundle bundle) {
118118

119119
public void sendNotificationScheduledCore(Bundle bundle) {
120120
long fireDate = (long) bundle.getDouble("fireDate");
121+
boolean allowWhileIdle = bundle.getBoolean("allowWhileIdle");
121122

122123
// If the fireDate is in past, this will fire immediately and show the
123124
// notification to the user
@@ -126,9 +127,13 @@ public void sendNotificationScheduledCore(Bundle bundle) {
126127
Log.d(LOG_TAG, String.format("Setting a notification with id %s at time %s",
127128
bundle.getString("id"), Long.toString(fireDate)));
128129
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
129-
getAlarmManager().setExact(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
130+
if(allowWhileIdle && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
131+
getAlarmManager().setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
132+
} else {
133+
getAlarmManager().setExact(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
134+
}
130135
} else {
131-
getAlarmManager().set(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
136+
getAlarmManager().set(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
132137
}
133138
}
134139

0 commit comments

Comments
 (0)