Skip to content

Commit

Permalink
[Android] Allow to set the notification to executes on idle
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Henrique Shiraiwa committed Dec 7, 2018
1 parent ae4f9fd commit 6cf060a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ PushNotification.localNotification({
priority: "high", // (optional) set notification priority, default: high
visibility: "private", // (optional) set notification visibility, default: private
importance: "high", // (optional) set notification importance, default: high
allowWhileIdle: false, // (optional) set notification to work while on doze, default: false

/* iOS only properties */
alertAction: // (optional) default: view
Expand Down Expand Up @@ -348,6 +349,17 @@ Available options:

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

## Notification while idle ##

(optional) Specify `allowWhileIdle` to set if the notification should be allowed to execute even when the system is on low-power idle modes.

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.

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.

More information:
https://developer.android.com/training/monitoring-device-state/doze-standby

#### IOS
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.
```javascript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public void sendNotificationScheduled(Bundle bundle) {

public void sendNotificationScheduledCore(Bundle bundle) {
long fireDate = (long) bundle.getDouble("fireDate");
boolean allowWhileIdle = bundle.getBoolean("allowWhileIdle");

// If the fireDate is in past, this will fire immediately and show the
// notification to the user
Expand All @@ -126,7 +127,11 @@ public void sendNotificationScheduledCore(Bundle bundle) {
Log.d(LOG_TAG, String.format("Setting a notification with id %s at time %s",
bundle.getString("id"), Long.toString(fireDate)));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getAlarmManager().setExact(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
if(allowWhileIdle && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getAlarmManager().setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
} else {
getAlarmManager().setExact(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
}
} else {
getAlarmManager().set(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
}
Expand Down

0 comments on commit 6cf060a

Please sign in to comment.