Skip to content

Commit

Permalink
PR zo0r#959: Merge remote-tracking branch 'hshiraiwa/feat/allow-while…
Browse files Browse the repository at this point in the history
…-idle'
  • Loading branch information
Mike McCartney committed Feb 15, 2019
2 parents f7e3f5e + 6cf060a commit c329124
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 c329124

Please sign in to comment.