Free Android app covering all aspects of addiction, including prevention and treatment

You can master notifications. It's easy! Part 1: Some stuff you should know.

  • Written by  Clive

 

Notification iconMaster the basics of Android notifications

Notifications inform the user about something. They appear in the Notification area at the top of the screen.

The Notification area

Notification area

Here are a couple of examples:

  • The battery is getting low
  • An email has arrived
  • Your file download is complete

Users can read them whenever the like. They just drag the notification drawer down to open it.

The Notification Drawer

Notification drawer opened. The user can now touch the notification to activate it. Pressing Clear or swiping the notification sideways, off-screen will cancel the notification.

Basic notifications have an icon, a title and a brief explanation. Some display ticker text.

Notifications can appear silently, with flashing lights, vibrations and sounds.

You can cancel notifications by clicking Clear or by sliding the notification off the screen. Touching the notification usually cancels it too.

App info to uninstall the app

Long-pressing the notification displays a pop-up showing which app sent it. You can choose to uninstall the app or stop all other notifications from it.

Uninstall app from app info screen

Notifications display the time that the system received the notification. This determines their order in the notification drawer.

As of Jelly Bean you can set the priority of a notification from low to high. This will then determine their order in the drawer or whether they are shown at all.

Two sizes fits all

All notifications appear in the notification drawer in normal view (up to 64dp high) except for Big view notifications at the top of the list which will be expanded.

Normal view notifications

Basic notification in Jelly Bean

This basic notification appears on a Jelly Bean device in normal view and is limited in height to 64dp.

Basic nonitifaction on Froyo device

Here’s the same normal view notification as it appears on a Froyo device.

Big view notifications

Devices running Jelly Bean and later can display Big view notifications. These can display more info and larger images (up to 256dp high) when expanded.

Big Picture notification on Jelly Bean device

The large icon (64 x 64px), top-left is usually used to display the contact photo of the person who is the source of the notification or for media apps to show a thumbnail. The big picture has a dimension of 512 x 256dp. The image is center-cropped.

Big Picture notification on Froyo device

This is the same big view notification as displayed on a Froyo device. It essentially displays a normal notification, disregarding all the big view information. The large icon is replaced by the small icon (24 x 24dp).

Big view notifications show most of the elements of normal view notifications. They also have a larger detail area which is used to display different information, depending on the type of Big view.

There are three types of Big view notifications:

  • Big picture style – displays a bitmap up to 256dp tall
  • Big text style – display large text block
  • Inbox style – displays up to 5 lines of text

All Big view notifications include the following content options not available in normal view notifications:

  • Big content title – overrides the normal view’s content title and only appears in the expanded view
  • Summary text – a line of text below the details area

So how do I send a notification?

Well, first you have to build it.

Building a normal view notification

Using the Notification.Builder class or the support library alternative

  • If your app is aimed at devices running Android 3.0 ((Honeycomb API level 11) and above - Use the Notification.Builder class
  • If your app is also aimed at devices running on anything below Honeycomb (API 4 and later), then use the support library class, NotificationCompat.Builder .  Not all the features will be available on the lower end devices. The notification still works but certain fields won’t be displayed.

Building the Notification

  1. Build your notification using the NotificationCompat.Builder (or Notification.Builder ) object to set your required information and actions
  2. Call NotificationCompat.Builder.build() (or Notification.Builder .build()) to create the notification. This returns a Notification object
  3. Send the notification by  passing the Notification object to the system by calling NotficationManager.notify()
A bare-bones notifications must have these fields:
  • Small icon (24 x 24dp)
  • Title
  • Content text
  • Notifications for pre-Gingerbread devices must have a content intent
  • Anything else is optional

Building a Big view notification

  • Create a normal view notification
  • Then call Builder.setStyle() with a big view style object (such as Big picture, Big text or Inbox) as the argument
  • This has only been available since Jelly Bean. You can still send these notifications on earlier devices but they will display as normal notifications.

It's got to do something! Actions

Usually notifications do something when touched. They perform an action, like opening another activity.

The action is defined by a pending intent containing an intent that starts the activity.

You can have a number of actions and since Jelly Bean, you can include action buttons.

Your notification should have at least one action.

Updating notifications

You should avoid sending multiple notifications for the same event. Rather update the existing notification.

Update existing notifications by building a new notification. Include the updated information and send it using the same ID as the existing notification. It will replace the existing notification if it exists or display a new one.

Cancel notifications

You can cancel notifications by:

  • Setting autoCancel(true) when you build it. This ensures that the notification is cancelled when touched
  • Selecting Clear on the device
  • Calling cancel() in your code, passing the notification ID as parameter
  • Calling cancelAll() in your code to cancel all notifications

Are you lost? Preserving navigation

Did you know?

When you start activities in your app, they run in the same task. Using the back key navigates back across these activities.

You can use the recents key to access previously run activities. This is done in a new task.

Pressing the back key should not cross between these tasks.

As of Android 3.0 the system creates a backstack history which enables you to navigate out of this new task back to the previous task’s home screen.

The TaskStackBuilder provides a way for older devices (pre Android 3.0) to correctly navigate between tasks as described above.

Starting a new activity from the notification

A new activity started from a notification, should start in a new task.

There are two types of activities that you could start like this. In each case the pending intent is set up differently to preserve the normal back behaviour:

Regular activity

These are activities that are part of the apps normal workflow. Here the pending intent should reproduce the apps normal back behaviour. Pressing back takes the user back to the apps Home screen.

Special activity

These activities are only seen when they are accessed from the notification. They typically extend the notification, displaying information that cannot be included in the notification itself. There is no need to create a back stack. Pressing back takes the user back to the apps Home screen.

In the second part, You can master notifications. It's easy! Part 2: Tutorials, we put everything together and build a small app that triggers normal view and Big Picture notifications.