From 6cf59a3edb09c2edbf483b3737a02570603ef659 Mon Sep 17 00:00:00 2001 From: Robert Ly Date: Sun, 11 May 2014 11:25:35 -0700 Subject: docs: updates for changes to notification APIs Change-Id: I8a016b0693e928eb864065bea1079be824987340 --- docs/html/wear/notifications/creating.jd | 86 +++++++++++++---------- docs/html/wear/notifications/pages.jd | 28 ++++---- docs/html/wear/notifications/remote-input.jd | 100 +++++++++++++++++---------- docs/html/wear/notifications/stacks.jd | 56 ++++++++------- 4 files changed, 162 insertions(+), 108 deletions(-) (limited to 'docs') diff --git a/docs/html/wear/notifications/creating.jd b/docs/html/wear/notifications/creating.jd index ce9e117..a5d7da7 100644 --- a/docs/html/wear/notifications/creating.jd +++ b/docs/html/wear/notifications/creating.jd @@ -44,8 +44,8 @@ library and the Developer Preview support library. So to get started, you should include the following imports in your project code:

-import android.preview.support.wearable.notifications.*;
-import android.preview.support.v4.app.NotificationManagerCompat;
+import android.support.wearable.notifications.*;
+import android.support.wearable.app.NotificationManagerCompat;
 import android.support.v4.app.NotificationCompat;
 
@@ -64,7 +64,7 @@ such as action buttons and large icons, while remaining compatible with Android

For example, here's some code that creates and issues a notification using the {@link android.support.v4.app.NotificationCompat} APIs combined with the new - + NotificationManagerCompat API:

@@ -206,54 +206,70 @@ Wear.

you can add additional pages of content that users can view by swiping to the left, or add the ability for users to deliver your app a text response using voice input.

-

To use these new APIs, pass your instance of -{@link android.support.v4.app.NotificationCompat.Builder} to the - WearableNotifications.Builder() constructor. You can then add new -features to your notification using the - WearableNotifications.Builder methods. For example:

+

To use these new APIs:

+ +
    +
  1. Create an instance of +{@link android.support.v4.app.NotificationCompat.Builder}, setting the +desired properties for your notification.
  2. +
  3. Create a + WearableNotificationOptions.Builder, setting the wearable-specific options for the notication.
  4. +
  5. Call WearableNotificationOptions.Builder.applyTo() + , passing in the {@link android.support.v4.app.NotificationCompat.Builder}. This applies + the wearable options to the notification.
  6. +
+ +

+For example, the following code calls the + + setHintHideIcon() method to remove the app icon from the notification card. +

 // Create a NotificationCompat.Builder for standard notification features
-NotificationCompat.Builder notificationBuilder =
-        new NotificationCompat.Builder(mContext)
-        .setContentTitle("New mail from " + sender.toString())
-        .setContentText(subject)
-        .setSmallIcon(R.drawable.new_mail);
-
-// Create a WearablesNotification.Builder to add special functionality for wearables
-Notification notification =
-        new WearableNotifications.Builder(notificationBuilder)
-        .setHintHideIcon(true)
-        .build();
+ NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
+         .setContentTitle("New mail from " + sender)
+         .setContentText(subject)
+         .setSmallIcon(R.drawable.new_mail);
+// Create a WearablesNotificationOptions.Builder to add functionality for wearables
+ Notification notif = new WearableNotificationOptions.Builder()
+         .setHintHideIcon(true)
+         .build()
+         .applyTo(builder); //apply wearable options to to the original notification
+         .build()
 
-

The - setHintHideIcon() method removes your app icon from the notification card. - This method is just one example of new notification features available from the - WearableNotifications.Builder class.

+

The + + setHintHideIcon() method is just one example of new notification features available with the + WearableNotificationOptions.Builder class. +

+ -

When you want to deliver your notifications, be certain to always use the - - NotificationManagerCompat API:

+

When you want to deliver your notifications, always use the + + NotificationManagerCompat API instead of + {@link android.app.NotificationManager}:

 // Get an instance of the NotificationManager service
 NotificationManagerCompat notificationManager =
         NotificationManagerCompat.from(this);
 
