Android P Beta - AlarmManager Notifications not wo

2020-06-16 04:39发布

I'm testing my application on Android P beta release 4. My app's targetSdkVersion is 27

It has been observed that alarm manager notifications are not working as expected. I'm using below code to set the notifications -

           if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, AlarmIntentBuilder.buildPendingIntent(context, uri));
            } else if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerAtMillis, AlarmIntentBuilder.buildPendingIntent(context, uri));
            } else {
                alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAtMillis, AlarmIntentBuilder.buildPendingIntent(context, uri));
            }

I tested the same logic on Android 8.0 but it's working fine. In Android 9.0, notifications are working but sometimes they did not work at all. Also, if they work they are not exact and takes too much time and this happens even if the application is in foreground.

The logic is, I've the repeating reminders which are set on specific time and those should repeat them-self on daily basis at the specified time. Also these are high priority reminders and should land at exact time so I'm using setExact and once the notification is received it's being display and new alarm for the next week of the same day is set.

I've checked the Android P API documents but could not find any link which has the impact on working of AlarmManager and Notifications. Only thing which I feel is causing the issue is Power management in Android P and the priority buckets. However notifications are not working properly even if application is in foreground.

Anything I'm missing here. Any help is much appreciated.

2条回答
\"骚年 ilove
2楼-- · 2020-06-16 05:07

As you yourself mentioned the new App Standby Buckets feature of Power Management is likely to be the cause. The new documentation states:

If an app is in the frequent bucket [or below], the system imposes stronger restrictions on its ability to run jobs and trigger alarms

and

In particular, the bucket determines how frequently the app's jobs run, how often the app can trigger alarms

Additionally, if you view Power Details you can get a rough idea of the delay times.

enter image description here

Worth noting is that it appears your bucket is based on average usage (and machine learning) not on current usage - which means even if your app has just been in the foreground, the buckets play some role

查看更多
SAY GOODBYE
3楼-- · 2020-06-16 05:15

This is happening because of Power management feature introduced in Android Pie.

In android P, strict restrictions are introduced on the apps running in background. These restrictions are explained here

As we can see in the above link, if we connect the device to charging there are no restrictions imposed on the device and notifications are working properly. However, if we remove the device then Android system adds the certain restrictions for the apps running in background.

We can turn off this restrictions by turning off battery optimization for our application from device settings. Search for battery optimization in settings and turn it off for our application.

Also, testing the notifications by changing the device date and time was a hack that worked fine till now but in Android P, we've to either test them in real time scenario or turn off battery optimization for our application to test them.

I hope this will clear our doubts.

查看更多
登录 后发表回答