简体   繁体   中英

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. 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).

I've tried all sorts of things, like using setExactAndAllowWhileIdle . Which according to other users should work on Oreo but it didn't work for me. 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.

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.

This is a really app breaking "bug" for me and at this point I don't know what to do. Thanks.

EDIT : The AlarmManager does work in Android Oreo and above, the problem was with the notifications. Starting with Oreo you need to set up a NotificationChannel , otherwise the notifications are never going to show up. 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. Even if device is in Doze Mode , this method will work.

You can know more about setAlarmClock() method from here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM