Technology Moves as Fast as We Move It by Brian Buikema
Calling Android’s Notification Service (Review and Example)
The handy class below sends a notification to Android’s Notification Service as follows:
- Creates a notification containing the number of alerts , a ticker message, and associated icon.
- If parameter set, flashes the led in a chosen color.
- If parameter set, vibrates the phone.
It is meant to be called periodically as alerts are required, e.g., from a thread that periodically wakes up and checks for alerts.
Keep in mind that you can control the led flashing and vibrating. This is the case where the user has not viewed recent notifications and we only want to update the number of alerts listed in the icon per determining that more alerts are available, but do not want more flashing and vibrating (so we set the flashLed and vibrate parameters to false). Remember that too much notification can be an annoyance to users.
public class NotifierHelper implements IDebugSwitch {
private static final int NOTIFY_1 = 0x1001;
public static void sendNotification(Activity caller, Class<?> activityToLaunch, String title, String msg, int numberOfEvents, boolean flashLed, boolean vibrate) {
NotificationManager notifier = (NotificationManager) caller.getSystemService(Context.NOTIFICATION_SERVICE);
// Create this outside the button so we can increment the number drawn over the notification icon.
// This indicates the number of alerts for this event.
final Notification notify = new Notification(R.drawable.android_32, "", System.currentTimeMillis());
notify.icon = R.drawable.android_32;
notify.tickerText = "New Alerts from Your App!";
notify.when = System.currentTimeMillis();
notify.number = numberOfEvents;
notify.flags |= Notification.FLAG_AUTO_CANCEL;
if (flashLed) {
// add lights
notify.flags |= Notification.FLAG_SHOW_LIGHTS;
notify.ledARGB = Color.CYAN;
notify.ledOnMS = 500;
notify.ledOffMS = 500;
}
if (vibrate) {
// add vibs
if (debug) Log.i("DEBUG", ">>>>> ViBrAtInG. >>>>>");
notify.vibrate = new long[] {100, 200, 200, 200, 200, 200, 1000, 200, 200, 200, 1000, 200};
}
Intent toLaunch = new Intent(caller, activityToLaunch);
PendingIntent intentBack = PendingIntent.getActivity(caller, 0, toLaunch, 0);
notify.setLatestEventInfo(caller, title, msg, intentBack);
notifier.notify(NOTIFY_1, notify);
}
public static void clear(Activity caller) {
NotificationManager notifier = (NotificationManager) caller.getSystemService(Context.NOTIFICATION_SERVICE);
notifier.cancelAll();
}
}
Note the use of IDebugSwitch interface.
In the near future, I will post how to write a simple notification service that uses the NotificationHelper class. I will discuss topics such as disabling alert checking while notifications are being reviewed by the user, and determining that more alerts are available while current notifications are still pending (i.e., the user has not yet reviewed them). Therefore, we will not want to flash the led and vibrate the phone as we earlier touched in this post. Basically we will create a slick way to determine pending alerts.
| Print article | This entry was posted by Brian Buikema on March 5, 2010 at 12:02 PM, and is filed under Android, Mobile. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 2 years ago
I have to say, every time I come to blog.brianbuikema.com there is another remarkable article to read. One of my friends was telling me about this topic a couple weeks ago. I think I’ll send my friend the url here and see what they say.
about 2 years ago
very well
information you write it very clean. I’m very lucky to get this information from you. RFID Reader
about 2 years ago
Great article, thank you very much! .I bookmarked your site!unlock iphone
about 2 years ago
Of course, what a great site and informative posts, I will add backlink – bookmark this site? Regards, Reader.
about 2 years ago
Nice content. Thank you for your information.
about 2 years ago
wonderful share, great article, very usefull for me…thanks
about 2 years ago
great experience, dude! thanks for this great post wow… it’s very wonderful report.
about 2 years ago
It’s really well done! Respect to author.
about 2 years ago
It’s really well done! Respect to author.
about 2 years ago
I bookmarked this link . Thank you for good job !
about 2 years ago
Amazing blog post, this is very similar to a site that I have.
Please check it out sometime and feel free to leave me a comenet on it and tell me what you think. Im always looking for feedback.