diff --git a/docs/downloads/training/NotifyUser.zip b/docs/downloads/training/NotifyUser.zip new file mode 100644 index 0000000000000..c335157db6e55 Binary files /dev/null and b/docs/downloads/training/NotifyUser.zip differ diff --git a/docs/html/images/training/notifications-bigview.png b/docs/html/images/training/notifications-bigview.png new file mode 100644 index 0000000000000..83a5610c93c20 Binary files /dev/null and b/docs/html/images/training/notifications-bigview.png differ diff --git a/docs/html/images/training/notifications-normalview.png b/docs/html/images/training/notifications-normalview.png new file mode 100644 index 0000000000000..06ea970496d72 Binary files /dev/null and b/docs/html/images/training/notifications-normalview.png differ diff --git a/docs/html/training/notify-user/build-notification.jd b/docs/html/training/notify-user/build-notification.jd new file mode 100644 index 0000000000000..ba6602884848d --- /dev/null +++ b/docs/html/training/notify-user/build-notification.jd @@ -0,0 +1,160 @@ +page.title=Building a Notification +parent.title=Notifying the User +parent.link=index.html + +trainingnavtop=true +next.title=Preserving Navigation when Starting an Activity +next.link=navigation.html + +@jd:body + +
+
+ + +

This lesson teaches you to

+
    +
  1. Create a Notification Builder
  2. +
  3. Define the Notification's Action
  4. +
  5. Set the Notification's Click Behavior
  6. +
  7. Issue the Notification
  8. +
+ + +

You should also read

+ + + + +
+
+ + +

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.

+ +

Create a Notification Builder

+ +

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!");
+
+ +

Define the Notification's Action

+ + +

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
+);
+
+ +

Set the Notification's Click Behavior

+ +

+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);
+ +

Issue the Notification

+ +

To issue the notification:

+ + + + + - -