-// Build the notification and issues it with notification manager.
-notificationManager.notify(notificationId, notification);
+// Issue the notification with notification manager.
+notificationManager.notify(notificationId, notif);
 
-

If you instead use the framework's {@link android.app.NotificationManager}, some -features from WearableNotifications.Builder -will not work.

+ +

If you use the framework's {@link android.app.NotificationManager}, some +features from WearableNotificationOptions.Builder +do not work.

+

To continue enhancing your notifications for wearables using - WearableNotifications.Builder and other APIs in the + WearableNotificationOptions.Builder and other APIs in the preview support library, see the following developer guides:

diff --git a/docs/html/wear/notifications/pages.jd b/docs/html/wear/notifications/pages.jd index 558f7b8..7d18b3f 100644 --- a/docs/html/wear/notifications/pages.jd +++ b/docs/html/wear/notifications/pages.jd @@ -15,14 +15,19 @@ multiple pages, see the Design Principles of Android Wear.

- -

When creating a notification with multiple pages, start by creating the main notification -(the first page) the way you'd like the notification to appear on a phone -or tablet. Then, add pages one at a time with the - +

To create a notification with multiple pages:

+
    +
  1. Create the main notification (the first page) the way you'd like the notification to appear on a phone + or tablet.
  2. +
  3. Add pages one at a time with the + addPage() method, or add multiple pages in a {@link java.util.Collection} with the - -addPages() method.

    + +addPages() method.
  4. +
  5. Apply the pages to the main notification with the + applyTo() method.
  6. +

For example, here's some code that adds a second page to a notification:

@@ -47,15 +52,14 @@ Notification secondPageNotification = .setStyle(secondPageStyle) .build(); -// Create main notification and add the second page +// Add second page with wearable options and apply to main notification Notification twoPageNotification = - new WearableNotifications.Builder(notificationBuilder) + new WearableNotificationsOptions.Builder() .addPage(secondPageNotification) + .build() + .applyTo(notificationBuilder) .build(); - - - diff --git a/docs/html/wear/notifications/remote-input.jd b/docs/html/wear/notifications/remote-input.jd index 1668363..4db8274 100644 --- a/docs/html/wear/notifications/remote-input.jd +++ b/docs/html/wear/notifications/remote-input.jd @@ -25,16 +25,16 @@ you must type text replies into the voice input field, so be sure you have enabl

Define the Remote Input

To create an action that supports voice input, first create an instance of - + RemoteInput using the - RemoteInput.Builder APIs. + RemoteInput.Builder APIs. The - RemoteInput.Builder constructor takes a string that the system + RemoteInput.Builder constructor takes a string that the system will use as a key for the {@link android.content.Intent} extra that carries the reply message to your app on the handheld.

For example, here's how to create a new - + RemoteInput object that provides a custom label for the voice input prompt:

@@ -56,7 +56,7 @@ RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)

In addition to allowing voice input, you can provide up to five text responses that the user can select for quick replies. Call - setChoices() and pass it a string array.

+ setChoices() and pass it a string array.

For example, you may define some responses in a resource array:

@@ -73,7 +73,7 @@ RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)

Then, inflate the string array and add it to the - RemoteInput:

+ RemoteInput:

 String replyLabel = getResources().getString(R.string.reply_label);
@@ -93,8 +93,8 @@ RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
 

If "Reply" is your notification's primary action (defined by the {@link android.support.v4.app.NotificationCompat.Builder#setContentIntent setContentIntent()} method), then you should attach the - RemoteInput to the main action using - + RemoteInput to the main action using + addRemoteInputForContentIntent(). For example:

@@ -116,18 +116,19 @@ RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
         .setLabel(replyLabel)
         .build();
 
-// Create wearable notification and add remote input
+// Add remote input to wearable options and apply to notification
 Notification replyNotification =
-        new WearableNotifications.Builder(replyNotificationBuilder)
+        new WearableNotificationOptions.Builder()
         .addRemoteInputForContentIntent(remoteInput)
+        .build()
+        .applyTo(replyNotificationBuilder)
         .build();
 
-

By using - + addRemoteInputForContentIntent() to add the - RemoteInput object to the notification's primary action, + RemoteInput object to the notification's primary action, the button that normally appears as an "Open" action becomes the "Reply" action and starts the voice input UI when users select it on Android Wear.

