Image for post
Image for post

Handling Android Notifications with Kotlin

A common situation that as Android Developers we deal with is keeping our app’s users up-to-date with what is going on when they are not using our app. The Android SDK provides us with a fully integrated API to do this: Notifications.

Notifications are messages that developers can program and send to the OS to be shown outside of our amazing UI so if the user is not using the app, he or she won’t miss an important event. Sometimes, our app may send a lot of notifications that can affect how the user perceives our features, potentially leading to losing them as our costumers because they are receiving meaningless information. However, with this new level API we have now the tools to let the users manage this issue.

Notifications can be used in several situations:

  • Show reminders.
  • Inform the users about a new promotional code/price for a service our app provides.
  • Tell them that some long running operation has been finished successfully or if an error occurred.
  • Inform the users that something they were expecting to happen, has already happened.
  • Anything you can imagine that needs to gain user attention. Imagination is the limit!!!

How do we use them?

The first thing you need to create is a Notification Channel, which in essence is a way to group your notifications by topic. This will offer the user the possibility for fine-grained control over notifications.

Call this factory from your Application class.

Here we are only creating one channel to group all our notifications but you can use a channel for each major topic/subject your app internally handles. For example, a channel could show sales news and another display order tracking data. As you can see, the above code snippet uses @Inject & @ApplicationScope, which are Dagger (2.13) annotations to indicate that this class can be instantiated only once (singleton pattern) and can be injected on our app components.

Next, we have to place notifications on our channels. To do this, we use Kotlin Sealed Classes to create a common interface so we can create our notifications according to the type and actions needed. Also, we can reuse code for future implementations.

We’ll use these models in the next Factory to build the OS notifications in a customized and centralized way.

This class does two things well. First, it creates an OS Notification Object based on the classes that extend from *SkeletalNotification*. Second, it delivers this notification to the system to be displayed on the status bar so that the user can interact with the app even when it is in the background. On prior versions of Android 8.0, the *NotificationCompat* ignores the assigned channel to the notification in order to deliver and display the notification.

Some suggestions

  • Always use the *NotificationCompat* class to build notifications. This class will put the notifications inside the right channel when running on devices with Android 8.0+ and will be compatible with versions that don’t support them.
  • Create enough channels to let your users disable only the notifications they are not interested in. Be careful and separate them correctly so users don’t lose important notifications.

Full code is hosted here, go over the notifications recipe inside the cookbook package.

Image for post
Image for post

Wolox

We are a tech company redefining the way things work.

Thanks to Dylan Muszel, Romina Blasucci, and Lucía Bermúdez

León Darío Peña Londoño

Written by

Wolox

Wolox

We specialize in end-to-end development of high impact products, providing technological solutions to start-ups and companies that are seeking to innovate and need support in developing their ideas. In January 2021, we became part of Accenture.

Wolox

Wolox

We specialize in end-to-end development of high impact products, providing technological solutions to start-ups and companies that are seeking to innovate and need support in developing their ideas. In January 2021, we became part of Accenture.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store