[英]Notifications in Android Oreo
First of all, I know this has been asked several times before, however I've yet to find a working solution so I thought about asking anyway. 首先,我知道这已经被问过几次了,但是我还没有找到一个可行的解决方案,所以我还是想问一下。
In my app I'm using a AlarmManager
to trigger a notification at a specific moment. 在我的应用中,我正在使用
AlarmManager
在特定时刻触发通知。 It works all fine in Nougat and lower, however it never triggers when tested on Oreo (and suppose it wouldn't work on Pie either). 它在牛轧糖及更低的牛轧糖上都可以正常工作,但是在奥利奥(Oreo)上测试时永远不会触发(假设它在Pie上也不起作用)。
I've tried all sorts of things, like using setExactAndAllowWhileIdle
. 我已经尝试过各种方法,例如使用
setExactAndAllowWhileIdle
。 Which according to other users should work on Oreo but it didn't work for me. 其他用户认为应该在Oreo上工作,但对我不起作用。 I also found about WorkManager, however people recommended not to use it in production apps since it's still in beta and apparently it's also not recommended if you need the task to run at a really specific time.
我还发现了有关WorkManager的信息,但是人们建议不要在生产应用程序中使用它,因为它仍处于测试阶段,并且如果您需要在特定时间运行任务,显然也建议不要使用它。
I would show my code however it's just a simple AlarmManager
with a BroadcastReceiver
like any other example you might find on the internet. 我将展示我的代码,但是它只是一个带有
BroadcastReceiver
的简单AlarmManager
,就像您在互联网上可能找到的任何其他示例一样。
This is a really app breaking "bug" for me and at this point I don't know what to do. 对我来说,这是一个真正的应用程序,可以打破“ bug”,目前我不知道该怎么办。 Thanks.
谢谢。
EDIT : The AlarmManager
does work in Android Oreo and above, the problem was with the notifications. 编辑 :
AlarmManager
确实可以在Android Oreo及更高版本中使用,问题出在通知上。 Starting with Oreo you need to set up a NotificationChannel
, otherwise the notifications are never going to show up. 从Oreo开始,您需要设置一个
NotificationChannel
,否则通知将永远不会显示。 That's it. 而已。
Key point is that you have to use explicit broadcasts in case of Oreo. 关键是在奥利奥的情况下,您必须使用显式广播。
Discussion with solution . 讨论解决方案 。
Documentation . 文件资料 。
Try this, it may work. 试试这个,可能会起作用。 I was also facing same issue as yours and this code works fine for me.
我也遇到了与您相同的问题,此代码对我来说很好用。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(d.getTime(), pendingIntent), pendingIntent);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
alarmManager.setExact(AlarmManager.RTC, d.getTime(), pendingIntent);
else
alarmManager.set(AlarmManager.RTC, d.getTime(), pendingIntent);
setAlarmClock() method works in most of each and every conditions. setAlarmClock()方法适用于大多数情况。 Even if device is in Doze Mode , this method will work.
即使设备处于打ze模式 ,此方法也可以使用。
You can know more about setAlarmClock()
method from here . 您可以从此处了解有关
setAlarmClock()
方法的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.