Android P Beta-AlarmManager通知不起作用 [英] Android P Beta - AlarmManager Notifications not working

查看:214
本文介绍了Android P Beta-AlarmManager通知不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Android P beta 4版上测试我的应用程序.我的应用程序的 targetSdkVersion为27

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));
            }

我在Android 8.0上测试了相同的逻辑,但工作正常.在Android 9.0中,通知有效,但有时根本不起作用.另外,如果它们能正常工作,它们并不精确,并且会花费太多时间,即使应用程序在前台,也会发生这种情况.

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.

逻辑是,我有在特定时间设置的重复提醒,这些提醒应该在指定时间每天自动重复.另外,这些都是高优先级提醒,应该在准确的时间降落,因此我正在使用setExact,一旦收到通知,它就会显示出来,并设置同一天下周的新警报.

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.

我已经检查了Android P API文档,但是找不到任何对 AlarmManager 和通知的工作有影响的链接.我唯一引起问题的是 Android P中的电源管理和优先级存储桶.但是,即使应用程序位于前台,通知也无法正常工作.

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.

推荐答案

由于电源管理功能.

在android P中,对在后台运行的应用程序引入了严格的限制.在此处

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

正如我们在上面的链接中看到的那样,如果将设备连接至充电状态,则设备不受任何限制,并且通知可以正常运行.但是,如果我们删除设备,则Android系统会为在后台运行的应用添加某些限制.

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.

此外,通过更改设备日期和时间来测试通知是迄今为止一直可以正常使用的黑客手段,但是在Android P中,我们必须实时测试它们或关闭电池优化我们的应用程序以测试它们.

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.

这篇关于Android P Beta-AlarmManager通知不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