Posted on by & filed under Content - Highlights and Reviews, Mobile Development.

A fundamental reason for Android’s success is due to its true multitasking and the central notification system in which users can see important messages from applications. Notifications have been a cornerstone of the Android operating system since its inception and have just received a facelift. With Android 4.1 Jelly Bean, the latest iteration of the Android Operating System, Google has tweaked the notification system giving developers a great deal more flexibility in presenting information to the user.

The newest platform update allows developers to implement one of three new styles:

  • Inbox (a multi-lined list)
  • BigPicture (preview of a bitmap)
  • BigText (large text support)
    .

Each of these new styles has two states, expanded and compressed: expanded showing the entire contents of the notification, and compressed being very similar to the older style notifications with only a title and one line of content showing.

Custom layout

As with previous versions of the Notification system, developers can still supply their own custom view. It’s a powerful feature as the notifications are at the heart of Android – having a consistent look and feel to such a core aspect of the UI is important and this has made them easy to use. Google has done well with keeping the UI consistent with the implementation of the new Notifications and although custom notifications haven’t become popular so far, hopefully developers will respect this need for consistency and won’t go crazy implementing their own layouts.

Actions

On top of the new styles and custom layouts, developers can implement actions that appear as buttons at the bottom of the notification. This allows developers to put up to three different actions for the user to interact with. If more actions are needed, the switch-a-roo method can provide more than three actions by supplying a more action that replaces the notification with an updated version of different supplied actions – effectively allowing either a scrolling list, or a hierarchal tree of actions.

Implementation

Implementing the old style notifications is a trivial exercise.

Get a reference to the NotificationManager:

Instantiate the Notification:

Define the notification’s message and PendingIntent:

Pass the Notification to the NotificationManager:

Implementing the new Jelly Bean notifications is actually easier thanks to the new supplied ‘re-builder’ classes. For example, to create a new Inbox style notification do the following.

Get a reference to the NotificationManager:

Define the PendingIntent:

Create a base Notification:

Create the extended Notification:

Pass the Notification to the NotificationManager:

Sample source code for each new notification style is given on the android.com developer reference site, but implementing the new methods will cause your application to crash if used on older versions of the OS, as they are currently only for API versions greater or equal to 16. It’s therefore recommended to use Jake Wharton’s NotificationCompat2 library to provide backward compatibility across all versions of the Android OS. Jake has made it easy to implement a version of the above code that is compatible across all versions of the Android OS.

Get a reference to the NotificationManager:

Define the PendingIntent:

Create a base notification:

Create the extended notification and pass to the NotificationManager:

Nick Butcher, one of Google’s UK Android developer advocates, mentions that future native support may be on the cards within the Android support library. However, at the time of writing there is nothing planned, so in the meantime Jake Wharton’s compatibility library is the easiest way to write code that will support all versions of the Android OS.

Safari Books Online has the content you need

Check out these Android books available from Safari Books Online:

Android in Action, Third Edition is a fast-paced book that puts you in the driver’s seat–you’ll master the SDK, build WebKit apps using HTML 5, explore cross-platform graphics with RenderScript, learn to use Native Development Kit, and master important tablet concepts like drag-and-drop, fragments, and the Action Bar, all new in Android 3.
Beginning Android 4 is fresh with details on the latest iteration of the Android platform. Begin at the beginning by installing the tools and compiling a skeleton app. Move through creating layouts, employing widgets, taking user input, and giving back results.
Android in Practice is a treasure trove of Android goodness, with over 90 tested, ready-to-use techniques including complete end-to-end example applications and practical tips for real world mobile application developers. The book dives into important topics like multitasking and services, testing and instrumentation, building and deploying applications, and using alternative languages.
Android UI Fundamentals: Develop and Design walks developers through the different choices available on their way to creating a well-designed application for Android. While building a simple application, Jason works through the basics of Android UI development including layout, event handling, menus and notifications.
Programming Android shows experienced application developers what they need to program for the Android operating system — the core building blocks, how to put those blocks together, and how to build compelling apps that work on a full range of Android devices.
Whether you want to develop a commercial application for mobile devices, or just want to create a mobile mashup for personal use, Android Application Development demonstrates how you can design, build, and test applications for the new mobile market

About this author

Martyn Haigh has been a coder since his dad showed him a BBC Micro at the age of 3. He has a degree in Computer Science, likes clean code and has been programming Android devices for longer than it’s been commercially viable. In his spare time he snowboards, slays monsters and has been the Times Magazine person of the year on three different occasions. Sometimes he writes things on www.martynhaigh.com. He’d love you to visit.

Tags: android, Android 4.1, Jelly Bean, Notifications,

Trackbacks/Pingbacks

  1.  » Guest blog on Safari books dev blog things that fell out of martyn's brain