page.title=Building a Notification page.tags=notifications helpoutsWidget=true trainingnavtop=true @jd:body
This lesson explains how to create and issue a notification.
The examples in this class are based on the {@link android.support.v4.app.NotificationCompat.Builder} class. {@link android.support.v4.app.NotificationCompat.Builder} is in the Support Library. You should use {@link android.support.v4.app.NotificationCompat} and its subclasses, particularly {@link android.support.v4.app.NotificationCompat.Builder}, to provide the best notification support for a wide range of platforms.
When creating a notification, specify the UI content and actions with a {@link android.support.v4.app.NotificationCompat.Builder} object. At bare minimum, a {@link android.support.v4.app.NotificationCompat.Builder Builder} object must include the following:
For example:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!");
Although actions are optional, you should add at least one action to your notification. An action takes users directly from the notification to an {@link android.app.Activity} in your application, where they can look at the event that caused the notification or do further work. Inside a notification, the action itself is defined by a {@link android.app.PendingIntent} containing an {@link android.content.Intent} that starts an {@link android.app.Activity} in your application.
How you construct the {@link android.app.PendingIntent} depends on what type of {@link android.app.Activity} you're starting. When you start an {@link android.app.Activity} from a notification, you must preserve the user's expected navigation experience. In the snippet below, clicking the notification opens a new activity that effectively extends the behavior of the notification. In this case there is no need to create an artificial back stack (see Preserving Navigation when Starting an Activity for more information):
Intent resultIntent = new Intent(this, ResultActivity.class); ... // Because clicking the notification opens a new ("special") activity, there's // no need to create an artificial back stack. PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT );
To associate the {@link android.app.PendingIntent} created in the previous step with a gesture, call the appropriate method of {@link android.support.v4.app.NotificationCompat.Builder}. For example, to start an activity when the user clicks the notification text in the notification drawer, add the {@link android.app.PendingIntent} by calling {@link android.support.v4.app.NotificationCompat.Builder#setContentIntent setContentIntent()}. For example:
PendingIntent resultPendingIntent; ... mBuilder.setContentIntent(resultPendingIntent);
To issue the notification:
For example:
NotificationCompat.Builder mBuilder; ... // Sets an ID for the notification int mNotificationId = 001; // Gets an instance of the NotificationManager service NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // Builds the notification and issues it. mNotifyMgr.notify(mNotificationId, mBuilder.build());