Android P Beta - AlarmManager 通知不工作
Android P Beta - AlarmManager Notifications not working
我正在 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));
}
我在 Android 8.0 上测试了相同的逻辑,但它工作正常。在 Android 9.0 中,通知是有效的,但有时它们根本不起作用。此外,如果它们有效,它们并不准确并且会花费太多时间,即使应用程序在前台也会发生这种情况。
逻辑是,我在特定时间设置了重复提醒,这些提醒应该每天在指定时间重复。此外,这些都是高优先级提醒,应该在准确的时间着陆,所以我正在使用 setExact,一旦收到通知就会显示,并设置同一天下一周的新警报。
我检查了 Android P API 文档,但找不到任何影响 AlarmManager 工作的 link和通知。我觉得唯一导致问题的是 Android P 中的电源管理和优先级桶。但是,即使应用程序在前台,通知也无法正常工作。
我在这里遗漏的任何东西。非常感谢任何帮助。
正如您自己提到的,电源管理的新 App Standby Buckets 功能可能是原因。新文档指出:
If an app is in the frequent bucket [or below], the system imposes stronger restrictions on its ability to run jobs and trigger alarms
和
In particular, the bucket determines how frequently the app's jobs run, how often the app can trigger alarms
此外,如果您查看 Power Details,您可以大致了解延迟时间。
值得注意的是,您的存储桶似乎是基于平均使用情况(和机器学习)而不是 当前使用情况 - 这意味着即使您的应用刚刚在前台运行, 水桶起到一定的作用
这是因为 Power management 在 Android Pie 中引入的功能。
在androidP中,对后台应用运行进行了严格的限制。这些限制解释 here
正如我们在上面 link 中看到的那样,如果我们将设备连接到充电,则不会对设备施加任何限制并且通知会正常工作。但是,如果我们移除设备,那么 Android 系统会在后台添加对应用 运行 的特定限制。
我们可以通过从设备设置中为我们的应用程序关闭电池优化来关闭此限制。在设置中搜索电池优化并为我们的应用程序将其关闭。
此外,通过更改设备日期和时间来测试通知是一种直到现在都运行良好的 hack 但在 Android P 中,我们要么测试他们在实时场景中或为我们的应用程序关闭电池优化以测试它们。
我希望这会消除我们的疑虑。
我正在 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));
}
我在 Android 8.0 上测试了相同的逻辑,但它工作正常。在 Android 9.0 中,通知是有效的,但有时它们根本不起作用。此外,如果它们有效,它们并不准确并且会花费太多时间,即使应用程序在前台也会发生这种情况。
逻辑是,我在特定时间设置了重复提醒,这些提醒应该每天在指定时间重复。此外,这些都是高优先级提醒,应该在准确的时间着陆,所以我正在使用 setExact,一旦收到通知就会显示,并设置同一天下一周的新警报。
我检查了 Android P API 文档,但找不到任何影响 AlarmManager 工作的 link和通知。我觉得唯一导致问题的是 Android P 中的电源管理和优先级桶。但是,即使应用程序在前台,通知也无法正常工作。
我在这里遗漏的任何东西。非常感谢任何帮助。
正如您自己提到的,电源管理的新 App Standby Buckets 功能可能是原因。新文档指出:
If an app is in the frequent bucket [or below], the system imposes stronger restrictions on its ability to run jobs and trigger alarms
和
In particular, the bucket determines how frequently the app's jobs run, how often the app can trigger alarms
此外,如果您查看 Power Details,您可以大致了解延迟时间。
值得注意的是,您的存储桶似乎是基于平均使用情况(和机器学习)而不是 当前使用情况 - 这意味着即使您的应用刚刚在前台运行, 水桶起到一定的作用
这是因为 Power management 在 Android Pie 中引入的功能。
在androidP中,对后台应用运行进行了严格的限制。这些限制解释 here
正如我们在上面 link 中看到的那样,如果我们将设备连接到充电,则不会对设备施加任何限制并且通知会正常工作。但是,如果我们移除设备,那么 Android 系统会在后台添加对应用 运行 的特定限制。
我们可以通过从设备设置中为我们的应用程序关闭电池优化来关闭此限制。在设置中搜索电池优化并为我们的应用程序将其关闭。
此外,通过更改设备日期和时间来测试通知是一种直到现在都运行良好的 hack 但在 Android P 中,我们要么测试他们在实时场景中或为我们的应用程序关闭电池优化以测试它们。
我希望这会消除我们的疑虑。