1page.title=User Notifications 2@jd:body 3 4<div id="qv-wrapper"> 5<div id="qv"> 6 7<h2>Quickview</h2> 8 9<ul> 10<li>Learn how to send a single message to multiple devices owned by a single user.</li> 11</ul> 12 13 14<h2>In this document</h2> 15 16<ol class="toc"> 17 <li><a href="#what">What are User Notifications?</a> </li> 18 <li><a href="#examples">Examples</a> 19 <ol> 20 <li><a href="#create">Generate a notification key</a></li> 21 <li><a href="#add">Add registration IDs</a></li> 22 <li><a href="#remove">Remove registration IDs</a></li> 23 <li><a href="#upstream">Send upstream messages</a></li> 24 <li><a href="#response">Response formats</a></li> 25 </ol> 26 </li> 27</ol> 28 29<h2>See Also</h2> 30 31<ol class="toc"> 32<li><a href="{@docRoot}google/play-services/gcm/gs.html">Getting Started</a></li> 33<li><a href="https://services.google.com/fb/forms/gcm/" class="external-link" target="_android">CCS and User Notifications Signup Form</a></li> 34</ol> 35 36</div> 37</div> 38 39<p class="note"><strong>Note:</strong> To try out this feature, sign up using <a href="https://services.google.com/fb/forms/gcm/">this form</a>.</p> 40 41<p>The upstream messaging (device-to-cloud) feature described in this document is part of the Google Play services platform. Upstream messaging is available through the <a href="{@docRoot}reference/com/google/android/gms/gcm/GoogleCloudMessaging.html">GoogleCloudMessaging</a> APIs. To use upstream messaging and the new streamlined registration process, you must <a href="{@docRoot}google/play-services/setup.html">set up</a> the Google Play services SDK.</p> 42 43<h2 id="what">What are User Notifications?</h2> 44 45<p>Third party servers can send a single message to multiple instance of an app running on devices owned by a single user. This feature is called <em>user notifications</em>. User notifications make it possible for every app instance that a user owns to reflect the latest messaging state. For example:</p> 46 47 <ul> 48 <li>If a message has been handled on one device, the GCM message on the other devices are dismissed. For example, if a user has handled a calendar notification on one device, the notification will go away on the user's other devices.</li> 49 <li>If a message has not been delivered yet to a device and but it has been handled, the GCM server removes it from the unsent queue for the other devices.</li> 50 <li>Likewise, a device can send messages to the {@code notification_key}, which is the token that GCM uses to fan out notifications to all devices whose registration IDs are associated with the key.</li> 51</ul> 52 53<p>The way this works is that during registration, the 3rd-party server requests a {@code notification_key}. The {@code notification_key} maps a particular user to all of the user's associated registration IDs (a regID represents a particular Android application running on a particular device). Then instead of sending one message to one regID at a time, the 3rd-party server can send a message to to the {@code notification_key}, which then sends the message to all of the user's regIDs.</p> 54 55<p>You can use this feature with either the new <a href="ccs.html">GCM Cloud Connection Server</a> (CCS), or the older <a href="gcm.html">GCM HTTP server</a>.</p> 56 57 58<h3 id="examples">Examples</h3> 59 60<p>The examples in this section show you how to perform generate/add/remove operations, and how to send upstream messages. For generate/add/remove operations, the message body is JSON.</p> 61 62<h4 id="request">Request format</h4> 63<p>To send a message, the application server issues a POST request to <code>https://android.googleapis.com/gcm/notification</code>.</p> 64 65<p>Here is the HTTP request header you should use for all create/add/remove operations:</p> 66 67<pre>content-type: "application/json" 68Header : "project_id": <projectID> 69Header: "Authorization", "key=API_KEY" 70</pre> 71 72<h4 id="create">Generate a notification key</h4> 73 74<p>This example shows how to create a new <code>notification_key</code> for a <code>notification_key_name</code> called <code>appUser-Chris</code>. The {@code notification_key_name} is a name or identifier (can be a username for a 3rd-party app) that is unique to a given user. It is used by third parties to group together registration IDs for a single user. Note that <code>notification_key_name</code> and <code>notification_key</code> are unique to a group of registration IDs. It is also important that <code>notification_key_name</code> be uniquely named per app in case you have multiple apps for the same project ID. This ensures that notifications only go to the intended target app.</p> 75 76 77<p>A create operation returns a token (<code>notification_key</code>). Third parties must save this token (as well as its mapping to the <code>notification_key_name</code>) to use in subsequent operations:</p> 78 79<pre>request: 80{ 81 "operation": "create", 82 "notification_key_name": "appUser-Chris", 83 "registration_ids": ["4", "8", "15", "16", "23", "42"] 84}</pre> 85 86<h4 id="add">Add registration IDs</h4> 87 88<p>This example shows how to add registration IDs for a given notification key. The maximum number of members allowed for a {@code notification_key} is 10.</p> 89 90<p>Note that the <code>notification_key_name</code> is not strictly required for adding/removing regIDs. But including it protects you against accidentally using the incorrect <code>notification_key</code>.</p> 91 92<pre>request: 93{ 94 "operation": "add", 95 "notification_key_name": "appUser-Chris", 96 "notification_key": "aUniqueKey" 97 "registration_ids": ["4", "8", "15", "16", "23", "42"] 98}</pre> 99 100<h4 id="remove">Remove registration IDs</h4> 101 102<p>This example shows how to remove registration IDs for a given notification key:</p> 103<pre>request: 104{ 105 "operation": "remove", 106 "notification_key_name": "appUser-Chris", 107 "notification_key": "aUniqueKey" 108 "registration_ids": ["4", "8", "15", "16", "23", "42"] 109}</pre> 110 111<h4 id="upstream">Send upstream messages</h4> 112 113<p>To send an upstream (device-to-cloud) message, you must use the <a href="{@docRoot}reference/com/google/android/gms/gcm/GoogleCloudMessaging.html">GoogleCloudMessaging</a> API. Specifying a {@code notification_key} as the target for an upstream message allows a user on one device to send a message to other devices in the notification group—for example, to dismiss a notification. Here is an example that shows targeting a {@code notification_key}:</p> 114 115<pre>GoogleCloudMessaging gcm = GoogleCloudMessaging.get(context); 116String to = NOTIFICATION_KEY; 117AtomicInteger msgId = new AtomicInteger(); 118String id = Integer.toString(msgId.incrementAndGet()); 119Bundle data = new Bundle(); 120data.putString("hello", "world"); 121 122gcm.send(to, id, data); 123</pre> 124 125<p>This call generates the necessary XMPP stanza for sending the message. The Bundle data consists of a key-value pair.</p> 126 127<p>For a complete example, see <a href="gs.html#gs_example">Getting Started</a>. 128 129<h4 id="response">Response formats</h4> 130 131<p>This section shows examples of the responses that can be returned for notification key operations.</p> 132 133<h5>Response for create/add/remove operations</h5> 134 135<p>When you make a request to create a {@code notification_key} or to add/remove its the wayregIDs, a successful response always returns the <code>notification_key</code>. This is the {@code notification_key} you will use for sending messages:</p> 136 137<pre>HTTP status: 200 138{ 139 "notification_key": "aUniqueKey", // to be used for sending 140}</pre> 141 142 143<h5>Response for send operations</h5> 144 145<p>For a send operation that has a {@code notification_key} as its target, the possible responses are success, partial success, and failure.</p> 146 147<p>Here is an example of "success"—the {@code notification_key} has 2 regIDs associated with it, and the message was successfully sent to both of them:</p> 148 149<pre>{ 150 "success": 2, 151 "failure": 0 152}</pre> 153 154<p>Here is an example of "partial success"—the {@code notification_key} has 3 regIDs associated with it. The message was successfully send to 1 of the regIDs, but not to the other 2. The response message lists the regIDs that failed to receive the message:</p> 155 156<pre>{ 157 "success":1, 158 "failure":2, 159 "failed_registration_ids":[ 160 "regId1", 161 "regId2" 162 ] 163}</pre> 164 165<p>In the case of failure, the response has HTTP code 503 and no JSON. When a message fails to be delivered to one or more of the regIDs associated with a {@code notification_key}, the 3rd-party server should retry.</p> 166 167 168 169 170 171 172