1page.title=Bridging Mode for Notifications 2meta.keywords="wear-preview" 3page.tags="wear-preview" 4 5@jd:body 6 7 <div id="qv-wrapper"> 8 <div id="qv"> 9 <ul> 10 <li> 11 <a href= 12 "#using-an-entry-in-the-manifest-file">Specifying a Bridging Configuration in the Manifest File</a> 13 </li> 14 15 <li> 16 <a href= 17 "#specifying-a-bridging-configuration-at-runtime">Specifying a Bridging Configuration at Runtime</a> 18 </li> 19 <li> 20 <a href= 21 "#existing-method-of-preventing-bridging">Existing Method of Preventing Bridging</a> 22 </li> 23 24 <li> 25 <a href= 26 "#using_a_dismissal_id_to_sync_notification_dismissals">Using a Dismissal ID to Sync Notification Dismissals</a> 27 </li> 28 </ul> 29 </div> 30 </div> 31 32 <p> 33 By default, notifications <a href= 34 "{@docRoot}training/wearables/notifications/index.html">are bridged 35 (shared)</a> from an app on a companion phone to the watch. If you build 36 a standalone watch app and have a companion phone app, they may duplicate 37 notifications. The Android Wear 2.0 Preview includes 38 features to handle this problem of repeated notifications. 39 </p> 40 41 <p> 42 With the Android Wear 2.0 Preview, developers can change the behavior of 43 notifications with one or more of the following: 44 </p> 45 46 <ul> 47 <li>Specifying a bridging configuration in the manifest file 48 </li> 49 50 <li>Specifying a bridging configuration at runtime 51 </li> 52 53 <li>Setting a dismissal ID so notification dismissals are synced across 54 devices 55 </li> 56 </ul> 57 58 <h2 id="using-an-entry-in-the-manifest-file"> 59 Specifying a Bridging Configuration in the Manifest File 60 </h2> 61 62 <p> 63 An app's Android manifest file can indicate that notifications from the 64 corresponding phone app should not be bridged to the watch. Specifically, 65 to prevent bridging of notifications from a phone app, you can use a 66 <code><meta-data></code> 67 entry in the manifest file of the watch app (e.g. the standalone watch 68 app), as follows: 69 </p> 70 71<pre> 72com.google.android.wearable.notificationBridgeMode 73</pre> 74 75 <p> 76 Setting that entry to <code>NO_BRIDGING</code> will prevent bridging: 77 </p> 78 79<pre> 80<meta-data android:name="com.google.android.wearable.notificationBridgeMode" 81 android:value="NO_BRIDGING" /> 82</pre> 83 84 <p> 85 The default bridging behavior occurs if you do not 86 include the <code><meta-data></code> entry or 87 if you specify a value of <code>BRIDGING</code> instead of 88 <code>NO_BRIDGING</code>. 89 </p> 90 91 <p> 92 For an existing app, if you are using 93 Google Cloud Messaging (GCM) or Firebase Cloud 94 Messaging (FCM) to send notification alerts to devices, 95 you may already have disabled bridging in case a phone is not 96 connected at the time of receiving an alert. 97 In this case, you may still want to dismiss the notification 98 across other devices when it is dismissed in a watch app. 99 </p> 100 101 <p> 102 The bridging configuration that is set in the manifest takes effect as 103 soon as a watch app is installed. 104 </p> 105 106 <h2 id="specifying-a-bridging-configuration-at-runtime"> 107 Specifying a Bridging Configuration at Runtime 108 </h2> 109 110 <p> 111 This section describes how to specify a bridging configuration at runtime 112 using the <code>BridgingManager</code> class 113 <code>(android.support.wearable.notifications.BridgingManager)</code>. 114 </p> 115 116 <p> 117 You can set a bridging mode, and optionally set tags for notifications 118 that are exempt from the bridging mode, using a 119 <code>BridgingManager</code> object. Specifically, create a 120 <code>BridgingConfig</code> object and set it as shown in this section, 121 optionally using the <code>setBridgingEnabled</code> method. If you 122 specify a bridging configuration at runtime, then if the 123 <code>setBridgingEnabled</code> method is not set, bridging is enabled by 124 default. 125 </p> 126 127 <p> 128 Specifying a bridging configuration at runtime overrides a 129 bridging-related setting in the Android manifest file. 130 </p> 131 132 <h3 id="disable-bridging-for-all-notifications"> 133 Disable bridging for all notifications 134 </h3> 135 136 <p> 137 You can use the <code>setBridgingEnabled</code> method, as follows: 138 </p> 139 140<pre> 141BridgingManager.setConfig(context, 142 new BridgingConfig.Builder(context) 143 .setBridgingEnabled(false) 144 .build()); 145</pre> 146 <p> 147 If the above setter is not called, the bridging mode defaults to true. 148 Here is an example of setting tags without using the 149 <code>setBridgingEnabled</code> method, excluding notifications with a 150 tag of <code>foo</code> or <code>bar</code>: 151 </p> 152 153<pre> 154BridgingManager.setConfig(context, 155 new BridgingConfig.Builder(context) 156 .addExcludedTag("foo") 157 .addExcludedTag("bar") 158 .build()); 159</pre> 160 <h3 id="exempt-notifications-that-are-tagged"> 161 Exempt notifications that are tagged 162 </h3> 163 164 <p> 165 You can disable bridging for all notifications except those with certain 166 tags. 167 </p> 168 169 <p> 170 For example, you can disable bridging, except for notifications tagged as 171 <code>foo</code> or <code>bar,</code> with the following: 172 </p> 173 174<pre> 175BridgingManager.setConfig(context, 176 new BridgingConfig.Builder(context) 177 .setBridgingEnabled(false) 178 .addExcludedTag("foo") 179 .addExcludedTag("bar") 180 .build()); 181</pre> 182 183 <p> 184 As another example, you can disable bridging for all notifications except 185 for notifications tagged as <code>foo</code>, <code>bar</code> or 186 <code>baz</code>. 187 </p> 188 189 <pre> 190BridgingManager.setConfig(context, 191 new BridgingConfig.Builder(context) 192 .setBridgingEnabled(false) 193 .addExcludedTags(Arrays.asList("foo", "bar", "baz")) 194 .build()); 195</pre> 196 <h3 id="enable-bridging-except-for-notifications-with-certain-tags"> 197 Enable bridging except for notifications with certain tags 198 </h3> 199 200 <p> 201 You can enable bridging for all notifications except those with certain 202 tags. 203 </p> 204 205 <p> 206 For example, you can enable bridging for all notifications, except for 207 notifications tagged as <code>foo</code> or <code>bar</code>, with the 208 following: 209 </p> 210 211<pre> 212BridgingManager.setConfig(context, 213 new BridgingConfig.Builder(context) 214 .setBridgingEnabled(true) 215 .addExcludedTag("foo") 216 .addExcludedTag("bar") 217 .build()); 218</pre> 219 220 <h3 id="setting-a-bridge-tag"> 221 Setting a bridge tag 222 </h3> 223 224 <p> 225 A bridge tag can be set on a notification by calling the 226 <code>setNotificationBridgeTag</code> method as follows: 227 </p> 228 229<pre> 230BridgingManager.setNotificationBridgeTag(<NotificationCompat.Builder>, <String>); 231</pre> 232 233 <p> 234 For example: 235 </p> 236 237<pre> 238NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 239<set other fields>; 240BridgingManager.setNotificationBridgeTag(builder, "foo"); 241Notification notification = builder.build(); 242</pre> 243 244 <h2 id="existing-method-of-preventing-bridging"> 245 Existing Method of Preventing Bridging 246 </h2> 247 248 <p> 249 An existing way to prevent bridging is with the 250 <code>Notification.Builder</code> class; specify <code>true</code> in the 251 <a href= 252 "http://developer.android.com/reference/android/app/Notification.Builder.html#setLocalOnly(boolean)"> 253 setLocalOnly</a> method. 254 </p> 255 256 <p> 257 However, this way to prevent bridging may not be preferable. For example, 258 if a user installs a phone app but not the corresponding watch app, the 259 <code>setLocalOnly</code> method could prevent the bridging of helpful 260 notifications. Additionally, users may have multiple paired watches and 261 the watch app may not be installed on all of them. 262 </p> 263 264 265 <h2 id="using_a_dismissal_id_to_sync_notification_dismissals"> 266 Using a Dismissal ID to Sync Notification Dismissals 267 </h2> 268 269 <p> 270 If you prevent bridging with the Bridging mode feature, dismissals 271 (cancellations) of notifications are not synced across a user's devices. 272 However, the following methods of the <a href= 273 "http://developer.android.com/reference/android/support/v4/app/NotificationCompat.WearableExtender.html"> 274 NotificationCompat.WearableExtender</a> class enable you to use dismissal 275 IDs: 276 </p> 277 278 <pre> 279public WearableExtender setDismissalId(String dismissalId) 280public String getDismissalId() 281</pre> 282 <p> 283 To enable a dismissal to be synced, use the <code>setDismissalId()</code> 284 method. For each notification, pass a globally unique ID, as a string, 285 when you call the <code>setDismissalId()</code> method. When the 286 notification is dismissed, all other notifications with the same 287 dismissal ID are dismissed on the watch(es) and on the companion phone. 288 To retrieve a dismissal ID, use <code>getDismissalId()</code>. 289 </p> 290 291 <p> 292 In the following example, syncing of dismissals is enabled because a 293 globally unique ID is specified for a new notification: 294 </p> 295 296 <pre> 297NotificationCompat.WearableExtender wearableExtender = 298new NotificationCompat.WearableExtender().setDismissalId("abc123"); 299Notification notification = new NotificationCompat.Builder(context) 300<set other fields> 301.extend(wearableExtender) 302.build(); 303</pre> 304 <p> 305 Dismissal IDs work if a watch is paired to an Android phone, but not if a 306 watch is paired to an iPhone. 307 </p> 308