1page.title=Notification Changes in Android Wear 2.0 2meta.tags="wear", "wear-preview", "notifications" page.tags="wear" 3page.image=/wear/preview/images/expanded_diagram.png 4 5 6@jd:body 7 8<div id="qv-wrapper"> 9 <div id="qv"> 10 <!-- table of contents --> 11 <h2>This document includes</h2> 12 <ol> 13 <li><a href="#visual">Visual Updates</a></li> 14 <li><a href="#inline">Inline Action</a></li> 15 <li><a href="#expanded">Expanded Notifications</a></li> 16 <li><a href="#messaging">MessagingStyle</a></li> 17 </ol> 18 </div> 19</div> 20 21<p>Android Wear 2.0 updates the visual style and interaction paradigm of notifications 22 as well as introduces expanded notifications, which provide substantial additional 23 content and actions in an app-like experience. 24</p> 25 26<p>The visual and interaction changes make it much easier for users to read and 27 interact with notifications from your app. Expanded notifications enable 28 you to deliver Wear users an app-like experience even if you haven't built an 29 Android Wear app. 30</p> 31 32<p class="note"> 33 <strong>Note:</strong> When developing for Wear 2.0, ensure that 34 you have the latest version of the Android Wear app on your phone. 35</p> 36 37<h2 id="visual">Visual Updates</h2> 38<p>Notifications receive important visual updates in Wear 2.0, with 39<a href="http://www.google.com/design/spec-wear"> 40material design</a> visual changes. 41</p> 42 43<p><img src="{@docRoot}wear/preview/images/comparison_diagram.png" /> </p> 44<p><b>Figure 1.</b> Comparison of the same notification in Android Wear 1.x and 2.0.</p> 45 46<p>Some of the visual updates include:</p> 47<ul> 48<li><strong>Updated touch targets of a notification</strong>: 49 If no <a href="{@docRoot}reference/android/app/Notification.html#contentIntent">{@code contentIntent}</a> 50 is set or if the notification is 51 <a href="{@docRoot}design/wear/structure.html#Bridged">bridged</a> 52 from a paired phone, then tapping the notification opens an <a href="{@docRoot}wear/preview/features/notifications.html#expanded">expanded notification</a>. 53 If the notification is generated locally by a Wear app and if a 54 <a href="{@docRoot}reference/android/app/Notification.html#contentIntent">{@code contentIntent}</a> 55 is set, tapping the notification fires the 56 <a href="{@docRoot}reference/android/app/Notification.html#contentIntent">{@code contentIntent}</a>. 57 </li> 58 59<li><strong>Dark background color</strong>: 60 If you have notifications that are bridged to wearables, you need to be careful 61 with regards to using color for the notifications. Since a bridged 62 notification needs to support both light (Wear 1.x) and dark (Wear 2.0) 63 backgrounds, it is unlikely that any colors will work well on both. 64 <a href="{@docRoot}reference/android/app/Notification.WearableExtender.html#setDisplayIntent(android.app.PendingIntent)">{@code DisplayIntent}</a> 65 notifications render with both light and dark backgrounds 66 as well, and need to be checked for the same reason. 67 We recommended that you don't set color for bridged notifications. 68 69 When Wear apps post local notifications, you can work around this by checking 70 <a href="{@docRoot}training/basics/supporting-devices/platforms.html#version-codes">the API level of the device</a> 71 they're running on and using an appropriate color 72 for Wear 1.x and a different color for Wear 2.0. 73</li> 74 75<li><strong>Updated horizontal swipe gesture on a notification</strong>: 76 To dismiss a notification in Wear 2.0, the user swipes horizontally in either 77 direction. So if your notification instructs the user to swipe left or right, 78 you must update the text of your notification. 79</li> 80</ul> 81 82<h2 id="inline">Inline Action</h3> 83 84<img src="{@docRoot}wear/preview/images/inline_action.png" style="float:right;margin:10px 20px 0 0"> 85<p> 86 Wear 2.0 now supports inline action, which allows users to take actions on a 87 notification from within the notification stream card. On Wear, the inline 88 action appears as an additional button displayed at the bottom of the notification. 89</p> 90<p> 91 Inline actions are optional but recommended for cases in which users are likely 92 to take an action on a notification after viewing the contents in the 93 notification stream card (without going to the 94 <a href= "{@docRoot}wear/preview/features/notifications.html#expanded">expanded notification</a>). 95 Examples of good use cases for inline action on a notification include: replying to a 96 text message, stopping a fitness activity, and archiving an email message. 97</p> 98 99<p> 100 A notification can provide only one inline action. 101 To display the inline action as an additional button in the notification, set 102 the <a href="https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Action.WearableExtender.html#setHintDisplayActionInline(boolean)">{@code setHintDisplayActionInline()}</a> 103 method to true. When a user taps the inline action, the system invokes 104 the intent that you specified in the notification action. 105</p> 106 107<h3>Adding an inline action</h3> 108<p> 109 The following code example shows how to create a notification with an inline 110 reply action: 111</p> 112 113<ol> 114 <li>Create an instance of 115 <a href="https://developer.android.com/reference/android/support/v4/app/RemoteInput.Builder.html">{@code RemoteInput.Builder}</a></code> 116 that you can add to your notification action. This class's constructor accepts a 117 string that the system uses as the key for the text input. Later, your app 118 uses that key to retrieve the text of the input. 119 120<pre> 121String[] choices = context.getResources().getStringArray(R.array.notification_reply_choices); 122 choices = WearUtil.addEmojisToCannedResponse(choices); 123 RemoteInput remoteInput = new RemoteInput.Builder(Intent.EXTRA_TEXT) 124 .setLabel(context.getString(R.string.notification_prompt_reply)) 125 .setChoices(choices) 126 .build(); 127</pre> 128 129 </li> 130 131 <li> 132 Use the <a href="https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Action.Builder.html#addRemoteInput(android.support.v4.app.RemoteInput)">{@code addRemoteInput()}</a> 133 method to attach the <ahref="https://developer.android.com/reference/android/support/v4/app/RemoteInput.html">{@code RemoteInput}</a> 134 object to an action. 135 136<pre> 137NotificationCompat.Action.Builder actionBuilder = new NotificationCompat.Action.Builder( 138 R.drawable.ic_full_reply, R.string.notification_reply, replyPendingIntent); 139 actionBuilder.addRemoteInput(remoteInput); 140 actionBuilder.setAllowGeneratedReplies(true); 141</pre> 142 </li> 143 144 <li> 145 Add a hint to display the reply action inline, and use the 146 <a href="https://developer.android.com/reference/android/support/v4/app/NotificationCompat.WearableExtender.html#addAction(android.support.v4.app.NotificationCompat.Action)">{@code addAction}</a> 147 method to add this action to the notification. 148 149<pre> 150// Android Wear 2.0 requires a hint to display the reply action inline. 151 Action.WearableExtender actionExtender = 152 new Action.WearableExtender() 153 .setHintLaunchesActivity(true) 154 .setHintDisplayActionInline(true); 155 wearableExtender.addAction(actionBuilder.extend(actionExtender).build()); 156</pre> 157 </li> 158</ol> 159 160<h2 id="expanded">Expanded Notifications</h2> 161<p>Android Wear 2.0 introduces <i>expanded notifications</i>, which provide 162 substantial additional content and actions for each notification. 163</p> 164<p>When you <a href="{@docRoot}training/wearables/notifications/pages.html">specify additional content pages</a> 165 and actions for a notification, those are available to the user within the 166 expanded notification. Each expanded notification follows 167 <a href="http://www.google.com/design/spec-wear">Material Design for Android Wear</a>, 168 so the user gets an app-like experience. 169</p> 170 171 172<h3 id="expanded">Expanded notifications</h3> 173<p>If the first action in the expanded notification has a 174<a href=" {@docRoot}reference/android/support/v4/app/RemoteInput.html">{@code RemoteInput}</a> 175 (e.g., a Reply action), then the choices you set with <a href="http://developer.android.com/reference/android/support/v4/app/RemoteInput.Builder.html#setChoices(java.lang.CharSequence[])">{@code setChoices()}</a> 176 appear within the expanded notification below the first action. 177</p> 178 179<p>The user can view the expanded notification by tapping on a notification when 180 either of the following is true: 181</p> 182<ul> 183 <li>The notification is generated by an app on the paired phone and 184 bridged to Wear. 185 </li> 186 <li>The notification does not have a 187 <a href="http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setContentIntent(android.app.PendingIntent)">{@code contentIntent}</a>. 188 </li> 189</ul> 190<h3>Best practices for expanded notifications</h3> 191<p>To decide when to use expanded notifications, follow these guidelines:</p> 192<ul> 193 <li>All notifications bridged from the paired phone to the Wear device will 194 use expanded notifications. 195 </li> 196 <li>If a notification is generated by an app running locally on Wear 2.0, 197 you should <a href="{@docRoot}training/notify-user/build-notification.html#action"> 198 make the touch target of your notification </a> launch 199 <a href="{@docRoot}training/notify-user/build-notification.html#action"> an Activity</a> 200 within your app by calling <a href="{@docRoot}reference/android/support/v4/app/NotificationCompat.Builder.html#setContentIntent(android.app.PendingIntent)">{@code setContentIntent()}</a>. 201 We recommend that you do not use expanded notifications for notifications generated 202 by an app running locally on Wear 2.0. 203 </li> 204</ul> 205 206<h3>Adding expanded notifications</h3> 207<p> 208 Expanded Notifications allow you to include additional content and actions 209 for a notification. You choose the level of detail that your app's notifications 210 will provide; however be judicious with the amount of detail you include in a 211 notification. 212</p> 213<img src="{@docRoot}wear/preview/images/expanded_diagram.png" height="340" 214 style="float:left;margin:10px 20px 0 0" /> 215<h4>Adding additional content</h4> 216To show additional content in your expanded notification, see <a href="{@docRoot}training/wearables/notifications/pages.html">Adding Pages to a Notification</a>.</p> 217<p>Additional content pages are stacked vertically in the expanded notification 218 and appear in the order they were added. 219 These additional content pages can optionally use a style such as <a href="{@docRoot}reference/android/support/v4/app/NotificationCompat.BigTextStyle.html">{@code BigTextStyle}</a> or <a href="{@docRoot}reference/android/support/v4/app/NotificationCompat.BigPictureStyle.html">{@code BigPictureStyle}</a>. 220</p> 221<h4>Primary action</h4> 222The expanded notification will contain one primary action, which is the first 223action in the notification unless a different action is specified using 224<a href="{@docRoot}reference/android/support/v4/app/NotificationCompat.WearableExtender.html#setContentAction(int)">{@code setContentAction()}</a>. 225</p> 226<h4>Additional actions</h4> 227<p> 228 To specify additional actions, use 229 <a href="{@docRoot}reference/android/support/v4/app/NotificationCompat.WearableExtender.html#addAction(android.support.v4.app.NotificationCompat.Action)">{@code addAction()}</a> 230 or <a href="{@docRoot}reference/android/support/v4/app/NotificationCompat.WearableExtender.html#addActions(java.util.List<android.support.v4.app.NotificationCompat.Action>)">{@code addActions()}</a>. 231 The action drawer of the expanded notification contains all available actions. 232</p> 233<h2 id="messaging">MessagingStyle</h2> 234 235<p> 236 If you have a chat messaging app, your notifications should use 237 <a href="https://developer.android.com/reference/android/support/v4/app/NotificationCompat.MessagingStyle.html">{@code NotificationCompat.MessagingStyle}</a>, 238 which is new in Android 7.0. Wear 2.0 uses the chat messages included in a 239 {@code MessagingStyle} notification 240 (see <a href="https://developer.android.com/reference/android/support/v4/app/NotificationCompat.MessagingStyle.html#addMessage(android.support.v4.app.NotificationCompat.MessagingStyle.Message)">{@code addMessage()}</a>) 241 to provide a rich chat app-like experience in the expanded notification. 242</p> 243 244<p class="note">Note: <a href="https://developer.android.com/reference/android/support/v4/app/NotificationCompat.MessagingStyle.html">{@code MessagingStyle}</a> 245expanded notifications require that you have at least version 1.5.0.2861804 of the 246 <a href="https://play.google.com/store/apps/details?id=com.google.android.wearable.app">Android Wear app</a> 247 on your paired Android phone. 248</p> 249 250<h3 id="smart-reply">Smart Reply</h3> 251<img src="{@docRoot}wear/preview/images/messaging_style.png" height="420" 252 style="float:right;margin:10px 20px 0 0" /> 253<p>Wear 2.0 also introduces <i>Smart Reply</i> for 254 <a href="https://developer.android.com/reference/android/support/v4/app/NotificationCompat.MessagingStyle.html">{@code MessagingStyle}</a> notifications. 255 Smart Reply provides the user with contextually relevant, touchable choices in 256 the expanded notification and in {@code RemoteInput}. These augment the fixed 257 list of choices that the developer provides in 258 <a href="http://developer.android.com/reference/android/support/v4/app/RemoteInput.html">{@code RemoteInput}</a> 259 using the 260 <a href="{@docRoot}reference/android/support/v4/app/RemoteInput.Builder.html#setChoices(java.lang.CharSequence[])">{@code setChoices()}</a> method. 261</p> 262<p> Smart Reply provides users with a fast (single tap), discreet (no speaking aloud), 263 private (messages received by a user never leave the watch), and reliable (no 264 internet connection needed) way to respond to chat messages. 265</p> 266 267<p> 268 Smart Reply responses are generated by an entirely on-watch machine learning 269 model using the context provided by the MessagingStyle notification. No user 270 notification data is sent to Google servers to generate Smart Reply responses. 271</p> 272 273<p>To enable Smart Reply for your notification action, you need to do the 274following: 275</p> 276<ol> 277 <li>Use <a href="https://developer.android.com/reference/android/support/v4/app/NotificationCompat.MessagingStyle.html">{@code NotificationCompat.MessagingStyle}</a>. 278 </li> 279 <li>Call the method <a href="https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Action.Builder.html#setAllowGeneratedReplies(boolean)">{@code setAllowGeneratedReplies(true)}</a> 280 for the notification action. 281 </li> 282 <li>Ensure that the notification action has a 283 <a href="{@docRoot}reference/android/app/RemoteInput.html">{@code RemoteInput}</a> 284 (where the responses will reside). 285 </li> 286</ol> 287<p>The following example shows how to create a MessagingStyle notification with 288Smart Reply responses.</p> 289<pre> 290// Create an intent for the reply action 291Intent replyIntent = new Intent(this, ReplyActivity.class); 292PendingIntent replyPendingIntent = 293 PendingIntent.getActivity(this, 0, replyIntent, 294 PendingIntent.FLAG_UPDATE_CURRENT); 295 296// Create the reply action and add the remote input 297NotificationCompat.Action action = 298 new NotificationCompat.Action.Builder(R.drawable.ic_reply_icon, 299 getString(R.string.label), replyPendingIntent) 300 .addRemoteInput(remoteInput) 301 302// 1) allow generated replies 303.setAllowGeneratedReplies(true) 304 .build(); 305 306Notification noti = new NotificationCompat.Builder() 307 .setContentTitle(messages.length + " new messages with " + sender.toString()) 308 .setContentText(subject) 309 .setSmallIcon(R.drawable.new_message) 310 .setLargeIcon(aBitmap) 311 // 2) set the style to MessagingStyle 312 .setStyle(new NotificationCompat.MessagingStyle(resources.getString(R.string.reply_name)) 313 .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender()) 314 .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender())) 315 316 317// 3) add an action with RemoteInput 318.extend(new WearableExtender().addAction(action)).build(); 319</pre> 320 321<h3 id="images">Adding images to a MessagingStyle notification</h3> 322<p> 323 You can add images to a notification message by setting the appropriate MIME 324 type and placing the URI to the image in {@code NotificationCompat.MessagingStyle.Message.} 325 <a href="https://developer.android.com/reference/android/support/v4/app/NotificationCompat.MessagingStyle.Message.html#setData(java.lang.String, android.net.Uri)">{@code setData()}</a> method. 326</p> 327<p> 328 Here is the code snippet to set data of type image in a notification: 329</p> 330<pre> 331NotificationCompat.MessagingStyle.Message message = new Message("sticker", 1, "Jeff") 332 .setData("image/png", stickerUri); 333 334 NotificationCompat notification = new NotificationCompat.Builder() 335 .setStyle(new NotificationComapt.MessagingStyle("Me") 336 .addMessage(message) 337 .build()); 338 339</pre> 340<p> 341 In the above code snippet the variable <code>stickerUri </code>is a Uri that 342 points to a publicly-accessible location, as described <a 343 href="https://developer.android.com/reference/android/support/v4/app/NotificationCompat.MessagingStyle.Message.html">here 344 </a>. 345</p>