Using the Notification Manager to Create Your First Notification

The notification manager allows you to interface with Android's notification mechanism. Notifications appear in the status bar at the bottom of the device screen. Working with the NotificationManager is as simple as asking the current context for it. If you are within an activity, the code is as follows:

NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

This line of code obtains the NotificationManager object from the getSystemService() call.

The Task Reminder application needs a way to notify the user that a task needs attention. A notification would show up when the alarm goes off for that particular task. To set this notification in the status bar, you need to use the NotificationManager object.

In the doReminderWork() method of the ReminderService class, type the code as shown in Listing 16-1.

Listing 16-1: Implementation of doReminderWork()

Long rowId = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID);                                                             →1

NotificationManager mgr =
               (NotificationManager)getSystemService(NOTIFICATION_SERVICE);                                                        →4

Intent notificationIntent = new Intent(this, ReminderEditActivity.class);                                                          →6
notificationIntent.putExtra(RemindersDbAdapter.KEY_ROWID, rowId);                                                                  →7

PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);                            →9 Notification note=new Notification(android.R.drawable.stat_sys_warning, getString(R.string.notify_new_task_message), System.currentTimeMillis()); ...

Get Android™ Tablet Application Development For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.