The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Notifications

Pop-up messages. Tray icons and their associated “bubble” messages. Bouncing dock icons. You are no doubt used to programs trying to get your attention, sometimes for good reason.

Your phone also probably chirps at you for more than just incoming calls: low battery, alarm clocks, appointment notifications, incoming text message or email, etc.

Not surprisingly, Android has a whole framework for dealing with these sorts of things, collectively called “notifications”.

Prerequisites

Understanding this chapter requires you to have read the core chapters of the book.

What’s a Notification?

A service, running in the background, needs a way to let users know something of interest has occurred, such as when email has been received. Moreover, the service may need some way to steer the user to an activity where they can act upon the event — reading a received message, for example. For this, Android supplies status bar icons, flashing lights, and other indicators collectively known as “notifications”.

Your current phone may well have such icons, to indicate battery life, signal strength, whether Bluetooth is enabled, and the like. With Android, applications can add their own status bar icons, with an eye towards having them appear only when needed (e.g., a message has arrived).

Notifications will appear in one of two places. On most devices, they will appear in the status bar, on the top of the screen, left-aligned:

Notifications, on a Galaxy Nexus
Figure 405: Notifications, on a Galaxy Nexus

On a pre-Android 4.2 tablet (and occasionally on other tablets newer than that), they will appear in the system bar, on the bottom of the screen, towards the lower-right corner:

Notifications, on a Galaxy Tab 2
Figure 406: Notifications, on a Galaxy Tab 2

In either case, you can expand the “notification drawer” to get more details about the active notifications, either by sliding down the status bar:

Notification Drawer, on a Galaxy Nexus
Figure 407: Notification Drawer, on a Galaxy Nexus

or by tapping on the clock on the system bar on some tablets:

Notification Drawer, on a Galaxy Tab 2
Figure 408: Notification Drawer, on a Galaxy Tab 2

Some notifications will be complex, showing real-time information, such as the progress of a long download. More often, notifications are fairly simple, providing just a couple of lines of information, plus an identifying icon. Tapping on the notification drawer entry will typically trigger some action, such as starting an activity — an email app letting the user know that “you’ve got mail” can have its notification bring up the inbox activity when tapped.

Showing a Simple Notification

The preview of this section was lost in the sofa cushions.

A Tale of Two NotificationCompats

The preview of this section is en route to Mars.

The Activity-Or-Notification Scenario

The preview of this section was stepped on by Godzilla.

Big (and Rich) Notifications

The preview of this section was whisked away by a shark-infested tornado.

Foreground Services

The preview of this section is sleeping in.

Disabled Notifications

The preview of this section left for Hollywood to appear in a reality TV show.