Notifying with the Status Bar Android

The standard location for displaying notifications and indicators on an Android device is the status bar that runs along the top of the screen. Typically, the status bar shows information such as the current date and time. It also displays notifications (like incoming SMS messages) as they arrive—in short form along the bar and in full if the user pulls down the status bar to see the notification list. The user can clear the notifications by pulling down the status bar and hitting the Clear button.

Developers can enhance their applications by using notifications from their applications to inform the user of important events. For example, an application might want to send a simple notification to the user whenever new content has been downloaded. A simple notification has a number of important components:

  • An icon (appears on status bar and full notification)
  • Ticker text (appears on status bar)
  • Notification title text (appears in full notification)
  • Notification body text (appears in full notification)
  • An intent (launches if the user clicks on the full notification)

In this section, you learn how to create this basic kind of notification.

Using the NotificationManager Service

All notifications are created with the help of the NotificationManager. The Notification Manager (within the android.app package) is a system service that must be requested. The following code demonstrates how to obtain a valid NotificationManager object using the getSystemService() method:

NotificationManager notifier = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);

The NotificationManager is not useful without having a valid Notification object to use with the notify() method. The Notification object defines what information displays to the user when the Notification is triggered. This includes text that displays on the status bar, a couple of lines of text that display on the expanded status bar, an icon displayed in both places, a count of the number of times this Notification has been triggered, and a time for when the last event that caused this Notification took place.

Creating a Simple Text Notification with an Icon

You can set the icon and ticker text, both of which display on the status bar, through the constructor for the Notification object, as follows:

Notification notify = new Notification(
R.drawable.android_32, "Hello!", System.currentTimeMillis());

Additionally, you can set notification information through public member variable assignment, like this:

notify.icon = R.drawable.android_32;
notify.tickerText = "Hello!";
notify.when = System.currentTimeMillis();

You need to set a couple more pieces of information before the call to the notify() method takes place. First,we need to make a call to the setLastEventInfo() method, which configures a View that displays in the expanded status bar. Here is an example:

Intent toLaunch = new Intent
(SimpleNotificationsActivity.this,
SimpleNotificationsActivity.class);
PendingIntent intentBack = PendingIntent.getActivity
(SimpleNotificationsActivity.this, 0, toLaunch, 0);
notify.setLatestEventInfo(SimpleNotificationsActivity.this,
"Hi there!", "This is even more text.", intentBack);

Next, use the notify() method to supply the notification’s title and body text as well as the Intent triggered when the user clicks on the notification. In this case, we’re using our own Activity so that when the user clicks on the notification, our Activity launches again.

Working with the Notification Queue

Now the application is ready to actually notify the user of the event. All that is needed is a call to the notify() method of the NotificationManager with an identifier and the Notification we configured. This is demonstrated with the following code:

private static final int NOTIFY_1 = 0x1001;
// ...
notifier.notify(NOTIFY_1, notify);

The identifier matches up a Notification with any previous Notification instances of that type. When the identifiers match, the old Notification is updated instead of creating a new one. You might have a Notification that some file is being downloaded. You could update the Notification when the download is complete, instead of filling the Notification queue with a separate Notification, which quickly becomes obsolete. This Notification identifier only needs to be unique within your application.

The notification displays as an icon and ticker text showing up on the status bar. This is shown at the top of Figure.

Status bar notification showing an icon and ticker text.

Status bar notification showing an icon and ticker text.

Shortly after the ticker text displays, the status bar returns to normal with each notification icon shown. If the users expand the status bar, they see something like what is shown in Figure.

Expanded status bar showing the icon, both text fields, and the time of the notification.

Expanded status bar showing the icon, both text fields, and the time of the notification

Updating Notifications

You don’t want your application’s notifications piling up in the notification bar. Therefore, you might want to reuse or update notifications to keep the notification list manageable. For example, there is no reason to keep a notification informing the user that the application is downloading File X when you now want to send another notification saying File X has finished downloading. Instead, you can simply update the first notification with new information.

When the notification identifiers match, the old notification is updated. When a notification with matching identifier is posted, the ticker text does not draw a second time. To show the user that something has changed, you can use a counter. The value of the number member variable of the Notification object tracks and displays this. For instance, we can set it to the number 4, as shown here: notify.number = 4;

This is displayed to the user as a small number over the icon. This is only displayed in the status bar and not in the expanded status bar, although an application could update the text to also display this information. Figure shows what this might look like in the status bar.

Status bar notification with the count of “4” showing over the icon.

Status bar notification with the count of “4” showing over the icon

Clearing Notifications
When a user clicks on the notification, the Intent assigned is triggered. At some point after this, the application might want to clear the notification from the system notifications queue. This is done through a call to the cancel() method of the NotificationManager object. For instance, the notification we created earlier could be canceled with the following call:

notifier.cancel(NOTIFY_1);

This cancels the notification that has the same identifier. However, if the application doesn’t care what the user does after clicking on the notification, there is an easier way to cancel notifications. Simply set a flag to do so, as shown here:

notify.flags |= Notification.FLAG_AUTO_CANCEL;

Setting the Notification.FLAG_AUTO_CANCEL flag causes notifications to be canceled when the user clicks on them. This is convenient and easy for the application when just launching the Intent is good enough.

The Notification object is a little different from other Android objects you might have encountered. Most of the interaction with it is through direct access to its publicvariables instead of through helper methods. This is useful for a background application or service, discussed in Chapter “Working with Services.”The Notification object can be kept around and only the values that need to be changed can be modified. After any change, the Notification needs to be posted again by calling the notify() method.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Android Topics