Geek Culture
Published in

Geek Culture

Notifications in Android

Notification is a display message that we show outside of our app to users. It helps to remind time-based things or communicate with other people. Users can open the app by tapping on the notification or provide action there only.

Like in WhatsApp we get message notifications if the user taps on notification we open a chat window or we can simply type our message in notification. and send it.

Appearances on a device

Notification icons appear on left side of status bar
Reminder for sleep time in HealthifyMe app

Users can see notifications in the top drawer. Notification card contains Icon, title, message, big icon, and action buttons.

Users can drag down notifications to see expanded views.

A notification remains visible in the notification drawer until dismissed by the app or the user.

Notification will be visible on the lock screen until the user hides it from settings.

Gmail small and expanded notification view

As you can see from the above screenshots, a small view for email notification is on left and when a user drags it down we can see an expanded view. We have two action buttons here Archive and Reply. With on click of those button user will land on an integrated screen. In the next part of this article, we will see how to implement that.

Create a Notification

We will start from basic and then move to expand, action, and priority of it. A basic notification contains an icon, title, and message, and click.

Set the notification content:
  1. Small icon: From above screenshot, small icon will be set by setSmallIcon().
  2. App Name: This is provided by the system.
  3. Timestamp: This is provided by the system but you can override it with setWhen() or hide it with setShowWhen(false).
  4. Large icon: This is optional (usually used only for contact photos; do not use it for your app icon) and set with setLargeIcon().
  5. Title: This is optional and set with setContentTitle().
  6. Text: This is optional and set with setContentText().

you need to set the notification’s content and channel using a NotificationCompat.Builder object.

val builder = NotificationCompat.Builder(context, NotificationUtils.CHANNEL_OTHERS)
.setColor(ContextCompat.getColor(context, R.color.brand_nutrition_track))

Notice that the NotificationCompat.Builder the constructor requires that you provide a channel ID. This is required for compatibility with Android 8.0 (API level 26) and higher but is ignored by older versions.

By Default notification text comes in one line. If your content is big which can’t fit in one line, you can add an expanded view by adding style :


Create a channel

Before you can deliver the notification on Android 8.0 and higher, you must register your app’s notification channel with the system by passing an instance of NotificationChannel to createNotificationChannel(). So the following code is blocked by a condition on the SDK_INT version:

Because it's necessary to set a notification channel to deliver notification on android 8.0 or higher, this should happen on application launch or at least before you create notification builder.

User control app notification with channels. We can do that by going into app settings -> notification and you will see a window like this.

Above you can see a list of channels in the HealthifyMe app. If we don't want a particular channel notification just disable it.

Lock screen visibility

As you can see from the above code snippet, we are setting lock screen visibility. That is used for security purposes like if the user doesn't want the notification to be shown when the device is locked or just wants to show limited content.

Use setVisibility() method:

  • VISIBILITY_PUBLIC shows the notification's full content.
  • VISIBILITY_SECRET doesn't show any part of this notification on the lock screen.
  • VISIBILITY_PRIVATE shows basic information, such as the notification's icon and the content title, but hides the notification's full content.

Set Notification priority

Notification priority defines how a channel gonna interrupts users to show notifications when they are doing something else.

The importance level you assign will be the channel’s default. Users can change a channel’s importance level in Android Settings.


Notification’s tap action

When you click on the notification it should open an activity, To do so, you must specify a content intent defined with a PendingIntent object and pass it to setContentIntent().

Notice that we have added setAutoCancel(), which will cancel the notification when you tap on it.

Set Flags

val pendingIntent = PendingIntent.getActivity(context,

When we are creating pending intent for touch action of notification, we can set flag y calling setFlags() method or by providing object value in the method. It helps preserve the user’s expected navigation experience after they open your app via the notification.

Now we need to set this or not depends on the type of activity we are opening. There are two scenarios:

  • Regular activity: This is an activity that exists as a part of your app’s normal UX flow. So when the user arrives in the activity from the notification, the new task should include a complete back stack, allowing them to press Back and navigate up the app hierarchy.
  • Special activity: The user only sees this activity if it’s started from a notification. In a sense, this activity extends the notification UI by providing information that would be hard to display in the notification itself. So this activity does not need a back stack.

Details on flags

We’ve talked a bit about a few of the flags that can be used when creating a PendingIntent, but there are a few others to cover as well.

🤡 FLAG_IMMUTABLE: Indicates the Intent inside the PendingIntent cannot be modified by other apps that pass an Intent to PendingIntent.send(). An app can always use FLAG_UPDATE_CURRENT to modify its own PendingIntents

🆕 FLAG_MUTABLE: It should only be used when some functionality relies on modifying the underlying intent, e.g. any PendingIntent that needs to be used with inline reply or bubbles.

😎 FLAG_UPDATE_CURRENT: Requests that the system update the existing PendingIntent with the new extra data, rather than storing a new PendingIntent. If the PendingIntent isn’t registered, then this one will be.

👆🏻 FLAG_ONE_SHOT: Flag indicating that this PendingIntent can be used only once. For use with #getActivity, getBroadcast, and getService.

If set, after send() is called on it, it will be automatically canceled for you, and any future attempt to send through it will fail.

🔐 Utilizing FLAG_ONE_SHOT prevents issues such as “replay attacks”.

FLAG_CANCEL_CURRENT: Cancels an existing PendingIntent, if one exists, before registering this new one. This can be important if a particular PendingIntent was sent to one app, and you’d like to send it to a different app, potentially updating the data. By using FLAG_CANCEL_CURRENT, the first app would no longer be able to call send on it, but the second app would be.

This is what we have used in the above code snippet.

Set sound:

Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

Show Notification

Once you have created notification builder ,channel, and set the content to. show and. all action now. its time to show. the notification.


see below the complete code for notification:

I hope this article was helpful for you. Notifications are important in many android apps if we want user attention. Here we learned about what is notification how. they look at, how to create a notification channel, how to add display content for notification, how to provide click action and show a notification.

But this is not over yet. In the next part of this article, we will learn about grouped notifications, time-bound notifications and user action buttons on notifications, retrieving user input data in the app, progress bar.

Find next part of this article here:


If you have any feedback please write me back at claps are really appreciated to help others find this article 😃 .



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