@@ -137,14 +138,14 @@ and starts the voice input UI when users select it on Android Wear.

If the "Reply" action is not your notification's primary action and you want to enable voice input for a secondary action, add the - RemoteInput to a new action button defined by an - + RemoteInput to a new action button defined by an + Action object.

You should instantiate the - -Action with the - Action.Builder() + +WearableAction with the + WearableAction.Builder() constructor, which takes an icon and text label for the action button, plus the {@link android.app.PendingIntent} the system should use to invoke your app when the user selects the action. For example:

@@ -161,7 +162,7 @@ RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY) .build(); // Create the notification action -Action replyAction = new Action.Builder(R.drawable.ic_message, +WearableAction replyAction = new WearableAction.Builder(R.drawable.ic_message, "Reply", pendingIntent) .addRemoteInput(remoteInput) .build(); @@ -169,45 +170,72 @@ Action replyAction = new Action.Builder(R.drawable.ic_message,

After you add the - RemoteInput to the - -Action, add the - -Action to the - WearableNotifications.Builder using - addAction(). + RemoteInput to the + +Wearablection, set the + +WearableAction on the + WearableNotifications.Builder using + addAction(). For example:

 // Create basic notification builder
 NotificationCompat.Builder replyNotificationBuilder =
         new NotificationCompat.Builder(this)
-        .setContentTitle("New message");
+                .setContentTitle("New message");
 
 // Create the notification action and add remote input
-Action replyAction = new Action.Builder(R.drawable.ic_message,
+WearableAction replyAction = new WearableAction.Builder(R.drawable.ic_message,
         "Reply", pendingIntent)
         .addRemoteInput(remoteInput)
         .build();
 
 // Create wearable notification and add action
 Notification replyNotification =
-        new WearableNotifications.Builder(replyNotificationBuilder)
-        .addAction(replyAction)
-        .build();
+        new WearableNotificationOptions.Builder()
+                .addAction(replyAction)
+                .build()
+                .applyTo(replyNotificationBuilder)
+                .build();
 
+

Now, when the user selects "Reply" from an Android wearable, the system prompts the user for voice input (and shows the list of pre-defined replies, if provided). Once the user completes a response, the system invokes the {@link android.content.Intent} attached to the action and adds the EXTRA_VOICE_REPLY extra (the string you passed to the - RemoteInput.Builder constructor) - with the user's message as the string value.

- - - + RemoteInput.Builder constructor) + with the user's message as the string value.

+ +

Obtaining the Voice Input as a String

+

To obtain the user's voice input, call +getResultsFromIntent(), +passing in the "Reply" action's intent. This method returns +a {@link android.os.Bundle} that represents the intent's extras. You can then query the +{@link android.os.Bundle} to obtain the user's voice input string. +

+

+The following code shows a method that accepts an intent and returns the voice input string, +which is referenced by the EXTRA_VOICE_REPLY key that is used in the previous examples: +

+
+/**
+ * Obtain the intent that started this activity by calling
+ * Activity.getIntent() and pass it into this method to
+ * get the associated voice input string.
+ */
+private String getMessageText(Intent intent) {
+    Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
+        if (remoteInput != null) {
+            return remoteInput.getString(Intent.EXTRA_VOICE_REPLY);
+        }
+    }
+    return null;
+}
+
diff --git a/docs/html/wear/notifications/stacks.jd b/docs/html/wear/notifications/stacks.jd index a2d34ce..3c3dc09 100644 --- a/docs/html/wear/notifications/stacks.jd +++ b/docs/html/wear/notifications/stacks.jd @@ -16,7 +16,7 @@ are not able to read details from each message on the wearable (they must open y handheld to view more information). So for the wearable device, you should group all the notifications together in a stack. The stack of notifications appears as a single card, which users can expand to view the details from each notification separately. The new - + setGroup() method makes this possible while allowing you to still provide only one summary notification on the handheld device.

@@ -28,21 +28,24 @@ Wear.

Add Each Notification to a Group

To create a stack, call +href="{@docRoot}reference/android/support/wearable/notifications/WearableNotificationOptions.Builder.html#setGroup(java.lang.String, int)"> setGroup() for each notification you want in the stack and specify a -group key. Then call notify() to send it to the wearable.

