android - 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 中的电源管理和优先级存储桶。但是,即使应用程序在前台,通知也无法正常工作。
我在这里缺少的任何东西。任何帮助深表感谢。
解决方案
正如您自己提到的,电源管理的新App Standby Buckets功能可能是原因。新文件指出:
如果应用在频繁桶[或以下]中,系统对其运行作业和触发警报的能力施加更强的限制
和
特别是,存储桶决定了应用程序的作业运行频率,应用程序触发警报的频率
此外,如果您查看电源详细信息,您可以大致了解延迟时间。
值得注意的是,您的存储桶似乎是基于平均使用情况(和机器学习)而不是当前使用情况 - 这意味着即使您的应用刚刚处于前台,存储桶也会发挥一些作用
推荐阅读
- android - 不使用 SpannableString 更改文本颜色
- firebase - Firebase 是否捕获应用程序的内存和 CPU 利用率?
- android - 如何使用 mockito 为“const val”编写 Junit 测试
- android - 如何在 3 分钟的活动中每个用户至少一次在应用程序中显示应用评分弹出窗口?
- windows - 在 Windows 会话 0 中,即使没有桌面,我也能够获得桌面,为什么?
- python-3.x - Tensorflow:TypeError:'RepeatDataset'对象不可调用 - 为什么
- javascript - 添加临时活动类
- jquery - 如何在表中找到最匹配的相邻行
- javascript - 如何在反应组件中加载新的axios数据
- lua - 无法使用 C API 编译多个 LUA 文件