Notifying the User in Android

Wow, what a month November was, epically busy I was with various things both professionally and personally.  First, congratulations are in order to the organizing team for CodeMash; it sold 700 tickets in 3.5 days, absolutely destroying the original record of 31 days for the 2010 CodeMash.  Unfortunately, I was not selected to give my Android talk at the conference, though I do plan to still attend (I was one of the lucky ones who got a ticket during the small window they were available).

I am pleased to say that I was selected to present my Android talk in print via the SmashCode magazine, a magazine whose purpose it is to supplement the attendees of CodeMash.  At the time of this writing, I am preparing to review the second draft of my article, we are very close to completion, so look for it if you are one of those attending the conference in January.

Along the lines of Android, today I would like to talk about a topic of great use: notifications, more specifically, how to raise a notification using the AlarmManager.

First it is important to understand the flow for this process:

  1. Some action is taken in code to create an alarm
  2. The alarm will “go off” at some point in time
  3. The alarm will send an intent that the Android system will hear via a listening receiver
  4. The receiver will create a notification that the user can then respond to
  5. The user responding to the event sends an intent that can achieve just about any end

To begin with, we need to create some code that can create the alarm – below is an excerpt from an application I am writing.


The key part of this code is the interaction with the AlarmManager. First look at line 36, the set operation for an alarm.  The first parameter here indicates what type of alarm this is; RTC_WAKEUP states that the alarm should wake the device if it is sleeping, other options are documented here.  The second parameter indicates when the alarm should go off, in milliseconds from the current time.  Here we are using the Calendar class to set the time for 30 seconds from now.  Finally, we send a “pending intent” object, which wraps our original intent (defined on line 28).

Ok, so lets look at this more closely, effectively we have defined an alarm that, at a given point in time, will send an “intent” to android, as we have defined.  Our application will see this intent via this line in our Manifest file:


This defines that this receiver should expect to receive and intent of this type.  Receiver as a special type in Android that expect to receive an intent from somewhere.  Ours will inherit from BroadcastReceiver and is responsible for actually creating the notification the user will see on their phone.


Ok so there is quite a bit going on here.  First, Android features a class known as the NotificationManager, this class instance will be responsible for “notifying” the user.  Something to point out here is the use of the FLAG_AUTO_CANCEL flag.  This will automatically cause the notification to be cleared when the user selects it.  Leaving this off will cause the notification to sit in the status bar uncleared, unless you clear it manually.  Notice the first parameter to notifiy(), this is the notification code that you would be referencing to clear.

More Intents and PendingIntents, though in this case we are just transferring an object passed with the original intent to the new intent; this new intent will be used to respond to the user acknowledging the notification.  In this case, we are taking the user to one of the activities in our App.

SetLastestInfo will define what our notification will look like; here I am using the basic look.  Finally, we call notify() on the NotificationManager instance which will cause the notification to appear in the status bar and be clickable by the user.  Notice this will happen instantly, hence the reason for using an alarm to initiate it.

So, to conclude, there are times we may want to notify the user of something at a certain time.  The NotificationManager features a notify method to allow us to raise notifications, however, if we want to schedule a notification, then one option is to create an alarm which will use a receiver listening for the intent to create the notification.

Look for this to be used in my upcoming app designed for CodeMash – CodeMashDroid, available near the end of December.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s