| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 1 | page.title=Implementing GCM Server |
| kmccormick | f788ac1 | 2013-05-30 19:06:41 -0700 | [diff] [blame] | 2 | @jd:body |
| 3 | |
| 4 | <div id="qv-wrapper"> |
| 5 | <div id="qv"> |
| 6 | |
| kmccormick | f788ac1 | 2013-05-30 19:06:41 -0700 | [diff] [blame] | 7 | <h2>In this document</h2> |
| 8 | |
| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 9 | <ol class="toc"> |
| 10 | <li><a href="#choose">Choosing a GCM Connection Server</a></li> |
| 11 | <li><a href="#role">Role of the 3rd-party Application Server</a></li> |
| 12 | <li><a href="#send-msg">Sending Messages</a> |
| 13 | <ol class="toc"> |
| 14 | |
| 15 | <li><a href="#target">Target</a></li> |
| 16 | <li><a href="#payload">Payload</a></li> |
| 17 | <li><a href="#params">Message parameters</a> |
| kmccormick | f788ac1 | 2013-05-30 19:06:41 -0700 | [diff] [blame] | 18 | </ol> |
| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 19 | </li> |
| 20 | <li><a href="#receive">Receiving Messages</a> </li> |
| kmccormick | f788ac1 | 2013-05-30 19:06:41 -0700 | [diff] [blame] | 21 | </li> |
| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 22 | |
| kmccormick | f788ac1 | 2013-05-30 19:06:41 -0700 | [diff] [blame] | 23 | </ol> |
| 24 | |
| 25 | <h2>See Also</h2> |
| 26 | |
| 27 | <ol class="toc"> |
| 28 | <li><a href="gs.html">Getting Started</a></li> |
| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 29 | <li><a href="client.html">Implementing GCM Client</a></li> |
| 30 | <li><a href="ccs.html">Cloud Connection Server (XMPP)</a></li> |
| 31 | <li><a href="http.html">HTTP Connection Server</a></li> |
| kmccormick | f788ac1 | 2013-05-30 19:06:41 -0700 | [diff] [blame] | 32 | |
| 33 | |
| 34 | </ol> |
| 35 | |
| 36 | </div> |
| 37 | </div> |
| 38 | |
| 39 | |
| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 40 | <p>The server side of GCM consists of 2 components:</p> |
| kmccormick | f788ac1 | 2013-05-30 19:06:41 -0700 | [diff] [blame] | 41 | <ul> |
| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 42 | <li>Google-provided <strong>GCM Connection Servers</strong> |
| 43 | take messages from a 3rd-party application server and send them to a GCM-enabled |
| 44 | Android application (the "client app") running on a device. For example, |
| 45 | Google provides connection servers for <a href="{@docRoot}google/gcm/http.html"> |
| 46 | HTTP</a> and <a href="{@docRoot}google/gcm/ccs.html">CCS</a> (XMPP).</li> |
| 47 | <li>A <strong>3rd-party application server</strong> that you must implement. This application |
| 48 | server sends data to a GCM-enabled Android application via the chosen GCM connection server.</li> |
| 49 | </ul> |
| 50 | </p> |
| 51 | |
| 52 | <p>Here are the basic steps you follow to implement your 3rd-party app server:</p> |
| 53 | |
| 54 | <ul> |
| 55 | <li>Decide which GCM connection server(s) you want to use. Note that if you want to use |
| 56 | upstream messaging from your client applications, you must use CCS. For a more detailed |
| 57 | discussion of this, see <a href="#choose"> |
| 58 | Choosing a GCM Connection Server</a>.</li> |
| 59 | <li>Decide how you want to implement your app server. For example: |
| 60 | <ul> |
| 61 | <li>If you decide to use the HTTP connection server, you can use the |
| 62 | GCM server helper library and demo app to help in implementing your app server.</li> |
| 63 | <li>If you decide to use the XMPP connection server, you can use |
| 64 | the provided Python or Java <a href="http://www.igniterealtime.org/projects/smack/"> |
| 65 | Smack</a> demo apps as a starting point.</li> |
| 66 | <li>Note that Google AppEngine does not support connections to CCS.</li> |
| 67 | </ul> |
| 68 | </li> |
| Katie McCormick | 53d9628 | 2013-07-23 18:12:38 -0700 | [diff] [blame] | 69 | </ul> |
| 70 | </li> |
| kmccormick | f788ac1 | 2013-05-30 19:06:41 -0700 | [diff] [blame] | 71 | </ul> |
| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 72 | |
| 73 | <p>A full GCM implementation requires both a client implementation and a server |
| 74 | implementation. For more |
| 75 | information about implementing the client side, see <a href="client.html"> |
| 76 | Implementing GCM Client</a>.</p> |
| 77 | |
| 78 | <h2 id="choose">Choosing a GCM Connection Server</h2> |
| 79 | |
| 80 | <p>Currently GCM provides two connection servers: <a href="{@docRoot}google/gcm/http.html"> |
| 81 | HTTP</a> and <a href="{@docRoot}google/gcm/ccs.html">CCS</a> (XMPP). You can use them |
| 82 | separately or in tandem. CCS messaging differs from GCM HTTP messaging in the following ways:</p> |
| Katie McCormick | 53d9628 | 2013-07-23 18:12:38 -0700 | [diff] [blame] | 83 | <ul> |
| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 84 | <li>Upstream/Downstream messages |
| 85 | <ul> |
| 86 | <li>GCM HTTP: Downstream only: cloud-to-device. </li> |
| 87 | <li>CCS: Upstream and downstream (device-to-cloud, cloud-to-device). </li> |
| 88 | </ul> |
| 89 | </li> |
| 90 | <li>Asynchronous messaging |
| 91 | <ul> |
| 92 | <li>GCM HTTP: 3rd-party app servers send messages as HTTP POST requests and |
| 93 | wait for a response. This mechanism is synchronous and causes the sender to block |
| 94 | before sending another message.</li> |
| 95 | <li>CCS: 3rd-party app servers connect to Google infrastructure using a |
| 96 | persistent XMPP connection and send/receive messages to/from all their devices |
| 97 | at full line speed. CCS sends acknowledgment or failure notifications (in the |
| 98 | form of special ACK and NACK JSON-encoded XMPP messages) asynchronously.</li> |
| 99 | </ul> |
| 100 | </li> |
| 101 | |
| 102 | <li>JSON |
| 103 | <ul> |
| 104 | <li>GCM HTTP: JSON messages sent as HTTP POST.</li> |
| 105 | <li>CCS: JSON messages encapsulated in XMPP messages.</li> |
| 106 | </ul> |
| 107 | </li> |
| Katie McCormick | 53d9628 | 2013-07-23 18:12:38 -0700 | [diff] [blame] | 108 | </ul> |
| 109 | |
| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 110 | <h2 id="role">Role of the 3rd-party Application Server</h2> |
| Katie McCormick | 53d9628 | 2013-07-23 18:12:38 -0700 | [diff] [blame] | 111 | |
| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 112 | <p>Before you can write client Android applications that use the GCM feature, you must |
| 113 | have an application server that meets the following criteria:</p> |
| 114 | |
| 115 | <ul> |
| 116 | <li>Able to communicate with your client.</li> |
| 117 | <li>Able to fire off properly formatted requests to the GCM server.</li> |
| 118 | <li>Able to handle requests and resend them as needed, using |
| 119 | <a href="http://en.wikipedia.org/wiki/Exponential_backoff">exponential back-off.</a></li> |
| 120 | <li>Able to store the API key and client registration IDs. The |
| 121 | API key is included in the header of POST requests that send |
| 122 | messages.</li> |
| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 123 | <li>Able to generate message IDs to uniquely identify each message it sends.</li> |
| 124 | </ul> |
| 125 | |
| 126 | <h2 id="send-msg">Sending Messages</h2> |
| 127 | |
| 128 | <p>Here is the general sequence of events that occurs when a 3rd-party application |
| 129 | server sends a message:</p> |
| kmccormick | f788ac1 | 2013-05-30 19:06:41 -0700 | [diff] [blame] | 130 | <ol> |
| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 131 | <li>The application server sends a message to GCM servers.</li> |
| 132 | <li>Google enqueues and stores the message in case the device is offline.</li> |
| 133 | <li>When the device is online, Google sends the message to the device.</li> |
| 134 | <li>On the device, the system broadcasts the message to the specified Android |
| 135 | application via Intent broadcast with proper permissions, so that only the targeted |
| Katie McCormick | 6b37262 | 2013-11-15 16:02:58 -0800 | [diff] [blame] | 136 | Android application gets the message. This wakes the Android application up. |
| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 137 | The Android application does not need to be running beforehand to receive the message.</li> |
| 138 | <li>The Android application processes the message. </li> |
| kmccormick | f788ac1 | 2013-05-30 19:06:41 -0700 | [diff] [blame] | 139 | </ol> |
| kmccormick | f788ac1 | 2013-05-30 19:06:41 -0700 | [diff] [blame] | 140 | |
| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 141 | <p>The following sections describe the basic requirements for |
| 142 | sending messages.</p> |
| kmccormick | f788ac1 | 2013-05-30 19:06:41 -0700 | [diff] [blame] | 143 | |
| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 144 | <h3 id="target">Target</h3> |
| 145 | <p>Required. When your app server sends a message in GCM, it must specify a target.</p> |
| 146 | <p>For HTTP you must specify the target as one of:</p> |
| 147 | <ul> |
| Katie McCormick | 6b37262 | 2013-11-15 16:02:58 -0800 | [diff] [blame] | 148 | <li><code>registration_ids</code>: For sending to 1 or more devices (up to 1000). |
| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 149 | When you send a message to multiple registration IDs, that is called a multicast message.</li> |
| 150 | <li><code>notification_key</code>: For sending to multiple devices owned by a single user.</li> |
| 151 | </ul> |
| 152 | <p>For CCS (XMPP):</p> |
| 153 | <ul> |
| 154 | <li>You must specify the target as the "to" field, where the "to" |
| 155 | field may contain a single registration ID or a notification key. |
| 156 | CCS does not support multicast messaging.</li> |
| 157 | </ul> |
| 158 | <h3 id="payload">Payload</h3> |
| 159 | <p>Optional. If you are including a payload in the message, you use the <code>data</code> |
| 160 | parameter to include the payload. This applies for both HTTP and CCS.</p> |
| kmccormick | f788ac1 | 2013-05-30 19:06:41 -0700 | [diff] [blame] | 161 | |
| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 162 | <h3 id="params">Message parameters</h3> |
| 163 | |
| 164 | <p>The following table lists the parameters that a 3rd-party app server might |
| 165 | include in the JSON messages it sends to a connection server. See the "Where Supported" |
| 166 | column for information about which connection servers support that particular |
| 167 | parameter.</p> |
| 168 | |
| 169 | <p class="table-caption" id="table1"> |
| 170 | <strong>Table 1.</strong> Message parameters.</p> |
| 171 | |
| 172 | <table> |
| 173 | <tr> |
| 174 | <th>Field</th> |
| 175 | <th>Description</th> |
| 176 | <th>Where Supported</th> |
| 177 | </tr> |
| 178 | <td><code>to</code></td> |
| 179 | <td>In CCS, used in place of <code>registration_ids</code> to specify the |
| 180 | recipient of a message. Its value must be a registration ID. |
| 181 | The value is a string. Required.</td> |
| 182 | <td>CCS</td> |
| 183 | </tr> |
| 184 | <tr> |
| 185 | <td><code>message_id</code></td> |
| 186 | <td>In CCS, uniquely identifies a message in an XMPP connection. The value is a |
| 187 | string that uniquely identifies the associated message. The value is a string. Required.</td> |
| 188 | <td>CCS</td> |
| 189 | </tr> |
| 190 | <tr> |
| 191 | <td><code>message_type</code></td> |
| 192 | <td>In CCS, indicates a special status message, typically sent by the system. |
| 193 | However, your app server also uses this parameter to send an 'ack' or 'nack' |
| 194 | message back to the CCS connection server. For more discussion of this topic, see |
| 195 | <a href="ccs.html">Cloud Connection Server</a>. The value is a string. Optional.</td> |
| 196 | <td>CCS</td> |
| 197 | <tr> |
| 198 | <td><code>registration_ids</code></td> |
| 199 | <td>A string array with the list of devices (registration IDs) receiving the |
| 200 | message. It must contain at least 1 and at most 1000 registration IDs. To send a |
| 201 | multicast message, you must use JSON. For sending a single message to a single |
| 202 | device, you could use a JSON object with just 1 registration id, or plain text |
| 203 | (see below). A request must include a recipient—this can be either a |
| 204 | registration ID, an array of registration IDs, or a {@code notification_key}. |
| 205 | Required.</td> |
| 206 | <td>HTTP</td> |
| 207 | </tr> |
| 208 | <tr> |
| 209 | <td><code>notification_key</code></td> |
| 210 | <td>A string that maps a single user to multiple registration IDs associated |
| 211 | with that user. This allows a 3rd-party server to send a single message to |
| 212 | multiple app instances (typically on multiple devices) owned by a single user. |
| 213 | A 3rd-party server can use {@code notification_key} as the target for a message |
| 214 | instead of an individual registration ID (or array of registration IDs). The maximum |
| 215 | number of members allowed for a {@code notification_key} is 10. For more discussion |
| 216 | of this topic, see <a href="notifications.html">User Notifications</a>. Optional. |
| 217 | </td> |
| 218 | <td style="width:100px">HTTP. This feature is supported in CCS, but you use it by |
| 219 | specifying a notification key in the "to" field.</td> |
| 220 | </tr> |
| 221 | <tr> |
| 222 | <td><code>collapse_key</code></td> |
| 223 | <td>An arbitrary string (such as "Updates Available") that is used |
| 224 | to collapse a group of like messages |
| 225 | when the device is offline, so that only the last message gets sent to the |
| 226 | client. This is intended to avoid sending too many messages to the phone when it |
| 227 | comes back online. Note that since there is no guarantee of the order in which |
| 228 | messages get sent, the "last" message may not actually be the last |
| 229 | message sent by the application server. Collapse keys are also called |
| 230 | <a href="#s2s">send-to-sync messages</a>. |
| 231 | <br> |
| 232 | <strong>Note:</strong> GCM allows a maximum of 4 different collapse keys to be |
| 233 | used by the GCM server |
| 234 | at any given time. In other words, the GCM server can simultaneously store 4 |
| 235 | different send-to-sync messages per device, each with a different collapse key. |
| 236 | If you exceed |
| 237 | this number GCM will only keep 4 collapse keys, with no guarantees about which |
| 238 | ones they will be. See <a href="adv.html#collapsible">Advanced Topics</a> for more |
| 239 | discussion of this topic. Optional.</td> |
| 240 | <td>CCS, HTTP</td> |
| 241 | </tr> |
| 242 | <tr> |
| 243 | <td><code>data</code></td> |
| 244 | <td>A JSON object whose fields represents the key-value pairs of the message's |
| 245 | payload data. If present, the payload data it will be |
| 246 | included in the Intent as application data, with the key being the extra's name. |
| 247 | For instance, <code>"data":{"score":"3x1"}</code> would result in an intent extra |
| 248 | named <code>score</code> whose value is the string <code>3x1</code>. |
| 249 | There is no limit on the number of key/value pairs, though there is a limit on |
| 250 | the total size of the message (4kb). The values could be any JSON object, but we |
| 251 | recommend using strings, since the values will be converted to strings in the GCM |
| 252 | server anyway. If you want to include objects or other non-string data types |
| 253 | (such as integers or booleans), you have to do the conversion to string yourself. |
| 254 | Also note that the key cannot be a reserved word (<code>from</code> or any word |
| 255 | starting with <code>google.</code>). To complicate things slightly, there are |
| 256 | some reserved words (such as <code>collapse_key</code>) that are technically |
| 257 | allowed in payload data. However, if the request also contains the word, the |
| 258 | value in the request will overwrite the value in the payload data. Hence using |
| 259 | words that are defined as field names in this table is not recommended, even in |
| 260 | cases where they are technically allowed. Optional.</td> |
| 261 | <td>CCS, HTTP</td> |
| 262 | </tr> |
| 263 | <tr> |
| 264 | <td><code>delay_while_idle</code></td> |
| 265 | <td>If included, indicates that the message should not be sent immediately |
| 266 | if the device is idle. The server will wait for the device to become active, and |
| 267 | then only the last message for each <code>collapse_key</code> value will be |
| 268 | sent. The default value is <code>false</code>, and must be a JSON boolean. Optional.</td> |
| 269 | <td>CCS, HTTP</td> |
| 270 | </tr> |
| 271 | <tr> |
| 272 | <td><code>time_to_live</code></td> |
| 273 | <td>How long (in seconds) the message should be kept on GCM storage if the |
| 274 | device is offline. Optional (default time-to-live is 4 weeks, and must be set as |
| 275 | a JSON number).</td> |
| 276 | <td>CCS, HTTP</td> |
| 277 | </tr> |
| 278 | <tr> |
| 279 | <td><code>restricted_package_name</code></td> |
| 280 | <td>A string containing the package name of your application. When set, messages |
| 281 | will only be sent to registration IDs that match the package name. Optional. |
| 282 | </td> |
| 283 | <td>HTTP</td> |
| 284 | </tr> |
| 285 | <tr> |
| 286 | <td><code>dry_run</code></td> |
| 287 | <td>If included, allows developers to test their request without actually |
| 288 | sending a message. Optional. The default value is <code>false</code>, and must |
| 289 | be a JSON boolean. |
| 290 | </td> |
| 291 | <td>HTTP</td> |
| 292 | </tr> |
| 293 | </table> |
| 294 | |
| 295 | <p>If you want to test your request (either JSON or plain text) without delivering |
| 296 | the message to the devices, you can set an optional HTTP or JSON parameter called |
| 297 | <code>dry_run</code> with the value <code>true</code>. The result will be almost |
| 298 | identical to running the request without this parameter, except that the message |
| 299 | will not be delivered to the devices. Consequently, the response will contain fake |
| 300 | IDs for the message and multicast fields.</p> |
| 301 | |
| 302 | <h3 id="plain-text">Plain text (HTTP only)</h3> |
| 303 | |
| 304 | <p>If you are using plain text instead of JSON, the message fields must be set as |
| 305 | HTTP parameters sent in the body, and their syntax is slightly different, as |
| 306 | described below: |
| 307 | <table> |
| 308 | <tr> |
| 309 | <th>Field</th> |
| 310 | <th>Description</th> |
| 311 | </tr> |
| 312 | <tr> |
| 313 | <td><code>registration_id</code></td> |
| 314 | <td>Must contain the registration ID of the single device receiving the message. |
| 315 | Required.</td> |
| 316 | </tr> |
| 317 | <tr> |
| 318 | <td><code>collapse_key</code></td> |
| 319 | <td>Same as JSON (see previous table). Optional.</td> |
| 320 | </tr> |
| 321 | <tr> |
| 322 | <td><code>data.<key></code></td> |
| 323 | |
| 324 | <td>Payload data, expressed as parameters prefixed with <code>data.</code> and |
| 325 | suffixed as the key. For instance, a parameter of <code>data.score=3x1</code> would |
| 326 | result in an intent extra named <code>score</code> whose value is the string |
| 327 | <code>3x1</code>. There is no limit on the number of key/value parameters, though |
| 328 | there is a limit on the total size of the message. Also note that the key cannot |
| 329 | be a reserved word (<code>from</code> or any word starting with |
| 330 | <code>google.</code>). To complicate things slightly, there are some reserved words |
| 331 | (such as <code>collapse_key</code>) that are technically allowed in payload data. |
| 332 | However, if the request also contains the word, the value in the request will |
| 333 | overwrite the value in the payload data. Hence using words that are defined as |
| 334 | field names in this table is not recommended, even in cases where they are |
| 335 | technically allowed. Optional.</td> |
| 336 | |
| 337 | </tr> |
| 338 | <tr> |
| 339 | <td><code>delay_while_idle</code></td> |
| 340 | <td>Should be represented as <code>1</code> or <code>true</code> for |
| 341 | <code>true</code>, anything else for <code>false</code>. Optional. The default |
| 342 | value is <code>false</code>.</td> |
| 343 | </tr> |
| 344 | <tr> |
| 345 | <td><code>time_to_live</code></td> |
| 346 | <td>Same as JSON (see previous table). Optional.</td> |
| 347 | </tr> |
| 348 | <tr> |
| 349 | <td><code>restricted_package_name</code></td> |
| 350 | <td>Same as JSON (see previous table). Optional. |
| 351 | </td> |
| 352 | </tr> |
| 353 | <tr> |
| 354 | <td><code>dry_run</code></td> |
| 355 | <td>Same as JSON (see previous table). Optional. |
| 356 | </td> |
| 357 | </tr> |
| 358 | </table> |
| 359 | |
| 360 | <h2 id="receive">Receiving Messages</h2> |
| 361 | |
| 362 | <p>This is the sequence of events that occurs when an Android application |
| 363 | installed on a mobile device receives a message:</p> |
| 364 | |
| kmccormick | f788ac1 | 2013-05-30 19:06:41 -0700 | [diff] [blame] | 365 | <ol> |
| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 366 | <li>The system receives the incoming message and extracts the raw key/value |
| 367 | pairs from the message payload, if any.</li> |
| 368 | <li>The system passes the key/value pairs to the targeted Android application |
| 369 | in a <code>com.google.android.c2dm.intent.RECEIVE</code> Intent as a set of |
| 370 | extras.</li> |
| 371 | <li>The Android application extracts the raw data |
| 372 | from the <code>com.google.android.c2dm.intent.RECEIVE</code><code> </code>Intent |
| 373 | by key and processes the data.</li> |
| Scott Main | b478b10 | 2013-08-12 09:57:24 -0700 | [diff] [blame] | 374 | </ol> |
| Scott Main | b478b10 | 2013-08-12 09:57:24 -0700 | [diff] [blame] | 375 | |
| Katie McCormick | 0ab9326 | 2013-11-01 18:10:51 -0700 | [diff] [blame] | 376 | <p>See the documentation for each connection server for more detail on how it |
| 377 | handles responses.</p> |