首页 > 解决方案 > Android P Beta - AlarmManager 通知不起作用

问题描述

我正在 Android P beta 版本 4 上测试我的应用程序。我的应用程序的targetSdkVersion 是 27

已观察到警报管理器通知未按预期工作。我正在使用下面的代码来设置通知 -

           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.

我检查了 Android P API 文档,但找不到任何影响AlarmManager和 Notifications 工作的链接。我觉得唯一导致问题的是Android P 中的电源管理和优先级存储桶。但是,即使应用程序在前台,通知也无法正常工作。

我在这里缺少的任何东西。任何帮助深表感谢。

标签: androidandroid-notificationsandroid-alarmsandroid-9.0-pieandroid-powermanager

解决方案


正如您自己提到的,电源管理的新App Standby Buckets功能可能是原因。新文件指出:

如果应用在频繁桶[或以下]中,系统对其运行作业和触发警报的能力施加更强的限制

特别是,存储桶决定了应用程序的作业运行频率,应用程序触发警报的频率

此外,如果您查看电源详细信息,您可以大致了解延迟时间。

在此处输入图像描述

值得注意的是,您的存储桶似乎是基于平均使用情况(和机器学习)而不是当前使用情况 - 这意味着即使您的应用刚刚处于前台,存储桶也会发挥一些作用


推荐阅读