Home | History | Annotate | Download | only in gcm
      1 page.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 class="note"><strong>Note:</strong> A notification dismissal message is like any other upstream message, meaning that it will be delivered to the other devices that belong to the specified {@code notification_key}. You should design your app to handle cases where the app receives a dismissal message, but has not yet displayed the notification that is being dismissed. You can solve this by caching the dismissal and then reconciling it with the corresponding notification.
     56 </p>
     57 
     58 <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>
     59 
     60 
     61 <h3 id="examples">Examples</h3>
     62 
     63 <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>
     64 
     65 <h4 id="request">Request format</h4>
     66 <p>To send a  message, the application server issues a POST request to <code>https://android.googleapis.com/gcm/notification</code>.</p>
     67 
     68 <p>Here is the HTTP request header you should use for all create/add/remove operations:</p>
     69 
     70 <pre>content-type: "application/json"
     71 Header : "project_id": &lt;projectID&gt;
     72 Header: "Authorization", "key=API_KEY"
     73 </pre>
     74 
     75 <h4 id="create">Generate a notification key</h4>
     76 
     77 <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>
     78 
     79 
     80 <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>
     81 
     82 <pre>request:
     83 { 
     84    &quot;operation&quot;: &quot;create&quot;,
     85    &quot;notification_key_name&quot;: &quot;appUser-Chris&quot;,
     86    &quot;registration_ids&quot;: [&quot;4&quot;, &quot;8&quot;, &quot;15&quot;, &quot;16&quot;, &quot;23&quot;, &quot;42&quot;]
     87 }</pre>
     88 
     89 <h4 id="add">Add registration IDs</h4>
     90 
     91 <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>
     92 
     93 <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>
     94 
     95 <pre>request:
     96 { 
     97    &quot;operation&quot;: &quot;add&quot;,
     98    &quot;notification_key_name&quot;: &quot;appUser-Chris&quot;,
     99    &quot;notification_key&quot;: &quot;aUniqueKey&quot;
    100    &quot;registration_ids&quot;: [&quot;4&quot;, &quot;8&quot;, &quot;15&quot;, &quot;16&quot;, &quot;23&quot;, &quot;42&quot;]
    101 }</pre>
    102 
    103 <h4 id="remove">Remove registration IDs</h4>
    104 
    105 <p>This example shows how to remove registration IDs for a given notification key:</p>
    106 <pre>request:
    107 { 
    108    &quot;operation&quot;: &quot;remove&quot;,
    109    &quot;notification_key_name&quot;: &quot;appUser-Chris&quot;,
    110    &quot;notification_key&quot;: &quot;aUniqueKey&quot;
    111    &quot;registration_ids&quot;: [&quot;4&quot;, &quot;8&quot;, &quot;15&quot;, &quot;16&quot;, &quot;23&quot;, &quot;42&quot;]
    112 }</pre>
    113 
    114 <h4 id="upstream">Send upstream messages</h4>
    115 
    116 <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&mdash;for example, to dismiss a notification. Here is an example that shows targeting a {@code notification_key}:</p>
    117 
    118 <pre>GoogleCloudMessaging gcm = GoogleCloudMessaging.get(context);
    119 String to = NOTIFICATION_KEY;
    120 AtomicInteger msgId = new AtomicInteger();
    121 String id = Integer.toString(msgId.incrementAndGet());
    122 Bundle data = new Bundle();
    123 data.putString("hello", "world");
    124 
    125 gcm.send(to, id, data);
    126 </pre>
    127 
    128 <p>This call generates the necessary XMPP stanza for sending the message. The Bundle data consists of a key-value pair.</p>
    129 
    130 <p>For a complete example, see <a href="gs.html#gs_example">Getting Started</a>. 
    131 
    132 <h4 id="response">Response formats</h4>
    133 
    134 <p>This section shows examples of the responses that can be returned for notification key operations.</p>
    135 
    136 <h5>Response for create/add/remove operations</h5>
    137 
    138 <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>
    139 
    140 <pre>HTTP status: 200
    141 { 
    142     &quot;notification_key&quot;: &quot;aUniqueKey&quot;,   // to be used for sending
    143 }</pre>
    144 
    145 
    146 <h5>Response for send operations</h5>
    147 
    148 <p>For a send operation that has a {@code notification_key} as its target, the possible responses are success, partial success, and failure.</p>
    149 
    150 <p>Here is an example of "success"&mdash;the {@code notification_key} has 2 regIDs associated with it, and the message was successfully sent to both of them:</p>
    151 
    152 <pre>{
    153   "success": 2,
    154   "failure": 0
    155 }</pre>
    156 
    157 <p>Here is an example of "partial success"&mdash;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>
    158 
    159 <pre>{
    160   "success":1,
    161   "failure":2,
    162   "failed_registration_ids":[
    163      "regId1",
    164      "regId2"
    165   ]
    166 }</pre>
    167 
    168 <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>
    169 
    170 
    171 
    172 
    173 
    174 
    175