Image for post
Image for post
Photo by Noelle Otto from Pexels

Android Notifications in 5 Minutes

Brandon Wever
Jan 31, 2019 · 5 min read

Notifications are one of the fastest and easiest ways to add more interactivity and engagement to your apps. Notifications provide quick snippets of information that your users need to know right now. Notifications also allow users to quickly take actions on events. We’ll work through all the most used examples of notifications and you’ll be notifying your users in no time.

Building a Notification

Image for post
Image for post
  • Small icon will be shown in the status bar, and it is important to know that they will be painted over in color. The reason this is important is is you’ll need an icon with alpha. If there is no alpha in the icon, you’ll end up with a great white square instead of your own branding.
  • Content title is the well, title of your content. This should be a short description of why the notification is being shown.
  • Content body is a bit of detail on your notification. You should be cognizant of how much text you are putting in the body, with the normal configuration you will be limited to one line of text.
  • Large icon is a Bitmap related to your notification. This can be something like a profile image.

The code is fairly simple to put this together as shown below.

val notification = NotificationCompat.Builder(context, CHANNEL_ID)

Showing a Notification

with(NotificationManagerCompat.from(context)) {
if (getNotificationChannel(CHANNEL_ID) == null) {

There is an extra bit of code in there, the check for if the notification channel exists.

Channels and Importance

fun createNotificationChannel(context: Context) {
val name = context.getString(R.string.channel_name)
val descriptionText = context.getString(R.string.channel_description)
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
description = descriptionText

A channel should have the following information

  • ID, an identifier for our channel, this should be a String constant in your application.
  • Name, this should communicate to the user what sort of notifications are in the channel something short and simple like “Messages” work for this.
  • Importance, this can be High, Default, Low, or Min, how high you set this results in how much user interference there will be when you notify the user
  • Description, this will show in the Android settings menu and should describe in greater detail about the channel

Responding to User Actions

The simplest way to handle a large amount of notifications is through the use of a BroadcastReceiver. When a user performs an action on a notification, the Intent will be fired and the BroadcastReceiver’s onReceive method will be invoked.

val builder = NotificationCompat.Builder(context, CHANNEL_ID)
.setContentIntent(onContentTapped /** Pending intent */)
.setDeleteIntent(onSwipedAway /** Pending intent */)
onActionTapped /** Pending intent */
  • ContentIntent will be fired when the user taps on the notification. It is important to note the notification will not be dismissed unless you also call setAutoCancel(true) on the notification.
  • DeleteIntent will be fired when the user swipes away the notification.
  • Actions are the buttons at the bottom of the notification. These also have icons, and titles required.

To create your BroadcastReceiver you’ll need to extend the BroadcastReceiver class, override the onReceive method, and do not forget to declare the Receiver in the AndroidManifest.xml.

class NotificationBroadcastReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {
val action = intent.action

when (action) {
Toast.makeText(context, "Action One hit!", Toast.LENGTH_SHORT).show()
Toast.makeText(context, "Action Two hit!", Toast.LENGTH_SHORT).show()

object NotificationController {
fun pendingIntent(context: Context, name: String, extras: Bundle?): PendingIntent {
val intent = Intent(context, {
action = name

return PendingIntent.getBroadcast(context, 0, intent, 0)

NotificationController is a quick utility to create PendingIntents for notifications. The most important thing is to create differently named actions to correctly handle how the user interacted with the notification. Also if you are going to launch an Activity from the BroadcastReceiver you must use the IntentFlag NEW_TASK. It’s a small price to pay to handle all notifications in a common place in my mind. Intents can also be registered to different BroadcastReceivers, so you could limit the scope to one BroadcastReceiver per type of notification.

If you have any questions about notifications check out the docs or ask in the comments below. There are plenty of great examples in the documentation, but hopefully I gave you enough to get started.

Thanks for reading!

Atomic Robot

Digital agency specializing in strategy, design and…

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