March 12

Android Oreo Notifications: A Double Stuffed Headache

Oreo might be “Milk’s Favorite Cookie”, but it’s proving to be an Android developer’s least favorite update – at least in terms of the Android Oreo notifications. When the AerisWeather Android team started updating our projects for Android 8.0, it seemed like a typical day at the office. Update Gradle, set our target SDK to 26, resync, rebuild and then fix the stuff that Google broke. In fact, the first round of changes went pretty quickly. Then we noticed the notifications weren’t working on our Oreo test device…

The Deception Begins

One of the things that makes the Android Oreo notifications so tricky is that if they are not set up correctly, nothing really happens. Aside from the fact that the notifications aren’t being generated, there’s really not much indication of a problem. Yes, there is that annoying message that appears when debugging. It states, “Failed to post a notification on channel null”. But it, like a lot of things in software development, doesn’t stop you from digging yourself a hole to fall into. Doing due diligence, however, pretty quickly tells us that the lack of “in your face” errors may be hiding a deeper issue.

Channel The Energy

One of the main components of the updated Android Oreo notifications is the use of channels. When you target API level 26, you are required to use channels to display notifications to your users. Channels increase the user’s control over the notifications they get and are the “pipeline”, so to speak, for our apps to communicate with their humans. There are tons of great articles describing the intent behind, and proper use of, Android’s notification channels (like this one). So I’ll let you Google the details of the why, and for this conversation we’ll stick with how we dealt with it.

In the demo app, it’s pretty simple. We only need one channel, and we set it up in the app’s BaseApplication class.

Sweet! Now we have a way to “channel” our info to the user (sorry, I couldn’t resist).

Hey Background Services – Google Has It’s Eye On You

Notice that we’ve used the word “FOREGROUND” in the service id’s variable name above. It’s a not so subtle reminder to ourselves (and our SDK users) that something important is happening here. With Android Oreo, Google took the term “background” to a whole new level when it comes to services. In order to help save battery life, promote transparency of services, and I’m sure various other reasons, background services now stop running when the phone goes to sleep. It makes sense. It really does, and when we stopped banging our heads on the desk we agree with the change. It’s just a lot different from how services worked before, and therefore our notifications will need some changes if we want them to work as designed.

Foreground Service To The Rescue

For our weather apps, there are cases where we want to notify the user that something is happening, even if the phone is sleeping. One reason we use a foreground service is to create a persistent weather notification. Having this notification attached to a foreground service allows us to update that notification with current weather information or alerts.

To create a service that runs in the foreground, we just need to build the notification, then pass it to the startForeground() function.

Notice that when we created the notification, we now specify the channel created earlier. Next we told Android to start the foreground service. This part is really key. If the service isn’t started before we try to send a notification, we’ll get an error stating that the foreground service we’re trying to use wasn’t started. As far as errors go, that one is actually pretty helpful.

Wrap It Up

In this article we only scratched the surface of the Android Oreo notifications. Once you have the notifications up and running, you will need to decide how to tweak the settings to get your desired result. You can set the priority/importance level, which determines where the notifications will show, makes noise and peeks. You can set visibility, which deals with the lockscreen. Set the color of the indicator lights if the hardware supports it, or vibrate the phone.
Even though some of this was possible before the Android Oreo notifications update, a lot of it has changed. If you use notifications in your app and you haven’t checked out the documentation on them lately, you might want to take a look. Especially if you’re planning to move up to v26 of the Android SDK.

Once you’re done brushing up on your notification skills, come on over and check out our Aeris Android SDK. We just released v2.4 of the SDK, with the demo app we referenced earlier, so take a look and then build a weather app!

Happy Coding!

The Aeris Android Team

Android Corner Wave

Share this post:

3 Comments

  1. July 10, 2018 at 3:51 pm

    I don’t like the oreo notification at all. The notification style of marshmallow was better. I have watched over many OS which has better notification style than Oreo. The oreo notification is really a headache.

  2. acrmacrm
    September 5, 2018 at 10:56 am

    Tell me …how to Set battery optimization option –> Don’t optimise by default by programatically.

  3. September 10, 2018 at 3:53 am

    Nice and helpful information shared by this post with us about android Oreo notifications a headache and I hope that we will also get more new information regarding this post as soon as. This information is valuable for most of the users.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.