Android Notifications#1: Creating Basic Notifications

Shashank Mohabia
The Startup
Published in
8 min readMay 24, 2020

We all know how we reach out to our phones whenever we hear a pop sound, because we know there is something new for us. Yes, I am talking about notifications here. Almost every application we have in our phone notifies us about the latest updates we have like the chats, new offers, or media playback like we have in Saavn or VLC.

For developers Android provides a list of notification types and then are their customizations too. So I decided to break this topic into a series of blogs for readability. This series includes the following blogs:

  1. Creating Basic Notifications (this blog)
  2. Adding Actions and Modifying Properties
  3. Expandable Notifications and Notifications Groups

In this blog, we will see some basic concepts related to android notifications and then see how to create a simple notification for our app. We will dive deeper into the concept in the further blogs of the series.

Overview:

A notification is a message that Android displays outside your app’s UI to provide the user with reminders, communication from other people, or other timely information from your app. Users can tap the notification to open your app or take action directly from the notification.

Appearances:

Notifications appear on different locations and format like:

  1. App icon badge (Android 8+)
  2. Heads up notifications (for important notifications)
  3. Lock screen (Android 5+)
  4. Status bar and notification drawer
  5. Wear OS (If the user has a paired Wear OS device)

Notification anatomy:

The design of a notification is determined by system templates — your app simply defines the contents for each portion of the template. Some details of the notification appear only in the expanded view.

The most common parts of the notification are indicated in the figure as follows:

  1. Small icon: This is required and set with setSmallIcon().
  2. App name: This is provided by the system.
  3. Timestamp: This is provided by the system but you can override 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().

Notification channels:

Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel or they will not appear. By categorizing notifications into channels, users can disable specific notification channels for your app (instead of disabling all your notifications), and users can control the visual and auditory options for each channel — all from the Android system settings (figure below). Users can also long-press a notification to change behaviors for the associated channel.

On devices running Android 7.1 (API level 25) and lower, users can manage notifications on a per-app basis only (effectively each app only has one channel on Android 7.1 and lower).

Note: The user interface refers to channels as “categories.”

One app can have multiple notification channels — a separate channel for each type of notification the app issues. An app can also create notification channels in response to choices made by users of your app. For example, you may set up separate notification channels for each conversation group created by a user in a messaging app.

The channel is also where you specify the importance level for your notifications on Android 8.0 and higher. So all notifications posted to the same notification channel have the same behavior.

Note: There is a lot more customizations available for channels. Look at this article in documentation to learn more.

Notification importance:

Android uses the importance of notification to determine how much the notification should interrupt the user (visually and audibly). The higher the importance of the notification, the more interruptive the notification will be.

On Android 8.0 (API level 26) and above, the importance of a notification is determined by the importance of the channel the notification was posted to. Users can change the importance of a notification channel in the system settings (figure below).On Android 7.1 (API level 25) and below, the importance of each notification is determined by the notification's priority.

The possible importance levels and their behaviors are shown in the image.

All notifications, regardless of importance, appear in non-interruptive system UI locations, such as in the notification drawer and as a badge on the launcher icon.

Do Not Disturb mode:

Starting in Android 5.0 (API level 21), users can enable Do Not Disturb mode, which silences sounds and vibration for all notifications. Notifications still appear in the system UI as normal, unless the user specifies otherwise.

There are three different levels available in Do Not Disturb mode:

  • Total silence: blocks all sounds and vibrations, including from alarms, music, videos, and games.
  • Alarms only: blocks all sounds and vibrations, except from alarms.
  • Priority only: users can configure which system-wide categories can interrupt them (such as only alarms, reminders, events, calls, or messages). For messages and calls, users can also choose to filter based on who the sender or caller is (figure 13).

On Android 8.0 (API level 26) and above, users can additionally allow notifications through for app-specific categories (also known as channels) by overriding Do Not Disturb on a channel-by-channel basis. For example, a payment app might have channels for notifications related to withdrawals and deposits. The user can then choose to allow either withdrawal notifications, deposit notifications, or both when in priority mode. On devices running Android 7.1 (API level 25) and below, users can allow notifications through on an app by app basis, rather than on a channel by channel basis.

Posting limits:

Beginning with Android 8.1 (API level 27), apps cannot make a notification sound more than once per second. If your app posts multiple notifications in one second, they all appear as expected, but only the first notification per second makes a sound.

However, Android also applies a rate limit when updating a notification. If you post updates to a single notification too frequently (many in less than one second), the system might drop some updates.

Create a basic Notification:

Now you know enough theory you need to get started with creating a notification for your app.

Step 1. Add dependencies:

If you already have the androidx support library in your build.gradle file then you can skip this step. Otherwise to use NotificationCompat add the support compact library like this:

dependencies {
implementation "com.android.support:support-compat:28.0.0"
}

Step 2. Create a channel and set the importance:

First of all we have to create a channel for our notification with some details as shown in the code snippet below. Set the importance category with reference to what we saw in the previous sections.

Note: since the channels are required for Android 8.0 or higher, hence we put a check there.

For Android 8.0 and higher, you should execute this code as soon as your app starts. It’s safe to call this repeatedly because creating an existing notification channel performs no operation.

Although you must set the notification importance/priority as shown here, the system does not guarantee the alert behavior you’ll get. In some cases the system might change the importance level based on other factors, and the user can always redefine what the importance level is for a given channel.

Step 3. Create a notification builder:

Now we will use the channel created above to create a notification.

Don’t get confused by the fact that we set the priority in the channel then why are we doing it again here. This is being done for users with below Android 8.0 because for them channels won’t be created.

Another thing to notice here is the setStyle() template. Android by default truncates the notification content to one line, but if you want to display entire content in a larger text area then use it with BigTextStyle tag.

Step 4. Set the notification’s tap action:

Now its time to add tap action to our notification so that when we tap the notification we can be directed to the corresponding activity. For this we will use pending intent. Modify your createNotification() function like this:

setAutoCancel()automatically removes the notification when the user taps it.

The setFlags() method shown above helps preserve the user's expected navigation experience after they open your app via the notification. But whether you want to use that depends on what type of activity you're starting, which may be one of the following:

  • An activity that exists exclusively for responses to the notification. There’s no reason the user would navigate to this activity during normal app use, so the activity starts a new task instead of being added to your app’s existing task and back stack. This is the type of intent created in the sample above.
  • An activity that exists in your app’s regular app flow. In this case, starting the activity should create a back stack so that the user’s expectations for the Back and Up buttons is preserved.

For more about the different ways to configure your notification’s intent, read Start an Activity from a Notification.

Step 5. Start the notification:

To show the notification just notify the NotificationManager about the builder and pass a unique id for the notification. So the final createNotification() function looks like this:

Call this function whenever you want to display a notification. I hardcoded all the required data you can pass them as arguments to these functions.

You can also have a separate NotificationsManager for your app and use it by injecting it wherever you want it using DI. A sample NotificationsManager is given below:

We will dive deep into notifications in the further blogs of this series (list given at the top).

Feel free to comment if there are doubts or if you feel anything needs to be corrected😀.

Resources:

Google Codelabs

Android Developers Documentation

--

--