+group key. Then call notify() to send it to the wearable.

 final static String GROUP_KEY_EMAILS = "group_key_emails";
 
-// Build the notification and pass this builder to WearableNotifications.Builder
+// Build the notification
 NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
          .setContentTitle("New mail from " + sender1)
          .setContentText(subject1)
          .setSmallIcon(R.drawable.new_mail);
 
-Notification notif1 = new WearableNotifications.Builder(builder)
+// Set the group with WearableNotificationOptions.Builder and apply to the notification
+Notification notif1 = new WearableNotificationOptions.Builder()
          .setGroup(GROUP_KEY_EMAILS)
+         .build()
+         .applyTo(builder)
          .build();
 
 // Issue the notification
@@ -52,8 +55,10 @@ notificationManager.notify(notificationId1, notif);
 

Later on, when you create another notification, specify -the same group key. When you call notify(), this notification appears -in the same stack as the previous notification, instead of as a new card:

+the same group key. When you call +notify(), +this notification appears in the same stack as the previous notification, +instead of as a new card:

 builder = new NotificationCompat.Builder(mContext)
@@ -62,8 +67,10 @@ builder = new NotificationCompat.Builder(mContext)
          .setSmallIcon(R.drawable.new_mail);
 
 // Use the same group as the previous notification
-Notification notif2 = new WearableNotifications.Builder(builder)
+Notification notif2 = new WearableNotificationOptions.Builder()
          .setGroup(GROUP_KEY_EMAILS)
+         .build()
+         .applyTo(builder)
          .build();
 
 notificationManager.notify(notificationId2, notif);
@@ -72,7 +79,7 @@ notificationManager.notify(notificationId2, notif);
 

By default, notifications appear in the order in which you added them, with the most recent notification visible at the top. You can define a specific position in the group by passing an order position as the second parameter for +href="{@docRoot}reference/android/support/wearable/notifications/WearableNotificationOptions.Builder.html#setGroup(java.lang.String, int)"> setGroup().

@@ -83,7 +90,7 @@ href="{@docRoot}reference/android/preview/support/wearable/notifications/Wearabl

It's important that you still provide a summary notification that appears on handheld devices. So in addition to adding each unique notification to the same stack group, also add a summary notification, but set its order position to be GROUP_ORDER_SUMMARY.

+href="{@docRoot}reference/android/support/wearable/notifications/WearableNotificationsOptions.html#GROUP_ORDER_SUMMARY">GROUP_ORDER_SUMMARY.

This notification does not appear in your stack of notifications on the wearable, but appears as the only notification on the handheld device.

@@ -92,23 +99,22 @@ appears as the only notification on the handheld device.

Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_large_icon); +// Create an InboxStyle notification builder = new NotificationCompat.Builder(this) + .setContentTitle("2 new messages") .setSmallIcon(R.drawable.ic_small_icon) - .setLargeIcon(largeIcon); - -// Use the same group key and pass this builder to InboxStyle notification -WearableNotifications.Builder wearableBuilder = new WearableNotifications - .Builder(builder) - .setGroup(GROUP_KEY_EMAILS, - WearableNotifications.GROUP_ORDER_SUMMARY); - -// Build the final notification to show on the handset -Notification summaryNotification = new NotificationCompat.InboxStyle( - wearableBuilder.getCompatBuilder()) - .addLine("Alex Faaborg Check this out") - .addLine("Jeff Chang Launch Party") - .setBigContentTitle("2 new messages") - .setSummaryText("johndoe@gmail.com") + .setLargeIcon(largeIcon) + .setStyle(new NotificationCompat.InboxStyle() + .addLine("Alex Faaborg Check this out") + .addLine("Jeff Chang Launch Party") + .setBigContentTitle("2 new messages") + .setSummaryText("johndoe@gmail.com")); + +// Specify the notification to be the group summary +Notification summaryNotification = new WearableNotificationOptions.Builder() + .setGroupSummary(GROUP_KEY_EMAILS) + .build() + .applyTo(builder) .build(); notificationManager.notify(notificationId3, summaryNotification); -- cgit v1.1