diff options
author | Eric Gilmore <egilmore@google.com> | 2015-02-10 01:13:01 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2015-02-10 01:13:03 +0000 |
commit | c9dbaf0483b0274879164806297180d260689f1f (patch) | |
tree | 33e4eaae7762ea2e6a00b100ce3926310cf9c672 | |
parent | ba20f98c88b754b3a69d55963275e20ab2fba6bf (diff) | |
parent | 0137175af92b631932c03660baf4f634b65001b8 (diff) | |
download | frameworks_base-c9dbaf0483b0274879164806297180d260689f1f.zip frameworks_base-c9dbaf0483b0274879164806297180d260689f1f.tar.gz frameworks_base-c9dbaf0483b0274879164806297180d260689f1f.tar.bz2 |
Merge "Doc change: refactor GCM docs to be platform-agnostic." into lmp-docs
-rw-r--r-- | docs/html/google/gcm/adv.jd | 410 | ||||
-rw-r--r-- | docs/html/google/gcm/c2dm.jd | 12 | ||||
-rw-r--r-- | docs/html/google/gcm/ccs.jd | 58 | ||||
-rw-r--r-- | docs/html/google/gcm/client.jd | 146 | ||||
-rw-r--r-- | docs/html/google/gcm/gcm.jd | 339 | ||||
-rw-r--r-- | docs/html/google/gcm/gs.jd | 17 | ||||
-rw-r--r-- | docs/html/google/gcm/http.jd | 129 | ||||
-rw-r--r-- | docs/html/google/gcm/notifications.jd | 6 | ||||
-rw-r--r-- | docs/html/google/gcm/server-ref.jd | 763 | ||||
-rw-r--r-- | docs/html/google/gcm/server.jd | 648 | ||||
-rw-r--r-- | docs/html/google/google_toc.cs | 13 |
11 files changed, 1445 insertions, 1096 deletions
diff --git a/docs/html/google/gcm/adv.jd b/docs/html/google/gcm/adv.jd deleted file mode 100644 index 95497e3..0000000 --- a/docs/html/google/gcm/adv.jd +++ /dev/null @@ -1,410 +0,0 @@ -page.title=GCM Advanced Topics -@jd:body - -<div id="qv-wrapper"> -<div id="qv"> - -<h2>Quickview</h2> - -<ul> -<li>Learn more about GCM advanced features.</li> -</ul> - - -<h2>In this document</h2> - -<ol> -<li><a href="#lifetime">Lifetime of a Message</a></li> -<li><a href="#throttling">Throttling</a></li> -<li><a href="#reg-state">Keeping the Registration State in Sync</a> - <ol> - <li><a href="#canonical">Canonical IDs</a></li> - </ol> -</li> -<li><a href="#retry">Automatic Retry Using Exponential Back-Off</a></li> -<li><a href="#unreg">Unregistration</a> - <ol> - <li><a href="#unreg-why">Why you should rarely unregister</a></li> - <li><a href="#unreg-how">How unregistration works</a></li> - </ol> -</li> -<li><a href="#collapsible">Send-to-Sync vs. Messages with Payload</a> - <ol> - <li><a href="#s2s">Send-to-sync messages</a></li> - <li><a href="#payload">Messages with payload</a></li> -<li><a href="#which">Which should I use?</a></li> - </ol> -</li> -<li><a href="#ttl">Setting an Expiration Date for a Message</a> </li> -<li><a href="#throttling"></a><a href="#multi-senders">Receiving Messages from -Multiple Senders</a></li> -</ol> - -</div> -</div> -<p>This document covers advanced topics for GCM.</p> - - - - -<h2 id="msg-lifetime">Lifetime of a Message</h2> -<p>When a 3rd-party server posts a message to GCM and receives a message ID back, -it does not mean that the message was already delivered to the device. Rather, it -means that it was accepted for delivery. What happens to the message after it is -accepted depends on many factors.</p> - -<p>In the best-case scenario, if the device is connected to GCM, the screen is on, -and there are no throttling restrictions (see <a href="#throttling">Throttling</a>), -the message will be delivered right away.</p> - -<p>If the device is connected but idle, the message will still be -delivered right away unless the <code>delay_while_idle</code> flag is set to true. -Otherwise, it will be stored in the GCM servers until the device is awake. And -that's where the <code>collapse_key</code> flag plays a role: if there is already -a message with the same collapse key (and registration ID) stored and waiting for -delivery, the old message will be discarded and the new message will take its place -(that is, the old message will be collapsed by the new one). However, if the collapse -key is not set, both the new and old messages are stored for future delivery. -Collapsible messages are also called <a href="#s2s">send-to-sync messages</a>.</p> - -<p class="note"><strong>Note:</strong> There is a limit on how many messages can -be stored without collapsing. That limit is currently 100. If the limit is reached, -all stored messages are discarded. Then when the device is back online, it receives -a special message indicating that the limit was reached. The application can then -handle the situation properly, typically by requesting a full sync. -<br><br> -Likewise, there is a limit on how many <code>collapse_key</code>s you can have for -a particular device. GCM allows a maximum of 4 different collapse keys to be used -by the GCM server per device -any given time. In other words, the GCM server can simultaneously store 4 different -send-to-sync messages, each with a different collapse key. If you exceed this number -GCM will only keep 4 collapse keys, with no guarantees about which ones they will be. -See <a href="#s2s">Send-to-sync messages</a> for more information. -</p> - -<p>If the device is not connected to GCM, the message will be stored until a -connection is established (again respecting the collapse key rules). When a connection -is established, GCM will deliver all pending messages to the device, regardless of -the <code>delay_while_idle</code> flag. If the device never gets connected again -(for instance, if it was factory reset), the message will eventually time out and -be discarded from GCM storage. The default timeout is 4 weeks, unless the -<code>time_to_live</code> flag is set.</p> - -<p>Finally, when GCM attempts to deliver a message to the device and the -application was uninstalled, GCM will discard that message right away and -invalidate the registration ID. Future attempts to send a message to that device -will get a <code>NotRegistered</code> error. See <a href="#unreg"> -How Unregistration Works</a> for more information.</p> -<p>Although is not possible to track the status of each individual message, the -Google Cloud Console stats are broken down by messages sent to device, messages -collapsed, and messages waiting for delivery.</p> - -<h2 id="throttling">Throttling</h2> -<p>To prevent abuse (such as sending a flood of messages to a device) and -to optimize for the overall network efficiency and battery life of -devices, GCM implements throttling of messages using a token bucket -scheme. Messages are throttled on a per application and per <a href="#collapsible">collapse -key</a> basis (including non-collapsible messages). Each application -collapse key is granted some initial tokens, and new tokens are granted -periodically therefter. Each token is valid for a single message sent to -the device. If an application collapse key exhausts its supply of -available tokens, new messages are buffered in a pending queue until -new tokens become available at the time of the periodic grant. Thus -throttling in between periodic grant intervals may add to the latency -of message delivery for an application collapse key that sends a large -number of messages within a short period of time. Messages in the pending -queue of an application collapse key may be delivered before the time -of the next periodic grant, if they are piggybacked with messages -belonging to a non-throttled category by GCM for network and battery -efficiency reasons.</p> - -<h2 id="reg-state">Keeping the Registration State in Sync</h2> -<p>Whenever the application registers as described in -<a href="{@docRoot}google/gcm/client.html">Implementing GCM Client</a>, -it should save the registration ID for future use, pass it to the -3rd-party server to complete the registration, and keep track of -whether the server completed the registration. If the server fails -to complete the registration, it should try again or unregister from GCM.</p> - -<p>There are also two other scenarios that require special care:</p> -<ul> - <li>Application update</li> - <li>Backup and restore - </li> -</ul> -<p>When an application is updated, it should invalidate its existing registration -ID, as it is not guaranteed to work with the new version. Because there is no -lifecycle method called when the application is updated, the best way to achieve -this validation is by storing the current application version when a registration -ID is stored. Then when the application is started, compare the stored value with -the current application version. If they do not match, invalidate the stored data -and start the registration process again.</p> - -<p>Similarly, you should not save the registration ID when an application is -backed up. This is because the registration ID could become invalid by the time -the application is restored, which would put the application in an invalid state -(that is, the application thinks it is registered, but the server and GCM do not -store that registration ID anymore—thus the application will not get more -messages).</p> -<h3 id="canonical">Canonical IDs</h3> -<p>On the server side, as long as the application is behaving well, everything -should work normally. However, if a bug in the application triggers multiple -registrations for the same device, it can be hard to reconcile state and you might -end up with duplicate messages.</p> -<p>GCM provides a facility called "canonical registration IDs" to easily -recover from these situations. A canonical registration ID is defined to be the ID -of the last registration requested by your application. This is the ID that the -server should use when sending messages to the device.</p> -<p>If later on you try to send a message using a different registration ID, GCM -will process the request as usual, but it will include the canonical registration -ID in the <code>registration_id</code> field of the response. Make sure to replace -the registration ID stored in your server with this canonical ID, as eventually -the ID you're using will stop working.</p> - -<h2 id="retry">Automatic Retry Using Exponential Back-Off</h2> - -<p>When registration or unregistration fails, the app should retry the failed operation.</p> -<p>In the simplest case, if your application attempts to register and GCM is not a -fundamental part of the application, the application could simply ignore the error -and try to register again the next time it starts. Otherwise, it should retry the -previous operation using exponential back-off. In exponential back-off, each time -there is a failure, it should wait twice the previous amount of time before trying -again. If the register (or unregister) operation was synchronous, it could be retried -in a simple loop. However, since it is asynchronous, the best approach is to schedule -a {@link android.app.PendingIntent} to retry the operation. - -<h2 id="unreg">Unregistration</h2> - -<p>This section explains when you should unregister in GCM and what happens -when you do.</p> - -<h3 id="unreg-why">Why you should rarely unregister</h3> - -<p>A registration ID (regID) represents a particular Android application running -on a particular device. You should only need to unregister in rare cases, such as -if you want an app to stop receiving messages, or if you suspect that the regID has -been compromised. In general, though, once an app has a regID, you shouldn't need -to change it.</p> - -<p>In particular, you should never unregister your app as a mechanism for -logout or for switching between users, for the following reasons:</p> - -<ul> - <li>A regID maps an app to a device. It isn't associated with a particular - logged in user. If you unregister and then re-register, GCM may return the same - ID or a different ID—there's no guarantee either way.</li> - - <li>Unregistration may take up to 5 minutes to propagate.</li> - <li>After unregistration, re-registration may again take up to 5 minutes to -propagate. During this time messages may be rejected due to the state of being -unregistered, and after all this, messages may still go to the wrong user.</li> -</ul> - - -<p>The solution is to manage your own mapping between users, the regID, and -individual messages:</p> - -<ul> - <li>Your app server should maintain a mapping between the current user -and the regID. This should include information about which user is supposed to -receive a particular message.</li> - <li>The app running on the device should check to ensure that messages it -receives match the logged in user.</li> -</ul> - - -<h3 id="unreg-how">How unregistration works</h3> - -<p>An application can be automatically unregistered after it is uninstalled from -the device. However, this process does not happens right away, as Android does not -provide an uninstall callback. What happens in this scenario is as follows:</p> -<ol> - <li>The end user uninstalls the application.</li> - <li>The 3rd-party server sends a message to GCM server.</li> - <li>The GCM server sends the message to the device.</li> - <li>The GCM client receives the message and queries Package Manager about -whether there are broadcast receivers configured to receive it, which returns -<code>false</code>. -</li> - <li>The GCM client informs the GCM server that the application was uninstalled.</li> - <li>The GCM server marks the registration ID for deletion.</li> - <li>The 3rd-party server sends a message to GCM.</li> - <li>The GCM returns a <code>NotRegistered</code> error message to the 3rd-party server.</li> - <li>The 3rd-party deletes the registration ID. - </li> -</ol> - -<p class ="note"><strong>Note:</strong> The GCM client is the Google Cloud -Messaging framework present on the device.</p> - -<p>Note that it might take a while for the registration ID be completely removed -from GCM. Thus it is possible that messages sent during step 7 above gets a valid -message ID as response, even though the message will not be delivered to the device. -Eventually, the registration ID will be removed and the server will get a -<code>NotRegistered</code> error, without any further action being required from -the 3rd-party server (this scenario happens frequently while an application is -being developed and tested).</p> - -<h2 id="collapsible">Send-to-Sync vs. Messages with Payload</h2> - -<p>Every message sent in GCM has the following characteristics:</p> -<ul> - <li>It has a payload limit of 4096 bytes.</li> - <li>By default, it is stored by GCM for 4 weeks.</li> -</ul> - -<p>But despite these similarities, messages can behave very differently depending -on their particular settings. One major distinction between messages is whether -they are collapsed (where each new message replaces the preceding message) or not -collapsed (where each individual message is delivered). Every message sent in GCM -is either a "send-to-sync" (collapsible) message or a "message with -payload" (non-collapsible message). These concepts are described in more -detail in the following sections.</p> - -<h3 id="s2s">Send-to-sync messages</h3> - -<p>A send-to-sync (collapsible) message is often a "tickle" that tells -a mobile application to sync data from the server. For example, suppose you have -an email application. When a user receives new email on the server, the server -pings the mobile application with a "New mail" message. This tells the -application to sync to the server to pick up the new email. The server might send -this message multiple times as new mail continues to accumulate, before the application -has had a chance to sync. But if the user has received 25 new emails, there's no -need to preserve every "New mail" message. One is sufficient. Another -example would be a sports application that updates users with the latest score. -Only the most recent message is relevant, so it makes sense to have each new -message replace the preceding message. </p> - -<p>The email and sports applications are cases where you would probably use the -GCM <code>collapse_key</code> parameter. A <em>collapse key</em> is an arbitrary -string that is used to collapse a group of like messages when the device is offline, -so that only the most recent message gets sent to the client. For example, -"New mail," "Updates available," and so on</p> -<p>GCM allows a maximum of 4 different collapse keys to be used by the GCM server -at any given time. In other words, the GCM server can simultaneously store 4 -different send-to-sync messages per device, each with a different collapse key. -For example, Device A can have A1, A2, A3, and A4. Device B can have B1, B2, B3, -and B4, and so on. If you exceed this number GCM will only keep 4 collapse keys, with no -guarantees about which ones they will be.</p> - -<h3 id="payload">Messages with payload</h3> - -<p>Unlike a send-to-sync message, every "message with payload" -(non-collapsible message) is delivered. The payload the message contains can be -up to 4kb. For example, here is a JSON-formatted message in an IM application in -which spectators are discussing a sporting event:</p> - -<pre class="prettyprint pretty-json">{ - "registration_id" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...", - "data" : { - "Nick" : "Mario", - "Text" : "great match!", - "Room" : "PortugalVSDenmark", - }, -}</pre> - -<p>A "message with payload" is not simply a "ping" to the -mobile application to contact the server to fetch data. In the aforementioned IM -application, for example, you would want to deliver every message, because every -message has different content. To specify a non-collapsible message, you simply -omit the <code>collapse_key</code> parameter. Thus GCM will send each message -individually. Note that the order of delivery is not guaranteed.</p> - -<p>GCM will store up to 100 non-collapsible messages. After that, all messages -are discarded from GCM, and a new message is created that tells the client how -far behind it is. The message is delivered through a regular -<code>com.google.android.c2dm.intent.RECEIVE</code> intent with the -extra <code>message_type</code>, for which the value is always the string -"deleted_messages".</p> - -<p>The application should respond by syncing with the server to recover the -discarded messages. </p> - -<h3 id="which">Which should I use?</h3> - <p>If your application does not need to use non-collapsible messages, collapsible -messages are a better choice from a performance standpoint, because they put less -of a burden on the device battery. However, if you use collapsible messages, remember that -<strong>GCM only allows a maximum of 4 different collapse keys to be used by the GCM server -per device at any given time</strong>. You must not exceed this number, or it could cause -unpredictable consequences.</p> - -<h2 dir="ltr" id="ttl">Setting an Expiration Date for a Message</h2> -<p>The Time to Live (TTL) feature lets the sender specify the maximum lifespan -of a message using the <code>time_to_live</code> parameter in the send request. -The value of this parameter must be a duration from 0 to 2,419,200 seconds, and -it corresponds to the maximum period of time for which GCM will store and try to -deliver the message. Requests that don't contain this field default to the maximum -period of 4 weeks.</p> -<p>Here are some possible uses for this feature:</p> -<ul> - <li>Video chat incoming calls</li> - <li>Expiring invitation events</li> - <li>Calendar events</li> -</ul> -<h3 id="bg">Background </h3> -<p>GCM will usually deliver messages immediately after they are sent. However, -this might not always be possible. For example, the device could be turned off, -offline, or otherwise unavailable. In other cases, the sender itself might request -that messages not be delivered until the device becomes active by using the -<code>delay_while_idle</code> flag. Finally, GCM might intentionally delay messages -to prevent an application from consuming excessive resources and negatively -impacting battery life.</p> - -<p>When this happens, GCM will store the message and deliver it as soon as it's -feasible. While this is fine in most cases, there are some applications for which -a late message might as well never be delivered. For example, if the message is -an incoming call or video chat notification, it will only be meaningful for a -small period of time before the call is terminated. Or if the message is an -invitation to an event, it will be useless if received after the event has ended.</p> - -<p>Another advantage of specifying the expiration date for a message is that GCM -will never throttle messages with a <code>time_to_live</code> value of 0 seconds. -In other words, GCM will guarantee best effort for messages that must be delivered -"now or never." Keep in mind that a <code>time_to_live</code> value of -0 means messages that can't be delivered immediately will be discarded. However, -because such messages are never stored, this provides the best latency for -sending notifications.</p> - -<p>Here is an example of a JSON-formatted request that includes TTL:</p> -<pre class="prettyprint pretty-json"> -{ - "collapse_key" : "demo", - "delay_while_idle" : true, - "registration_ids" : ["xyz"], - "data" : { - "key1" : "value1", - "key2" : "value2", - }, - "time_to_live" : 3 -}, -</pre> - - -<h2 id="multi-senders">Receiving Messages from Multiple Senders</h2> - -<p>GCM allows multiple parties to send messages to the same application. For -example, suppose your application is an articles aggregator with multiple -contributors, and you want each of them to be able to send a message when they -publish a new article. This message might contain a URL so that the application -can download the article. Instead of having to centralize all sending activity in -one location, GCM gives you the ability to let each of these contributors send -its own messages.</p> - -<p>To make this possible, all you need to do is have each sender generate its own -project number. Then include those IDs in the sender field, separated by commas, -when requesting a registration. Finally, share the registration ID with your -partners, and they'll be able to send messages to your application using their -own authentication keys.</p> -<p>This code snippet illustrates this feature. Senders are passed as an intent -extra in a comma-separated list:</p> - -<pre class="prettyprint pretty-java">Intent intent = new Intent(GCMConstants.INTENT_TO_GCM_REGISTRATION); -intent.setPackage(GSF_PACKAGE); -intent.putExtra(GCMConstants.EXTRA_APPLICATION_PENDING_INTENT, - PendingIntent.getBroadcast(context, 0, new Intent(), 0)); -String senderIds = "968350041068,652183961211"; -intent.putExtra(GCMConstants.EXTRA_SENDER, senderIds); -ontext.startService(intent); - </pre> - -<p>Note that there is limit of 100 multiple senders.</p> diff --git a/docs/html/google/gcm/c2dm.jd b/docs/html/google/gcm/c2dm.jd index 5c4a86e..fc95c2b 100644 --- a/docs/html/google/gcm/c2dm.jd +++ b/docs/html/google/gcm/c2dm.jd @@ -79,7 +79,7 @@ page.title=Migration <h3 id="interop">Relationship between C2DM and GCM</h3> -<p>C2DM and GCM are not interoperable. For example, you cannot post notifications from GCM to C2DM registration IDs, nor can you use C2DM registration IDs as GCM registration IDs. From your server-side application, you must keep keep track of whether a registration ID is from C2DM or GCM and use the proper endpoint. </p> +<p>C2DM and GCM are not interoperable. For example, you cannot post notifications from GCM to C2DM registration IDs, nor can you use C2DM registration IDs as GCM registration IDs. From your server-side application, you must keep track of whether a registration ID is from C2DM or GCM and use the proper endpoint. </p> <p>As you transition from C2DM to GCM, your server needs to be aware of whether a given registration ID contains an old C2DM sender or a new GCM project number. This is the approach we recommend: have the new app version (the one that uses GCM) send a bit along with the registration ID. This bit tells your server that this registration ID is for GCM. If you don't get the extra bit, you mark the registration ID as C2DM. Once no more valid registration IDs are marked as C2DM, you can complete the migration.</p> @@ -87,13 +87,11 @@ contains an old C2DM sender or a new GCM project number. This is the approach we <h2 id="migrating">Migrating Your Apps</h2> <p>This section describes how to move existing C2DM apps to GCM.</p> <h3 id="client">Client changes</h3> -<p>Migration is simple! The only change required in the application is replacing the email account passed in the sender parameter of the registration intent with the project number generated when signing up for the new service. For example:</p> -<pre class="prettyprint pretty-java">Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER"); -// sets the app name in the intent -registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); -registrationIntent.putExtra("sender", senderID); -startService(registrationIntent);</pre> +<p>Migration is simple! Just re-register the client app for your target GCM-enabled platform. For + example, see <a href="{@docRoot}google/gcm/client.html#sample-register">Register for GCM</a></p> + <p>After receiving a response from GCM, the registration ID obtained must be sent to the application server. When doing this, the application should indicate that it is sending a GCM registration ID so that the server can distinguish it from existing C2DM registrations.</p> + <h3 id="server">Server changes</h3> <p>When the application server receives a GCM registration ID, it should store it and mark it as such.</p> <p>Sending messages to GCM devices requires a few changes:</p> diff --git a/docs/html/google/gcm/ccs.jd b/docs/html/google/gcm/ccs.jd index 6332b8d..143b057 100644 --- a/docs/html/google/gcm/ccs.jd +++ b/docs/html/google/gcm/ccs.jd @@ -36,6 +36,7 @@ page.title=GCM Cloud Connection Server (XMPP) <h2>See Also</h2> <ol class="toc"> +<li><a href="server-ref.html">Server Reference</a></li> <li><a href="{@docRoot}google/gcm/http.html">HTTP</a></li> <li><a href="{@docRoot}google/gcm/gs.html">Getting Started</a></li> <li><a href="{@docRoot}google/gcm/server.html">Implementing GCM Server</a></li> @@ -73,8 +74,8 @@ Play services platform. Upstream messaging is available through the APIs. For examples, see <a href="#implement">Implementing an XMPP-based App Server</a>.</p> -<p class="note"><strong>Note:</strong> See -<a href="server.html#params">Implementing GCM Server</a> for a list of all the message +<p class="note"><strong>Note:</strong> See the +<a href="server-ref.html">Server Reference</a> for a list of all the message parameters and which connection server(s) supports them.</p> <h2 id="connecting">Establishing a Connection</h2> @@ -176,8 +177,8 @@ operation and your server needs to resend the messages. See <a href="#flow">Flow Control</a> for details. </p> -<p class="note"><strong>Note:</strong> See -<a href="server.html#params">Implementing GCM Server</a> for a list of all the message +<p class="note"><strong>Note:</strong> See the +<a href="server-ref.html">Server Reference</a> for a list of all the message parameters and which connection server(s) supports them.</p> <h3 id="request">Request format</h3> @@ -278,56 +279,11 @@ message is "nack". A NACK message contains:</p> </message> </pre> -<p>The following table lists NACK error codes. Unless otherwise +<p>See the <a href="server-ref.html#table11">Server Reference</a> for a complete list of the +NACK error codes. Unless otherwise indicated, a NACKed message should not be retried. Unexpected NACK error codes should be treated the same as {@code INTERNAL_SERVER_ERROR}.</p> -<p class="table-caption" id="table1"> - <strong>Table 1.</strong> NACK error codes.</p> - -<table border="1"> -<tr> -<th>Error Code</th> -<th>Description</th> -</tr> -<tr> -<td>{@code BAD_ACK}</td> -<td>The ACK message is improperly formed.</td> -</tr> -<tr> -<td>{@code BAD_REGISTRATION}</td> -<td>The device has a registration ID, but it's invalid or expired.</td> -</tr> -<tr> -<td>{@code CONNECTION_DRAINING}</td> -<td>The message couldn't be processed because the connection is draining. The -message should be immediately retried over another connection.</td> -</tr> -<tr> -<td>{@code DEVICE_UNREGISTERED}</td> -<td>The device is not registered.</td> -</tr> -<tr> -<td>{@code INTERNAL_SERVER_ERROR}</td> -<td>The server encountered an error while trying to process the request.</td> -</tr> -<tr> -<td>{@code INVALID_JSON}</td> -<td>The JSON message payload is not valid.</td> -</tr> -<td>{@code DEVICE_MESSAGE_RATE_EXCEEDED}</td> -<td>The rate of messages to a particular device is too high. You should reduce -the number of messages sent to this device and should not immediately retry -sending to this device. This error code is replacing {@code QUOTA_EXCEEDED}.</td> -</tr> -<tr> - <td>{@code SERVICE_UNAVAILABLE}</td> - <td>CCS is not currently able to process the message. The - message should be retried over the same connection using exponential backoff - with an initial delay of 1 second.</td> -</tr> -</table> - <h4 id="stanza">Stanza error</h4> <p>You can also get a stanza error in certain cases. diff --git a/docs/html/google/gcm/client.jd b/docs/html/google/gcm/client.jd index d44ee3c..9cb3f84 100644 --- a/docs/html/google/gcm/client.jd +++ b/docs/html/google/gcm/client.jd @@ -1,4 +1,4 @@ -page.title=Implementing GCM Client +page.title=Implementing GCM Client on Android page.tags=cloud,push,messaging @jd:body @@ -15,8 +15,8 @@ page.tags=cloud,push,messaging <ol class="toc"> <li><a href="#sample-play">Check for Google Play Services APK</a></li> <li><a href="#sample-register">Register for GCM</a></li> - <li><a href="#sample-send">Send a message</a></li> - <li><a href="#sample-receive">Receive a message</a></li> + <li><a href="#sample-receive">Receive a downstream message</a></li> + <li><a href="#sample-send">Send an upstream message</a></li> </ol> <li><a href="#run">Running the Sample</a></li> <li><a href="#stats">Viewing Statistics</a></li> @@ -34,14 +34,26 @@ page.tags=cloud,push,messaging </div> </div> -<p>A Google Cloud Messaging (GCM) client is a GCM-enabled app that runs on an +<p>A Google Cloud Messaging (GCM) Android client is a GCM-enabled app that runs on an Android device. To write your client code, we recommend that you use the <a href="{@docRoot}reference/com/google/android/gms/gcm/package-summary.html"> -GCM APIs</a>. -The client helper library that was offered in previous versions of GCM still works, -but it has been superseded by the more efficient -<a href="{@docRoot}reference/com/google/android/gms/gcm/package-summary.html"> -GCM APIs</a>.</p> +{@code GoogleCloudMessaging}</a> API.</p> + +<p>Here are the requirements for running a GCM Android client:</p> + +<ul> + <li>At a bare minimum, GCM requires devices running Android 2.2 or higher that also have the +Google Play Store application installed, or an emulator running Android 2.2 +with Google APIs. Note that you are not limited to deploying your +Android applications through Google Play Store.</li> + <li>However, if you want to continue to use new GCM features that are distributed +through Google Play Services, the device must be running Android 2.3 or higher, or +you can use an emulator running Android 2.3 with Google APIs.</li> +<li>On Android devices, GCM uses an existing connection for Google services. For +pre-3.0 devices, this requires users to set up their Google accounts on their mobile +devices. A Google account is not a requirement on devices running Android 4.0.4 or higher.</li> + +</ul> <p>A full GCM implementation requires both a client implementation and a server implementation. For more @@ -58,7 +70,7 @@ registration ID), and a broadcast receiver to receive messages sent by GCM. <p>To write your client application, use the <a href="{@docRoot}reference/com/google/android/gms/gcm/package-summary.html"> -GCM APIs</a>. +{@code GoogleCloudMessaging}</a> API. To use this API, you must set up your project to use the Google Play services SDK, as described in <a href="/google/play-services/setup.html">Setup Google Play Services SDK</a>.</p> @@ -88,9 +100,6 @@ If you're using Android Studio, this is the string to add to the the Android application can register and receive messages.</li> <li>The <code>android.permission.INTERNET</code> permission so the Android application can send the registration ID to the 3rd party server.</li> - <li>The <code>android.permission.GET_ACCOUNTS</code> permission as GCM requires -a Google account (necessary only if if the device is running a version lower than -Android 4.0.4)</li> <li>The <code>android.permission.WAKE_LOCK</code> permission so the application can keep the processor from sleeping when a message is received. Optional—use only if the app wants to keep the device from sleeping.</li> @@ -101,7 +110,7 @@ pattern—otherwise the Android application will not receive the messages.</ <li>A receiver for <code>com.google.android.c2dm.intent.RECEIVE</code>, with the category set as <code>applicationPackage</code>. The receiver should require the -<code>com.google.android.c2dm.SEND</code> permission, so that only the GCM +<code>com.google.android.c2dm.permission.SEND</code> permission, so that only the GCM Framework can send a message to it. If your app uses an {@link android.app.IntentService} (not required, but a common pattern), this receiver should be an instance of {@link android.support.v4.content.WakefulBroadcastReceiver}. @@ -324,8 +333,8 @@ private String getRegistrationId(Context context) { return ""; } // Check if app was updated; if so, it must clear the registration ID - // since the existing regID is not guaranteed to work with the new - // app version. + // since the existing registration ID is not guaranteed to work with + // the new app version. int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE); int currentVersion = getAppVersion(context); if (registeredVersion != currentVersion) { @@ -340,14 +349,14 @@ private String getRegistrationId(Context context) { */ private SharedPreferences getGCMPreferences(Context context) { // This sample app persists the registration ID in shared preferences, but - // how you store the regID in your app is up to you. + // how you store the registration ID in your app is up to you. return getSharedPreferences(DemoActivity.class.getSimpleName(), Context.MODE_PRIVATE); }</pre> <p>If the registration ID doesn't exist or the app was updated, {@code getRegistrationId()} returns an empty string -to indicate that the app needs to get a new regID. {@code getRegistrationId()} calls +to indicate that the app needs to get a new registration ID. {@code getRegistrationId()} calls the following method to check the app version:</p> <pre>/** @@ -400,7 +409,7 @@ private void registerInBackground() { // will send upstream messages to a server that echo back the // message using the 'from' address in the message. - // Persist the regID - no need to register again. + // Persist the registration ID - no need to register again. storeRegistrationId(context, regid); } catch (IOException ex) { msg = "Error :" + ex.getMessage(); @@ -433,7 +442,8 @@ private void sendRegistrationIdToBackend() { <p>After registering, the app calls {@code storeRegistrationId()} to store the registration ID in shared preferences for future use. This is just one way of -persisting a regID. You might choose to use a different approach in your app:</p> +persisting a registration ID. You might choose to use a different approach in +your app:</p> <pre>/** * Stores the registration ID and app versionCode in the application's @@ -455,64 +465,22 @@ private void storeRegistrationId(Context context, String regId) { <h4 id="reg-errors">Handle registration errors</h4> <p>As stated above, an Android app must register with GCM servers and get a registration ID -(regID) before it can receive messages. A given regID is not guaranteed to last indefinitely, -so the first thing your app should always do is check to make sure it has a valid regID -(as shown in the code snippets above).</p> +before it can receive messages. A given registration ID is not guaranteed to last indefinitely, +so the first thing your app should always do is check to make sure it has a valid +registration ID (as shown in the code snippets above).</p> -<p>In addition to confirming that it has a valid regID, your app should be prepared to handle +<p>In addition to confirming that it has a valid registration ID, your app should be prepared to handle the registration error {@code TOO_MANY_REGISTRATIONS}. This error indicates that the device has too many apps registered with GCM. The error only occurs in cases where there are extreme numbers of apps, so it should not affect the average user. The remedy is to prompt -the user to delete some of the other GCM-enabled apps from the device to make +the user to delete some of the other client apps from the device to make room for the new one.</p> - -<h3 id="sample-send">Send a message</h3> -<p>When the user clicks the app's <strong>Send</strong> button, the app sends an -upstream message using the -<a href="{@docRoot}reference/com/google/android/gms/gcm/GoogleCloudMessaging.html"> -{@code GoogleCloudMessaging}</a> API. In order to receive the upstream message, -your server should be connected to CCS. You can use one of the demo servers in -<a href="ccs.html#implement">Implementing an XMPP-based App Server</a> to run the sample and connect -to CCS.</p> - -<pre>public void onClick(final View view) { - if (view == findViewById(R.id.send)) { - new AsyncTask<Void, Void, String>() { - @Override - protected String doInBackground(Void... params) { - String msg = ""; - try { - Bundle data = new Bundle(); - data.putString("my_message", "Hello World"); - data.putString("my_action", - "com.google.android.gcm.demo.app.ECHO_NOW"); - String id = Integer.toString(msgId.incrementAndGet()); - gcm.send(SENDER_ID + "@gcm.googleapis.com", id, data); - msg = "Sent message"; - } catch (IOException ex) { - msg = "Error :" + ex.getMessage(); - } - return msg; - } - - @Override - protected void onPostExecute(String msg) { - mDisplay.append(msg + "\n"); - } - }.execute(null, null, null); - } else if (view == findViewById(R.id.clear)) { - mDisplay.setText(""); - } -}</pre> - -<h3 id="sample-receive">Receive a message</h3> +<h3 id="sample-receive">Receive a downstream message</h3> <p>As described above in <a href="#manifest">Step 2</a>, the app includes a {@link android.support.v4.content.WakefulBroadcastReceiver} for the <code>com.google.android.c2dm.intent.RECEIVE</code> -intent. A broadcast receiver is the mechanism GCM uses to deliver messages. When {@code onClick()} -calls {@code gcm.send()}, it triggers the broadcast receiver's {@code onReceive()} -method, which has the responsibility of making sure that the GCM message gets handled.</p> +intent. A broadcast receiver is the mechanism GCM uses to deliver messages. </p> <p>A {@link android.support.v4.content.WakefulBroadcastReceiver} is a special type of broadcast receiver that takes care of creating and managing a @@ -646,6 +614,46 @@ public class GcmIntentService extends IntentService { } }</pre> + +<h3 id="sample-send">Send an upstream message</h3> +<p>When the user clicks the app's <strong>Send</strong> button, the app sends an +upstream message using the +<a href="{@docRoot}reference/com/google/android/gms/gcm/GoogleCloudMessaging.html"> +{@code GoogleCloudMessaging}</a> API. In order to receive the upstream message, +your server should be connected to CCS. You can use one of the demo servers in +<a href="ccs.html#implement">Implementing an XMPP-based App Server</a> to run the sample and connect +to CCS.</p> + +<pre>public void onClick(final View view) { + if (view == findViewById(R.id.send)) { + new AsyncTask<Void, Void, String>() { + @Override + protected String doInBackground(Void... params) { + String msg = ""; + try { + Bundle data = new Bundle(); + data.putString("my_message", "Hello World"); + data.putString("my_action", + "com.google.android.gcm.demo.app.ECHO_NOW"); + String id = Integer.toString(msgId.incrementAndGet()); + gcm.send(SENDER_ID + "@gcm.googleapis.com", id, data); + msg = "Sent message"; + } catch (IOException ex) { + msg = "Error :" + ex.getMessage(); + } + return msg; + } + + @Override + protected void onPostExecute(String msg) { + mDisplay.append(msg + "\n"); + } + }.execute(null, null, null); + } else if (view == findViewById(R.id.clear)) { + mDisplay.setText(""); + } +}</pre> + <h2 id="run">Running the Sample</h2> <p>To run the sample:</p> diff --git a/docs/html/google/gcm/gcm.jd b/docs/html/google/gcm/gcm.jd index 3d6594d..4e345b3 100644 --- a/docs/html/google/gcm/gcm.jd +++ b/docs/html/google/gcm/gcm.jd @@ -9,58 +9,24 @@ page.title=Overview <ol class="toc"> <li><a href="#key">Key Concepts</a></li> <li><a href="#arch">Architectural Overview</a></li> - <li><a href="#lifecycle">Lifecycle Flow</a> - <ol class="toc"> - <li><a href="#register">Enable GCM</a></li> - <li><a href="#push-process">Send a message</a></li> - <li><a href="#receiving">Receive a message</a></li> - </ol> - </li> + <li><a href="#lifecycle">Lifecycle Flow</a></li> + <li><a href="#reg">Register to enable GCM</a></li> </ol> </div> </div> -<p>Google Cloud Messaging (GCM) for Android is a free service that helps -developers send data from servers to their Android applications on Android -devices, and upstream messages from the user's device back to the cloud. -This could be a lightweight message telling the Android application +<p>Google Cloud Messaging (GCM) is a free service that enables developers +to send downstream messages (from servers to GCM-enabled client apps), and +upstream messages (from the GCM-enabled client apps to servers). +This could be a lightweight message telling the client app that there is new data to be fetched from the server (for instance, a "new email" -notification informing the application that it is out of sync with the back end), +notification informing the app that it is out of sync with the back end), or it could be a message containing up to 4kb of payload data (so apps like instant messaging can consume the message directly). The GCM -service handles all aspects of queueing of messages and delivery to the target -Android application running on the target device.</p> - -<p class="note"> To jump right into using GCM with your Android - applications, see <a href="gs.html">Getting Started</a>.</p> +service handles all aspects of queueing of messages and delivery to and from +the target client app.</p> -<p>Here are the primary characteristics of Google Cloud -Messaging (GCM):</p> - -<ul> - <li>It allows 3rd-party application servers to send messages to -their Android applications.</li> - <li>Using the <a href="ccs.html">GCM Cloud Connection Server</a>, you can receive -upstream messages from the user's device.</li> - <li>An Android application on an Android device doesn't need to be running to receive -messages. The system will wake up the Android application via Intent broadcast -when the message arrives, as long as the application is set up with the proper -broadcast receiver and permissions.</li> - <li>It does not provide any built-in user interface or other handling for -message data. GCM simply passes raw message data received straight to the -Android application, which has full control of how to handle it. For example, the -application might post a notification, display a custom user interface, or -silently sync data.</li> - <li>It requires devices running Android 2.2 or higher that also have the -Google Play Store application installed, or or an emulator running Android 2.2 -with Google APIs. However, you are not limited to deploying your -Android applications through Google Play Store.</li> - <li>It uses an existing connection for Google services. For pre-3.0 devices, -this requires users to -set up their Google account on their mobile devices. A Google account is not a -requirement on devices running Android 4.0.4 or higher.</li> -</ul> <h2 id="key">Key Concepts</h2> @@ -70,7 +36,7 @@ divided into these categories:</p> <li><strong>Components</strong> — The entities that play a primary role in GCM.</li> <li><strong>Credentials</strong> — The IDs and tokens that are used in -different stages of GCM to ensure that all parties have been authenticated, and +GCM to ensure that all parties have been authenticated, and that the message is going to the correct place.</li> </ul> @@ -81,24 +47,20 @@ that the message is going to the correct place.</li> <tr> <th colspan="2">Components</th> </tr> - <tr> - <td width="165"><strong>Client App</strong></td> - <td width="1176">The GCM-enabled Android application that is running on a - device. This must be a 2.2 Android device that has Google Play Store installed, and it must -have at least one logged in Google account if the device is running a version -lower than Android 4.0.4. Alternatively, for testing you can use an emulator -running Android 2.2 with Google APIs.</td> +<tr> + <td><strong>GCM Connection Servers</strong></td> + <td>The Google-provided servers involved in sending messages between the +3rd-party app server and the client app.</td> </tr> <tr> - <td><strong>3rd-party Application Server</strong></td> - <td>An application server that you write as part of implementing -GCM. The 3rd-party application server sends data to an -Android application on the device via the GCM connection server.</td> + <td><strong>Client App</strong></td> + <td>A GCM-enabled client app that communicates with a 3rd-party app server.</td> </tr> <tr> - <td><strong>GCM Connection Servers</strong></td> - <td>The Google-provided servers involved in taking messages from the 3rd-party -application server and sending them to the device. </td> + <td><strong>3rd-party App Server</strong></td> + <td>An app server that you write as part of implementing +GCM. The 3rd-party app server sends data to a client app via +the GCM connection server.</td> </tr> <tr> <th colspan="2">Credentials</th> @@ -108,43 +70,29 @@ application server and sending them to the device. </td> <td>A project number you acquire from the API console, as described in <a href="gs.html#create-proj">Getting Started</a>. The sender ID is used in the <a href="#register">registration process</a> to identify a -3rd-party application server that is permitted to send messages to the device.</td> +3rd-party app server that is permitted to send messages to the client app.</td> + </tr> + <tr> + <td id="apikey"><strong>Sender Auth Token</strong></td> + <td>An API key that is saved on the 3rd-party app +server that gives the app server authorized access to Google services. +The API key is included in the header of POST requests. +</td> </tr> <tr> <td><strong>Application ID</strong></td> - <td>The Android application that is registering to receive messages. The Android application + <td>The client app that is registering to receive messages. How this is implemented +is platform-dependent. For example, an Android app is identified by the package name from the <a href="client.html#manifest">manifest</a>. -This ensures that the messages are targeted to the correct Android application.</td> +This ensures that the messages are targeted to the correct Android app.</td> </tr> <tr> <td><strong>Registration ID</strong></td> - <td>An ID issued by the GCM servers to the Android application that allows -it to receive messages. Once the Android application has the registration ID, it sends -it to the 3rd-party application server, which uses it to identify each device -that has registered to receive messages for a given Android application. In other words, -a registration ID is tied to a particular Android application running on a particular -device. Note that registration IDs must be kept secret. -<br/> -<br/> -<strong>Note:</strong> If you use -<a href="https://developer.android.com/google/backup/index.html">backup and restore</a>, -you should explicitly avoid backing up registration IDs. When you back up -a device, apps back up shared prefs indiscriminately. If you don't explicitly -exclude the GCM registration ID, it could get reused on a new device, -which would cause delivery errors. + <td>An ID issued by the GCM servers to the client app that allows +it to receive messages. Note that registration IDs must be kept secret. + </td> </tr> - <tr> - <td><strong>Google User Account</strong></td> - <td>For GCM to work, the mobile device must include at least one Google -account if the device is running a version lower than Android 4.0.4.</td> - </tr> - <tr> - <td id="apikey"><strong>Sender Auth Token</strong></td> - <td>An API key that is saved on the 3rd-party application -server that gives the application server authorized access to Google services. -The API key is included in the header of POST requests that send messages.</td> - </tr> </table> @@ -152,7 +100,8 @@ The API key is included in the header of POST requests that send messages.</td> <p>A GCM implementation includes a Google-provided connection server, a 3rd-party app server that interacts with the connection -server, and a GCM-enabled client app running on an Android device:</p> +server, and a GCM-enabled client app. For example, this diagram shows GCM +communicating with a client app on an Android device:</p> <img src="{@docRoot}images/gcm/GCM-arch.png"> @@ -163,84 +112,196 @@ server, and a GCM-enabled client app running on an Android device:</p> <p>This is how these components interact:</p> <ul> <li>Google-provided <strong>GCM Connection Servers</strong> take messages from -a 3rd-party application server and send these messages to a -GCM-enabled Android application (the "client app") running on a device. +a 3rd-party app server and send these messages to a +GCM-enabled client app (the "client app"). Currently Google provides connection servers for <a href="http.html">HTTP</a> and <a href="ccs.html">XMPP</a>.</li> - <li>The <strong>3rd-Party Application Server</strong> is a component that you + <li>The <strong>3rd-Party App Server</strong> is a component that you implement to work with your chosen GCM connection server(s). App servers send messages to a GCM connection server; the connection server enqueues and stores the -message, and then sends it to the device when the device is online. +message, and then sends it to the client app. For more information, see <a href="server.html">Implementing GCM Server</a>.</li> - <li>The <strong>Client App</strong> is a GCM-enabled Android application running -on a device. To receive GCM messages, this app must register with GCM and get a + <li>The <strong>Client App</strong> is a GCM-enabled client app. +To receive GCM messages, this app must register with GCM and get a registration ID. If you are using the <a href="ccs.html">XMPP</a> (CCS) connection -server, the client app can send "upstream" messages back to the connection server. +server, the client app can send "upstream" messages back to the 3rd-party app server. For more information on how to implement the client app, see -<a href="client.html">Implementing GCM Client</a>.</li> +the documentation for your platform.</li> </ul> <h2 id="lifecycle">Lifecycle Flow</h2> <ul> - <li><a href="#register">Enable GCM</a>. An Android application running on a -mobile device registers to receive messages.</li> + <li><strong>Register to enable GCM</strong>. A client app registers to receive messages. +For more discussion, see <a href="#register">Register to enable GCM</a>.</li> + <li><strong>Send and receive downstream messages</strong>. + <ul> + <li>Send a message. A 3rd-party app server sends messages to the client app: + <ol> + <li>The 3rd-party app server <a href="server.html#send-msg">sends a message</a> +to GCM connection servers.</li> + <li>The GCM connection server enqueues and stores the message if the device is offline.</li> + <li>When the device is online, the GCM connection server sends the message to the device. </li> + <li>On the device, the client app receives the message according to the platform-specific implementation. +See your platform-specific documentation for details.</li> + </ol> + </li> + <li>Receive a message. A client app +receives a message from a GCM server. See your platform-specific documentation for details +on how a client app in that environment processes the messages it receives.</li> + </ul> +</li> + + <li><strong>Send and receive upstream messages</strong>. This feature is only available if +you're using the <a href="ccs.html">XMPP Cloud Connection Server</a> (CCS). +<ul> + <li>Send a message. A client app sends messages to the 3rd-party app server: + <ol> + <li>On the device, the client app sends messages to XMPP (CCS).See your platform-specific + documentation for details on how a client app can send a message to XMPP (CCS).</li> + <li>XMPP (CCS) enqueues and stores the message if the server is disconnected.</li> + <li>When the 3rd-party app server is re-connected, XMPP (CCS) sends the message to the 3rd-party app server.</li> + </ol> + </li> + <li>Receive a message. A 3rd-party app server receives a message from XMPP (CCS) and then does the following: + <ol> + <li>Parses the message header to verify client app sender information. + <li>Sends "ack" to GCM XMPP connection server to acknowledge receiving the message. + <li>Optionally parses the message payload, as defined by the client app. + </ol> +</li> +</ul> + +</li> + - <li><a href="#push-process">Send a message</a>. A 3rd-party application -server sends messages to the device.</li> - <li><a href="#receiving">Receive a message</a>. An Android application -receives a message from a GCM server.</li> </ul> -<p>These processes are described in more detail below.</p> +<h2 id="reg">Register to enable GCM</h2> + +<p>Regardless of the platform you're developing on, the first step +a client app must do is register with GCM. This section covers some of the general +best practices for registration and unregistration. See your platform-specific docs for +details on writing a GCM-enabled client app on that platform.</p> -<h3 id="register">Enable GCM</h3> +<h3 id="reg-state">Keeping the Registration State in Sync</h3> +<p>Whenever the app registers as described in +<a href="{@docRoot}google/gcm/client.html">Implementing GCM Client</a>, +it should save the registration ID for future use, pass it to the +3rd-party server to complete the registration, and keep track of +whether the server completed the registration. If the server fails +to complete the registration, the client app should retry passing the +registration ID to 3rd-party app server to complete the registration. +If this continues to fail, the client app should unregister from GCM.</p> -<p>The first time the Android application needs to use the messaging service, it -calls the <a href="{@docRoot}reference/com/google/android/gms/gcm/GoogleCloudMessaging.html"> -{@code GoogleCloudMessaging}</a> method {@code register()}, as discussed in -<a href="client.html">Implementing GCM Client</a>. -The {@code register()} method returns a registration ID. The Android -application should store this ID for later use (for instance, -to check in <code>onCreate()</code> if it is already registered). +<p>There are also two other scenarios that require special care:</p> +<ul> + <li>Client app update</li> + <li>Backup and restore + </li> +</ul> +<p><bold>Client app update:</bold> When a client app is updated, it should invalidate its existing registration +ID, as it is not guaranteed to work with the new version. The recommended way to achieve +this validation is by storing the current app version when a registration +ID is stored. Then when the app starts, compare the stored value with +the current app version. If they do not match, invalidate the stored data +and start the registration process again.</p> + +<p><bold>Backup and restore: </bold> You should not save the registration ID when an app is +backed up. This is because the registration ID could become invalid by the time +the app is restored, which would put the app in an invalid state +(that is, the app thinks it is registered, but the server and GCM do not +store that registration ID anymore—thus the app will not get more +messages). The best practice is to initiate the registration process as if the app has been +installed for the first time.</p> + +<h4 id="canonical">Canonical IDs</h4> +<p>If a bug in the app triggers multiple +registrations for the same device, it can be hard to reconcile state and you might +end up with duplicate messages.</p> +<p>GCM provides a facility called "canonical registration IDs" to easily +recover from these situations. A canonical registration ID is defined to be the ID +of the last registration requested by your app. This is the ID that the +server should use when sending messages to the device.</p> +<p>If later on you try to send a message using a different registration ID, GCM +will process the request as usual, but it will include the canonical registration +ID in the <code>registration_id</code> field of the response. Make sure to replace +the registration ID stored in your server with this canonical ID, as eventually +the ID you're using will stop working.</p> + +<h3 id="retry">Automatic Retry Using Exponential Back-Off</h3> + +<p>When registration or unregistration fails, the app should retry the failed operation.</p> +<p>In the simplest case, if your app attempts to register and GCM is not a +fundamental part of the app, the app could simply ignore the error +and try to register again the next time it starts. Otherwise, it should retry the +previous operation using exponential back-off. In exponential back-off, each time +there is a failure, it should wait twice the previous amount of time before trying +again. </p> -<h3 id="push-process">Send a message</h3> +<h3 id="unreg">Unregistration</h3> -<p>Here is the sequence of events that occurs when the application server sends a -message:</p> +<p>This section explains when you should unregister in GCM and what happens +when you do.</p> -<ol> - <li>The application server sends a message to GCM servers.</li> - <li>Google enqueues and stores the message in case the device is -offline.</li> - <li>When the device is online, Google sends the message to the device. </li> - <li>On the device, the system broadcasts the message to the specified -Android application via Intent broadcast with proper permissions, so that only the -targeted Android application gets the message. This wakes the Android application up. The -Android application does not need to be running beforehand to receive the message.</li> - <li>The Android application processes the message. If the Android application is doing -non-trivial processing, you may want to grab a -{@link android.os.PowerManager.WakeLock} and do any processing in a service.</li> -</ol> +<h4 id="unreg-why">Why you should rarely unregister</h4> + +<p>You should only need to unregister in rare cases, such as +if you want an app to stop receiving messages, or if you suspect that the registration ID has +been compromised. In general, once an app has a registration ID, you shouldn't need +to change it.</p> -<p> An Android application can unregister GCM if it no longer wants to receive -messages.</p> +<p>In particular, you should never unregister your app as a mechanism for +logout or for switching between users, for the following reasons:</p> + +<ul> + <li>A registration ID isn't associated with a particular + logged in user. If you unregister and then re-register, GCM may return the same + ID or a different ID—there's no guarantee either way.</li> + + <li>Unregistration may take up to 5 minutes to propagate.</li> + <li>After unregistration, re-registration may again take up to 5 minutes to +propagate. During this time messages may be rejected due to the state of being +unregistered, and after all this, messages may still go to the wrong user.</li> +</ul> -<h3 id="receiving">Receive a message</h3> -<p>This is the sequence of events that occurs when an Android application -installed on a mobile device receives a message:</p> +<p>To make sure that messages go to the intended user:</p> +<ul> + <li>Your app server can maintain a mapping between the current user +and the registration ID.</li> + <li>The app can then check to ensure that messages it +receives match the logged in user.</li> +</ul> + + +<h4 id="unreg-how">How unregistration works</h4> + +<p>An app can be automatically unregistered after it is uninstalled. +However, this process does not happen right away. What happens in +this scenario is as follows:</p> <ol> - <li>The system receives the incoming message and extracts the raw key/value -pairs from the message payload, if any.</li> - <li>The system passes the key/value pairs to the targeted Android application -in a <code>com.google.android.c2dm.intent.RECEIVE</code> Intent as a set of -extras.</li> - <li>The Android application extracts the raw data -from the <code>com.google.android.c2dm.intent.RECEIVE</code><code> </code>Intent -by key and processes the data.</li> + <li>The end user uninstalls the client app.</li> + <li>The 3rd-party app server sends a message to GCM server.</li> + <li>The GCM server sends the message to the GCM client on the device.</li> + <li>The GCM client on the device receives the message and detects that the client app has been + uninstalled; the detection details depend on the platform on which the client app is running. +</li> + <li>The GCM client on the device informs the GCM server that the client app was uninstalled.</li> + <li>The GCM server marks the registration ID for deletion.</li> + <li>The 3rd-party app server sends a message to GCM.</li> + <li>The GCM returns a <code>NotRegistered</code> error message to the 3rd-party app server.</li> + <li>The 3rd-party app server deletes the registration ID. + </li> </ol> +<p>Note that it might take a while for the registration ID be completely removed +from GCM. Thus it is possible that messages sent during step 7 above gets a valid +message ID as response, even though the message will not be delivered to the client app. +Eventually, the registration ID will be removed and the server will get a +<code>NotRegistered</code> error, without any further action being required from +the 3rd-party server (this scenario happens frequently while an app is +being developed and tested).</p> diff --git a/docs/html/google/gcm/gs.jd b/docs/html/google/gcm/gs.jd index a889624..2331292 100644 --- a/docs/html/google/gcm/gs.jd +++ b/docs/html/google/gcm/gs.jd @@ -1,4 +1,4 @@ -page.title=Getting Started +page.title=Getting Started on Android page.tags=cloud,push,messaging @jd:body @@ -46,7 +46,7 @@ project number. For example, <strong>Project Number: 670330094152</strong>.</p>< <li>Copy down your project number. You will use it later on as the <a href="{@docRoot}google/gcm/gcm.html#senderid">GCM sender ID</a>.</li> - + </ol> <h2 id="gcm-service">Enabling the GCM Service</h2> <p>To enable the GCM service:</p> @@ -71,7 +71,7 @@ purposes, you can use {@code 0.0.0.0/0}.</p></li> <li>In the refreshed page, copy the <a href="{@docRoot}google/gcm/gcm.html#apikey">API key</a>. -You will need the API key later on to perform authentication in your application server.</li> +You will need the API key later on to perform authentication in your app server.</li> <p class="note"><strong>Note:</strong> If you need to rotate the key, click <strong>Regenerate key</strong>. A new key will be created. If you think the key has been @@ -84,16 +84,11 @@ compromised and you want to delete it immediately, click <strong>Delete</strong> implementing GCM. Here is an overview of the basic steps:</p> <ol> - <li>Decide which Google-provided GCM connection server you want to use— - <a href="http.html">HTTP</a> or <a href="ccs.html">XMPP</a> (CCS). GCM connection servers -take messages from a 3rd-party application -server (written by you) and send them to a GCM-enabled Android application (the -"client app," also written by you) running on a device. </li> - <li>Implement an application server (the "3rd-party application server") to interact + <li>Implement an app server (the "3rd-party app server") to interact with your chosen GCM connection server. The app server sends data to a -GCM-enabled Android client application via the GCM connection server. For more +GCM-enabled Android client app via the GCM connection server. For more information about implementing the server side, see <a href="server.html"> Implementing GCM Server</a>.</li> -<li>Write your client app. This is the GCM-enabled Android application that runs +<li>Write your client app. This is the GCM-enabled Android app that runs on a device. See <a href="client.html">Implementing GCM Client</a> for more information.</li> </ol> diff --git a/docs/html/google/gcm/http.jd b/docs/html/google/gcm/http.jd index 773acd1..5022e09 100644 --- a/docs/html/google/gcm/http.jd +++ b/docs/html/google/gcm/http.jd @@ -23,6 +23,7 @@ page.title=GCM HTTP Connection Server <h2>See Also</h2> <ol class="toc"> +<li><a href="server-ref.html">Server Reference</a></li> <li><a href="gs.html">Getting Started</a></li> <li><a href="client.html">Implementing GCM Client</a></li> <li><a href="ccs.html">Cloud Connection Server</a></li> @@ -42,26 +43,30 @@ application server and sending them to the device.</p> applies to <a href="http://developer.chrome.com/apps/cloudMessaging"> GCM with Chrome apps</a> as well as Android.</p> -<p>See -<a href="server.html#params">Implementing GCM Server</a> for a list of all the message +<p>See the +<a href="server-ref.html">Server Reference</a> for a list of all the message parameters and which connection server(s) supports them.</p> <h2 id="auth">Authentication</h2> -<p>To send a message, the application server issues a POST request to -<code>https://android.googleapis.com/gcm/send</code>.</p> +<p>To send a message, the application server issues a POST request. For example:</p> +<pre>https://android.googleapis.com/gcm/send</pre> <p>A message request is made of 2 parts: HTTP header and HTTP body.</p> <p>The HTTP header must contain the following headers:</p> <ul> <li><code>Authorization</code>: key=YOUR_API_KEY</li> - <li><code>Content-Type</code>: <code>application/json</code> for JSON; <code>application/x-www-form-urlencoded;charset=UTF-8</code> for plain text. + <li><code>Content-Type</code>: <code>application/json</code> for JSON; +<code>application/x-www-form-urlencoded;charset=UTF-8</code> for plain text. +If <code>Content-Type</code> is omitted, the format +is assumed to be plain text. </li> </ul> <p>For example: </p> + <pre>Content-Type:application/json Authorization:key=AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA @@ -71,26 +76,30 @@ Authorization:key=AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA ... }, }</pre> -<p class="note"> - <p><strong>Note:</strong> If <code>Content-Type</code> is omitted, the format -is assumed to be plain text.</p> -</p> + <p>The HTTP body content depends on whether you're using JSON or plain text. See -<a href="server.html#params">Implementing GCM Server</a> for a list of all the +<a href="server-ref.html#params">the Server Reference</a> for a list of all the parameters your JSON or plain text message can contain.</p> <h2 id="request">Request Format</h2> + +<p>This section shows you how to format a request for both JSON and plain text. See +the <a href="server-ref.html#table1">Server Reference</a> for a complete +list of the fields you can include in a request.</p> + <p>Here is the smallest possible request (a message without any parameters and just one recipient) using JSON:</p> + <pre class="prettyprint pretty-json">{ "registration_ids": [ "42" ] }</pre> <p>And here the same example using plain text:</p> <pre class="prettyprint">registration_id=42</pre> <p> Here is a message with a payload and 6 recipients:</p> + <pre class="prettyprint pretty-HTML">{ "data": { "score": "5x1", "time": "15:10" @@ -112,10 +121,25 @@ just one recipient) using JSON:</p> <pre class="prettyprint">collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.score=4x8&data.time=15:16.2342&registration_id=42 </pre> +<p>Here is a message that includes a notification key and payload:</p> + +<pre> +{ + "data": { + "message": "ciao" + }, + "notification_key":"aUniqueKey" +} +</pre> + +<p>For more information about notifications and how to use them, see +<a href="{@docRoot}google/gcm/notifications.html">User Notifications</a>.</p> + + <p class="note"><strong>Note:</strong> If your organization has a firewall that restricts the traffic to or from the Internet, you need to configure it to allow connectivity with GCM in order for -your Android devices to receive messages. +your GCM client apps to receive messages. The ports to open are: 5228, 5229, and 5230. GCM typically only uses 5228, but it sometimes uses 5229 and 5230. GCM doesn't provide specific IPs, so you should allow your firewall to accept outgoing connections to all IP addresses @@ -127,54 +151,12 @@ contained in the IP blocks listed in Google's ASN of 15169.</p> <p>There are two possible outcomes when trying to send a message:</p> <ul> - <li>The message is processed successfully.</li> - <li>The GCM server rejects the request.</li> + <li>The message is processed successfully. The HTTP response has a 200 status, and +the body contains more information about the status of the message (including possible errors).</li> + <li>The GCM server rejects the request. The HTTP response contains a +non-200 status code (such as 400, 401 or 5xx).</li> </ul> -<p>When the message is processed successfully, the HTTP response has a 200 status -and the body contains more information about the status of the message -(including possible errors). When the request is rejected, -the HTTP response contains a non-200 status code (such as 400, 401, or 503).</p> - -<p>The following table summarizes the statuses that the HTTP response header might -contain. Click the troubleshoot link for advice on how to deal with each type of -error.</p> -<table border=1> - <tr> - <th>Response</th> - <th>Description</th> - </tr> - <tr> - <td>200</td> - <td>Message was processed successfully. The response body will contain more -details about the message status, but its format will depend whether the request -was JSON or plain text. See <a href="#success">Interpreting a success response</a> -for more details.</td> - </tr> - <tr> - <td>400</td> - <td><span id="internal-source-marker_0.2">Only applies for JSON requests. -Indicates that the request could not be parsed as JSON, or it contained invalid -fields (for instance, passing a string where a number was expected). The exact -failure reason is described in the response and the problem should be addressed -before the request can be retried.</td> - </tr> - <tr> - <td>401</td> - <td>There was an error authenticating the sender account. -<a href="#auth_error">Troubleshoot</a></td> - </tr> - <tr> - <td>5xx</td> - <td>Errors in the 500-599 range (such as 500 or 503) indicate that there was -an internal error in the GCM server while trying to process the request, or that -the server is temporarily unavailable (for example, because of timeouts). Sender -must retry later, honoring any <code>Retry-After</code> header included in the -response. Application servers must implement exponential back-off. -<a href="#internal_error">Troubleshoot</a></td> - </tr> -</table> - <h3 id="success">Interpreting a success response</h3> <p>When a JSON request is successful (HTTP status code 200), the response body contains a JSON object with the following fields:</p> @@ -241,8 +223,8 @@ code>registration_ids</code> passed in the request (using the same index).</li> request.</li> <li>If it is <code>NotRegistered</code>, you should remove the registration ID from your server database because the application was uninstalled from the -device or it does not have a broadcast receiver configured to receive -<code>com.google.android.c2dm.intent.RECEIVE</code> intents.</li> +device, or the client app isn't configured to receive +messages.</li> <li>Otherwise, there is something wrong in the registration ID passed in the request; it is probably a non-recoverable error that will also require removing the registration from the server database. See <a href="#error_codes">Interpreting @@ -291,8 +273,7 @@ might occur when trying to send a message to a device:</p> <dt id="invalid_reg"><strong>Invalid Registration ID</strong></dt> <dd>Check the formatting of the registration ID that you pass to the server. Make -sure it matches the registration ID the phone receives in the -<code>com.google.android.c2dm.intent.REGISTRATION</code> intent and that you're +sure it matches the registration ID the client app receives and that you're not truncating it or adding additional characters. <br/>Happens when error code is <code>InvalidRegistration</code>.</dd> @@ -306,17 +287,13 @@ Happens when error code is <code>MismatchSenderId</code>.</dd> <dt id="unreg_device"><strong>Unregistered Device</strong></dt> <dd>An existing registration ID may cease to be valid in a number of scenarios, including: <ul> - <li>If the application manually unregisters by issuing a -<span class="prettyprint pretty-java"> -<code>com.google.android.c2dm.intent.UNREGISTER</code></span><code> -</code>intent.</li> + <li>If the application manually unregisters.</li> <li>If the application is automatically unregistered, which can happen (but is not guaranteed) if the user uninstalls the application.</li> <li>If the registration ID expires. Google might decide to refresh registration IDs. </li> - <li>If the application is updated but the new version does not have a broadcast -receiver configured to receive <code>com.google.android.c2dm.intent.RECEIVE</code> -intents.</li> + <li>If the application is updated but the new version is not configured to receive +messages.</li> </ul> For all these cases, you should remove this registration ID from the 3rd-party server and stop using it to send @@ -331,8 +308,7 @@ as the values. <dt id="invalid_datakey"><strong>Invalid Data Key</strong></dt> <dd>The payload data contains a key (such as <code>from</code> or any value -prefixed by <code>google.</code>) that is used internally by GCM in the -<code>com.google.android.c2dm.intent.RECEIVE</code> Intent and cannot be used. +prefixed by <code>google.</code>) that is used internally by GCM and therefore cannot be used. Note that some words (such as <code>collapse_key</code>) are also used by GCM but are allowed in the payload, in which case the payload value will be overridden by the GCM value. @@ -354,9 +330,9 @@ authenticated. Possible causes are: <ul> <li>Request originated from a server not whitelisted in the Server Key IPs.</li> </ul> -Check that the token you're sending inside the <code>Authorization</code> header -is the correct API key associated with your project. You can check the validity -of your API key by running the following command:<br/> +When there is an Authentication Error, you can check the validity of your API key by running You can check the validity +of your API key by running the following command (this example shows what you +would do on Android; see the documentation for your platform):<br/> <pre># api_key=YOUR_API_KEY @@ -405,8 +381,7 @@ Happens when the HTTP status code is between 501 and 599, or when the <dd> The server encountered an error while trying to process the request. You could retry the same request (obeying the requirements listed in the <a href="#timeout">Timeout</a> -section), but if the error persists, please report the problem in the -<a href="https://groups.google.com/forum/?fromgroups#!forum/android-gcm">android-gcm group</a>. +section), but if the error persists, please report the problem to Google. <br /> Happens when the HTTP status code is 500, or when the <code>error</code> field of a JSON object in the results array is <code>InternalServerError</code>. @@ -488,7 +463,7 @@ registration_id=32 <p>This section gives examples of implementing an app server that works with the GCM HTTP connection server. Note that a full GCM implementation requires a -client-side implementation, in addition to the server.</a> +client-side implementation, in addition to the server. This example is based on Android.</a> <p>Requirements</p> @@ -508,7 +483,7 @@ version 1.6 or later.</li> </ul> <p>For the Android application:</p> <ul> - <li>Emulator (or device) running Android 2.2 with Google APIs.</li> + <li>Emulator (or device) running Android 2.2 (ideally, 2.3 or above) with Google APIs.</li> <li>The Google API project number of the account registered to use GCM.</li> </ul> diff --git a/docs/html/google/gcm/notifications.jd b/docs/html/google/gcm/notifications.jd index 147b69c..333d4b6 100644 --- a/docs/html/google/gcm/notifications.jd +++ b/docs/html/google/gcm/notifications.jd @@ -306,9 +306,3 @@ receive the message:</p> <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> - - - - - - diff --git a/docs/html/google/gcm/server-ref.jd b/docs/html/google/gcm/server-ref.jd new file mode 100644 index 0000000..a94e727 --- /dev/null +++ b/docs/html/google/gcm/server-ref.jd @@ -0,0 +1,763 @@ +page.title=Server Reference +@jd:body + +<div id="qv-wrapper"> +<div id="qv"> + +<h2>In this document</h2> + +<ol class="toc"> + <li><a href="#downstream">Downstream Messages</a></li> +<ol class="toc"> + <li><a href="#send-downstream">Sending a downstream message</a></li> + <li><a href="#interpret-downstream">Interpreting a downstream message response</a></li> + </ol> + <li><a href="#upstream">Upstream Messages (XMPP)</a> + <ol class="toc"> + <li><a href="#interpret-upstream">Interpreting an upstream XMPP message</a></li> + <li><a href="#upstream-response">Sending an upstream XMPP message response</a></li> + </ol> + </li> +<li><a href="#ccs">Cloud Connection Server Messages (XMPP)</a></li> +<li><a href="#error-codes">Downstream message error response codes (HTTP and XMPP)</a></li> +</ol> + +</div> +</div> + +<p>This document provides a reference for the syntax used to pass +messages back and forth in GCM. These messages fall into +the following broad categories:</p> + +<ul> + <li><a href="#downstream">Downstream messages</a></li> + <li><a href="#upstream">Upstream messages</a></li> + <li><a href="#ccs">Cloud Connection Server messages (XMPP)</a></li> + <li><a href="#error-codes">Downstream message error response codes (HTTP and XMPP)</a></li> +</ul> + +<p>The following sections describe the basic requirements for +sending messages.</p> + +<h2 id="downstream">Downstream Messages</h2> +<p>This is the message that a 3rd-party app server sends to a client app. +</p> +<p>A downstream message includes the following components:</p> +<ul> + <li>Target: specifies the recipient of the message.</li> + <li>Options: specifies attributes of the message.</li> + <li>Payload: specifies additional content to be included in the message. Optional.</li> +</ul> + +<p>The syntax for each of these components is described in the tables below. </p> + +<h3 id="send-downstream">Sending a downstream message</h3> + +<p>This section gives the syntax for sending a downstream messages. For JSON, +these messages can be either HTTP or XMPP. For plain text, these messages can only be HTTP.</p> + +<h4>Downstream HTTP or XMPP messages (JSON)</h4> + +<p>The following table lists the targets, options, and payload for HTTP or XMPP JSON messages.</p> +<p class="table-caption" id="table1"> + <strong>Table 1.</strong> Targets, options, and payload for downstream HTTP or XMPP message (JSON).</p> +<table border="1"> + <tr> + <th>Parameter</th> + <th>Protocol</th> + <th>Usage</th> + <th>Description</th> + </tr> +<tr> + <td colspan="4"><strong>Targets</strong></td> + </tr> + <tr> + <td><code>to</code></td> + <td>XMPP</td> + <td>Required, string</td> + <td><p>This parameter specifies the recipient of a message. </p> + <p>The value must be a registration ID or notification key.</p> + <p>This parameter is used in XMPP in place of {@code registration_ids} or {@code notification_key} in HTTP.</p></td> + </tr> + <tr> + <td><code>registration_ids</code></td> + <td>HTTP</td> + <td>Required if {@code notification_key} not present, string array</td> + <td><p>This parameter specifies the list of devices (registration IDs) +receiving the message. It must contain at least 1 and at most 1000 registration IDs.</p> + <p>Multicast messages (sending to more than 1 registration IDs) are allowed using HTTP JSON format only.</p> + <p>This parameter or {@code notification_key} is used in HTTP in place of {@code to} in XMPP.</p></td> + </tr> + <tr> + <td><code>notification_key</code></td> + <td>HTTP</td> + <td>Required if {@code registration_ids} not present, string</td> + <td><p>This parameter specifies the mapping of a single user to +multiple registration IDs associated with that user.</p> + <p>This allows a 3rd-party app server to send a single message to multiple app instances +(typically on multiple devices) owned by a single user.</p> + <p>A 3rd-party app server can use {@code notification_key} as the target for a +message instead of an individual registration ID (or array of registration IDs). +The maximum number of members allowed for a {@code notification_key} is 20.</p> + <p>This parameter or {@code registration_ids} is used in HTTP in place of {@code to} in XMPP.</p> + <p>See <a href="notifications.html">User Notifications</a> for details.</p></td> + </tr> +<tr> + <td colspan="4"><strong>Options</strong></td> + </tr> + <tr> + <td><code>message_id</code></td> + <td>XMPP</td> + <td>Required, string</td> + <td><p>This parameter uniquely identifies a message in an XMPP connection.</p></td> + </tr> + <tr> + <td><code>collapse_key</code></td> + <td>HTTP, XMPP</td> + <td>Optional, string</td> + <td><p>This parameters identifies a group of messages (e.g., with +{@code collapse_key: "Updates Available"}) that can be collapsed, so that only the +last message gets sent when delivery can be resumed. This is intended to avoid sending too +many of the same messages when the device comes back online or becomes active (see {@code delay_while_idle}).</p> + <p>Note that there is no guarantee of the order in which messages get sent.</p> + <p>Messages with collapse key are also called +<a href="{@docRoot}google/gcm/server.html#s2s">send-to-sync messages</a> messages. +</p> + <p>Note: A maximum of 4 different collapse keys is allowed at any given time. This means a +GCM connection server can simultaneously store 4 different send-to-sync messages per client app. If you +exceed this number, there is no guarantee which 4 collapse keys the GCM connection server will keep. </p></td> + </tr> + <tr> + <td><code>delay_while_idle</code></td> + <td>HTTP, XMPP</td> + <td>Optional, JSON boolean</td> + <td>When this parameter is set to {@code true}, it indicates that the message should not be +sent until the device becomes active.</p> + <p>The default value is {@code false}.</p></td> + </tr> + <tr> + <td><code>time_to_live</code></td> + <td>HTTP, XMPP</td> + <td>Optional, JSON number</td> + <td><p>This parameter specifies how long (in seconds) the message should be kept in GCM storage +if the device is offline. The maximum time to live supported is 4 weeks.</p> + <p>The default value is 4 weeks. </p></td> + </tr> + <tr> + <td><code>delivery_receipt_ +<br>requested</code></td> + <td>XMPP</td> + <td>Optional, JSON boolean</td> + <td><p>This parameter lets 3rd-party app server request confirmation of message delivery.</p> + <p>When this parameter is set to {@code true}, CCS sends a delivery receipt +when the device confirms that it received the message.</p> + <p>The default value is {@code false}.</p></td> + </tr> + <tr> + <td><code>restricted_package_ +<br>name</code></td> + <td>HTTP</td> + <td>Optional, string</td> + <td>This parameter specifies the package name of the application where the +registration IDs must match in order to receive the message.</td> + </tr> + <tr> + <td><code>dry_run</code></td> + <td>HTTP</td> + <td>Optional, JSON boolean</td> + <td><p>This parameter, when set to {@code true}, allows developers to test a +request without actually sending a message.</p> + <p>The default value is {@code false}.</p></td> + </tr> +<tr> + <td colspan="4"><strong>Payload</strong></td> + </tr> + <tr> + <td><code>data</code></td> + <td>HTTP, XMPP</td> + <td>Optional, JSON object</td> + <td><p>This parameter specifies the key-value pairs of the message's payload. There is +no limit on the number of key-value pairs, but there is a total message size limit of 4kb.</p> + <p>For instance, in Android, <code>data:{"score":"3x1"}</code> would result in an intent extra +named {@code score} with the string value {@code 3x1}.</p> + <p>The key should not be a reserved word ({@code from} or any word starting with +{@code google}). It is also not recommended to use words defined in this table +(such as {@code collapse_key}) because that could yield unpredictable outcomes. </p> + <p>Values in string types are recommended. You have to convert values in objects +or other non-string data types (e.g., integers or booleans) to string.</p></td> + </tr> +</table> + +<h3>Downstream HTTP messages (Plain Text)</h3> + +<p>The following table lists the syntax for targets, options, and payload in plain +text downstream HTTP messages.</p> + +<p class="table-caption" id="table2"> + <strong>Table 2.</strong> Targets, options, and payload for downstream plain text HTTP messages.</p> + +<table border="1"> + <tr> + <th>Parameter</th> + <th>Usage</th> + <th>Description</th> + </tr> +<tr> + <td colspan="3"><strong>Targets</strong></td> + </tr> + <tr> + <td><code>registration _id</code></td> + <td>Required, string</td> + <td><p>This parameter specifies the client apps (registration ID) receiving the message.</p> + <p>Multicast messaging (sending to more than one registration ID) is allowed using HTTP JSON format only.</p></td> + </tr> +<tr> + <td colspan="3"><strong>Options</strong></td> + </tr> + <tr> + <td><code>collapse_key</code></td> + <td>Optional, string</td> + <td>See <a href="#table1">table 1</a> for details.</td> + </tr> + <tr> + <td><code>delay_while_idle</code></td> + <td>Optional, boolean or number</td> + <td>See <a href="#table1">table 1</a> for details.</td> + </tr> + <tr> + <td><code>time_to_live</code></td> + <td>Optional, number</td> + <td>See <a href="#table1">table 1</a> for details.</td> + </tr> + <tr> + <td><code>restricted_package_name</code></td> + <td>Optional, string</td> + <td>See <a href="#table1">table 1</a> for details.</td> + </tr> + <tr> + <td><code>dry_run </code></td> + <td>Optional, boolean</td> + <td>See <a href="#table1">table 1</a> for details.</td> + </tr> +<tr> + <td colspan="3"><strong>Payload</strong></td> + </tr> + <tr> + <td><code>data.<key></code></td> + <td>Optional, string</td> + <td><p>This parameter specifies the key-value pairs of the message's payload. +There is no limit on the number of key-value parameters, +but there is a total message size limit of 4kb.</p> + <p>For instance, in Android, <code>data:{"score":"3x1"}</code> would result in an intent extra +named {@code score} with the string value {@code 3x1}.</p> + <p>The key should not be a reserved word ({@code from} or any word starting with +{@code google}). It is also not recommended to use words defined in this table +(such as {@code collapse_key}) because that could yield unpredictable outcomes.</p></td> + </tr> +</table> + +<h3 id="interpret-downstream">Interpreting a Downstream Message Response</h3> + +<p>This section describes the syntax of a response to a downstream message. A client +app or the GCM Connection Server sends the response to 3rd-party app server upon processing +the message request. </p> + +<h4>Interpreting a downstream HTTP message response </h4> +<p>The 3rd-party app server should look at both the message response header and the body +to interpret the message response sent from the GCM Connection Server. The following table +describes the possible responses.</p> +<p> + +<p class="table-caption" id="table3"> + <strong>Table 3.</strong> Downstream HTTP message response header.</p> +<table border=1> + <tr> + <th>Response</th> + <th>Description</th> + </tr> + <tr> + <td>200</td> + <td>Message was processed successfully. The response body will contain more +details about the message status, but its format will depend whether the request +was JSON or plain text. See <a href="#table4">table 4</a> +for more details.</td> + </tr> + <tr> + <td>400</td> + <td>Only applies for JSON requests. +Indicates that the request could not be parsed as JSON, or it contained invalid +fields (for instance, passing a string where a number was expected). The exact +failure reason is described in the response and the problem should be addressed +before the request can be retried.</td> + </tr> + <tr> + <td>401</td> + <td>There was an error authenticating the sender account. +<a href="server.html#auth_error">Troubleshoot</a></td> + </tr> + <tr> + <td>5xx</td> + <td>Errors in the 500-599 range (such as 500 or 503) indicate that there was +an internal error in the GCM server while trying to process the request, or that +the server is temporarily unavailable (for example, because of timeouts). Sender +must retry later, honoring any <code>Retry-After</code> header included in the +response. Application servers must implement exponential back-off. +<a href="server.html#internal_error">Troubleshoot</a></td> + </tr> +</table> + +<p>The following table lists the fields in a downstream message response body +(JSON).</p> + + +<p class="table-caption" id="table4"> + <strong>Table 4.</strong> Downstream HTTP message response body (JSON).</p> +<table> + <tr> + <th>Parameter</th> + <th>Usage</th> + <th>Description</th> + </tr> + <tr> + <td><code>multicast_id</code></td> + <td>Required, number</td> + <td>Unique ID (number) identifying the multicast message.</td> + </tr> + <tr> + <td><code>success</code></td> + <td>Required, number</td> + <td>Number of messages that were processed without an error.</td> + </tr> + <tr> + <td><code>failure</code></td> + <td>Required, number</td> + <td>Number of messages that could not be processed.</td> + </tr> + <tr> + <td><code>canonical_ids</code></td> + <td>Required, number</td> + <td>Number of results that contain a canonical registration ID. See the +<a href="{@docRoot}google/gcm/gcm.html#canonical">Overview</a> for more discussion of this topic.</td> + </tr> + <tr> + <td><code>results</code></td> + <td>Optional, array object</td> + <td>Array of objects representing the status of the messages processed. The +objects are listed in the same order as the request (i.e., for each registration +ID in the request, its result is listed in the same index in the response).<br> + <ul> + <li><code>message_id</code>: String specifying a unique ID for each successfully processed + message.</li> + <li><code>registration_id</code>: Optional string specifying the canonical registration ID + for the client app that the message was processed and sent to. Sender should use this + value as the Registration ID for future requests. Otherwise, the messages might + be rejected. + </li> + <li><code>error</code>: String specifying the error that occurred when processing the + message for the recipient. The possible values can be found in <a href="#table11">table 11 + </a>.</li> + </ul></td> + </tr> +</table> + + +<p class="table-caption" id="table5"> + <strong>Table 5.</strong> Success response for downstream HTTP message response body (Plain Text).</p> +<table border="1"> + <tr> + <th>Parameter</th> + <th>Usage</th> + <th>Description</th> + </tr> + <tr> + <td><code>id</code></td> + <td>Required, string</td> + <td>This parameter specifies the unique message ID that GCM server processed successfully.</td> + </tr> + <tr> + <td><code>registration_id</code></td> + <td>Optional, string</td> + <td>This parameter specifies the canonical registration ID for the client app that the message was +processed and sent to. Sender should replace the registration ID with this value on future requests, +otherwise, the messages might be rejected.</td> + </tr> +</table> + +<p class="table-caption" id="table6"> + <strong>Table 6.</strong> Error response for downstream HTTP message response body (Plain Text).</p> +<table border="1"> + <tr> + <th>Parameter</th> + <th>Usage</th> + <th>Description</th> + </tr> + <tr> + <td>{@code Error}</td> + <td>Required, string</td> + <td>This parameter specifies the error value while processing the message for the recipient. +See <a href="#table11">table 11</a> for details. </td> + </tr> +</table> + +<h4>Interpreting a downstream XMPP message response</h4> + +<p>The following table lists the fields that appear in a downstream XMPP message response.</p> + +<p class="table-caption" id="table7"> + <strong>Table 7.</strong> Downstream message XMPP message response body.</p> +<table border="1"> + <tr> + <th>Parameter</th> + <th>Usage</th> + <th>Description</th> + </tr> + <tr> + <td><code>from</code></td> + <td>Required, string</td> + <td><p>This parameter specifies who sent this response.</p> + <p>The value is the registration ID of the client app.</p></td> + </tr> + <tr> + <td><code>message_id</code></td> + <td>Required, string</td> + <td>This parameter uniquely identifies a message in an XMPP connection. +The value is a string that uniquely identifies the associated message.</td> + </tr> + <tr> + <td><code>message_type</code></td> + <td>Required, string</td> + <td><p>This parameter specifies an 'ack' or 'nack' message from XMPP (CCS) +to the 3rd-party app server.</p> + <p>If the value is set to {@code nack}, the 3rd-party app server should look at +{@code error} and {@code error_description} to get failure information. </p></td> + </tr> + <tr> + <td><code>error</code></td> + <td>Optional, string</td> + <td>This parameter specifies an error related to the downstream message. It is set when the +{@code message_type} is {@code nack}. See <a href="#table11">table 6</a> for details.</td> + </tr> + <tr> + <td><code>error_description</code></td> + <td>Optional, string</td> + <td>This parameter provides descriptive information for the error. It is set +when the {@code message_type} is {@code nack}.</td> + </tr> +</table> +<h2 id="upstream">Upstream Messages (XMPP)</h2> + +<p>An upstream message is a message the client app sends to the 3rd-party app server. +Currently only CCS (XMPP) supports upstream messaging.</p> + +<h3 id="interpret-upstream">Interpreting an upstream XMPP message </h3> +<p>The following table describes the fields that appear in an upstream XMPP message. + +<p class="table-caption" id="table8"> + <strong>Table 8.</strong> Upstream XMPP messages.</p> +<table border="1"> + <tr> + <th>Parameter</th> + <th>Usage</th> + <th>Description</th> + </tr> + <tr> + <td><code>from</code></td> + <td>Required, string</td> + <td><p>This parameter specifies who sent the message.</p> + <p>The value is the registration ID of the client app.</p></td> + </tr> + <tr> + <td><code>category</code></td> + <td>Required, string</td> + <td>This parameter specifies the application package name of the client app that sent the message. </td> + </tr> + <tr> + <td><code>message_id</code></td> + <td>Required, string</td> + <td>This parameter specifies the unique ID of the message. </td> + </tr> + <tr> + <td><code>data</code></td> + <td>Optional, string</td> + <td>This parameter specifies the key-value pairs of the message's payload.</td> + </tr> +</table> + +<h3 id="upstream-response">Sending an upstream XMPP message response</h3> + +<p>The following table describes the response that 3rd-party app server is expected to send to +<a href="{@docRoot}google/gcm/ccs.html">XMPP (CCS)</a> in response to an +upstream message it (the app server) received.</p> + +<p class="table-caption" id="table9"> + <strong>Table 9.</strong> Upstream XMPP message response.</p> +<table border="1"> + <tr> + <th>Parameter</th> + <th>Usage</th> + <th>Description</th> + </tr> + <tr> + <td><code>to</code></td> + <td>Required, string</td> + <td><p>This parameter specifies the recipient of a response message. </p> + <p>The value must be a registration ID of the client app that sent the upstream message.</p></td> + </tr> + <tr> + <td><code>message_id</code></td> + <td>Required, string</td> + <td>This parameter specifies which message the response is intended for. The value must be +the {@code message_id} value from the corresponding upstream message.</td> + </tr> + <tr> + <td><code>message_type</code></td> + <td>Required, string</td> + <td>This parameter specifies an {@code ack} message from a 3rd-party app server to CCS.</td> + </tr> +</table> +<h2 id="ccs">Cloud Connection Server Messages (XMPP) </h2> +<p>This is a message sent from XMPP (CCS) to a 3rd-party app server. Here are the primary types +of messages that XMPP (CCS) sends to the 3rd-party app server:</p> +<ul> + <li><strong>Delivery Receipt:</strong> If the 3rd-party app server included {@code delivery_receipt_requested} +in the downstream message, XMPP (CCS) sends a delivery receipt when it receives confirmation +that the device received the message.</li> + <li><strong>Control:</strong> These CCS-generated messages indicate that +action is required from the 3rd-party app server.</li> +</ul> + +<p>The following table describes the fields included in the messages CCS +sends to a 3rd-party app server.</p> + +<p class="table-caption" id="table10"> + <strong>Table 10.</strong> GCM Cloud Connection Server messages (XMPP).</p> +<table border="1"> + <tr> + <th>Parameter</th> + <th>Usage</th> + <th>Description</th> + </tr> + <tr> + <td colspan="3"><strong>Common Field</strong></td> + </tr> + <tr> + <td><code>message_type</code></td> + <td>Required, string</td> + <td><p>This parameter specifies the type of the CCS message: either delivery receipt or control.</p> + <p>When it is set to {@code receipt}, the message includes {@code from}, {@code message_id}, + {@code category} and {@code data} fields to provide additional information.</p> + <p>When it is set to {@code control}, the message includes {@code control_type} to indicate the +type of control message.</p></td> + </tr> + <tr> + <td colspan="3"><strong>Delivery receipt-specific</strong></td> + </tr> + <tr> + <td><code>from</code></td> + <td>Required, string</td> + <td>This parameter is set to {@code gcm.googleapis.com}, indicating that the +message is sent from CCS.</td> + </tr> + <tr> + <td><code>message_id</code></td> + <td>Required, string</td> + <td>This parameter specifies the original message ID that the receipt is intended for, +prefixed with {@code dr2:} to indicate that the message is a delivery receipt. A 3rd-party app +server must send an {@code ack} message with this message ID to acknowledge that it +received this delivery receipt. See <a href="#table9">table 9</a> for the 'ack' message format.</td> + </tr> + <tr> + <td><code>category</code></td> + <td>Optional, string</td> + <td>This parameter specifies the application package name of the client app that +receives the message that this delivery receipt is reporting. This is available when +{@code message_type} is {@code receipt}.</td> + </tr> + <tr> + <td><code>data</code></td> + <td>Optional, string</td> + <td>This parameter specifies the key-value pairs for the delivery receipt message. This is available +when the {@code message_type} is {@code receipt}. + <ul> + <li>{@code message_status}: This parameter specifies the status of the receipt message. +It is set to {@code MESSAGE_SENT_TO_DEVICE} to indicate the device has confirmed its receipt of +the original message.</li> + <li>{@code original_message_id}: This parameter specifies the ID of the original message +that the 3rd-party app server sent to the client app.</li> + <li>{@code device_registration_id}: This parameter specifies the registration ID of the +client app to which the original message was sent.</li> + </ul> +</td> + </tr> + <tr> + <td colspan="3"><strong>Control-specific</strong></td> + </tr> + <tr> + <td><code>control_type</code></td> + <td>Optional, string</td> + <td><p>This parameter specifies the type of control message sent from CCS.</p> + <p>Currently, only {@code CONNECTION_DRAINING} is supported. XMPP (CCS) sends this control message +before it closes a connection to perform load balancing. As the connection drains, no more messages +are allowed to be sent to the connection, but existing messages in the pipeline will +continue to be processed.</p></td> + </tr> +</table> + +<h2 id="error-codes">Downstream message error response codes (HTTP and XMPP)</h2> + +<p>The following table lists the error response codes for downstream messages (HTTP and XMPP).</p> + +<p class="table-caption" id="table11"> + <strong>Table 11.</strong> Downstream message error response codes.</p> +<table border="1"> + <tr> + <th>Error</th> + <th>HTTP Code</th> + <th>XMPP Code</th> + <th>Recommended Action</th> + </tr> + <tr> + <td>Missing Registration ID</td> + <td>200 + error:MissingRegistration</td> + <td><code>INVALID_JSON</code></td> + <td>Check that the request contains a registration ID (either in the +{@code registration_id} in a plain text message, or in the {@code registration_ids} in JSON).</td> + </tr> + <tr> + <td>Invalid Registration ID</td> + <td>200 + error:InvalidRegistration</td> + <td><code>BAD_REGISTRATION</code></td> + <td>Check the format of the registration ID you pass to the server. Make sure it +matches the registration ID the client app receives from registering with GCM. Do not +truncate or add additional characters.</td> + </tr> + <tr> + <td>Unregistered Device</td> + <td>200 + error:NotRegistered</td> + <td><code>DEVICE_UNREGISTERED</code></td> + <td>An existing registration ID may cease to be valid in a number of scenarios, including:<br> + <ul> + <li>If the client app unregisters with GCM.</li> + <li>If the client app is automatically unregistered, which can happen if the user uninstalls the application.</li> + <li>If the registration ID expires (for example, Google might decide to refresh registration IDs).</li> + <li>If the client app is updated but the new version is not configured to receive messages.</li> +</ul> + For all these cases, remove this registration ID from the 3rd-party app +server and stop using it to send messages.</td> + </tr> + <tr> + <td>Invalid Package Name</td> + <td>200 + error:InvalidPackageName</td> + <td></td> + <td>Make sure the message was addressed to a registration ID whose package name +matches the value passed in the request.</td> + </tr> + <tr> + <td>Authentication Error</td> + <td>401</td> + <td> </td> + <td>The sender account used to send a message couldn't be authenticated. Possible causes are:<br> +<ul> + <li>Authorization header missing or with invalid syntax in HTTP request.</li> + <li>Invalid project number sent as key.</li> + <li>Key valid but with GCM service disabled.</li> + <li>Request originated from a server not whitelisted in the Server Key IPs.</li> +</ul> + Check that the token you're sending inside the Authentication header is +the correct API key associated with your project. See +<a href="{@docRoot}google/gcm/http.html">GCM HTTP Connection Server</a> for details.</td> + </tr> + <tr> + <td>Mismatched Sender</td> + <td>200 + error:MismatchSenderId</td> + <td><code>BAD_REGISTRATION</code></td> + <td>A registration ID is tied to a certain group of senders. When a client app registers +for GCM, it must specify which senders are allowed to send messages. You should use one +of those sender IDs when sending messages to the client app. If you switch to a different +sender, the existing registration IDs won't work.</td> + </tr> + <tr> + <td>Invalid JSON</td> + <td>400</td> + <td><code>INVALID_JSON</code></td> + <td>Check that the JSON message is properly formatted and contains valid fields +(for instance, making sure the right data type is passed in).</td> + </tr> + <tr> + <td>Message Too Big</td> + <td>200 + error:MessageTooBig</td> + <td><code>INVALID_JSON</code></td> + <td>Check that the total size of the payload data included in a message does +not exceed 4096 bytes. This includes both the the keys and the values.</td> + </tr> + <tr> + <td>Invalid Data Key</td> + <td>200 + error: +<br /> +InvalidDataKey</td> + <td><code>INVALID_JSON</code></td> + <td>Check that the payload data does not contain a key (such as {@code from} or any value +prefixed by {@code google}) that is used internally by GCM. Note that some words (such as {@code collapse_key}) +are also used by GCM but are allowed in the payload, in which case the payload value +will be overridden by the GCM value.</td> + </tr> + <tr> + <td>Invalid Time to Live</td> + <td>200 + error:InvalidTtl</td> + <td><code>INVALID_JSON</code></td> + <td>Check that the value used in {@code time_to_live} is an integer representing a +duration in seconds between 0 and 2,419,200 (4 weeks).</td> + </tr> + <tr> + <td>Bad ACK message</td> + <td>N/A</td> + <td><code>BAD_ACK</code></td> + <td>Check that the 'ack' message is properly formatted before retrying. See +<a href="#table9">table 9</a> for details.</td> + </tr> + <tr> + <td>Timeout</td> + <td>5xx or 200 + error:Unavailable</td> + <td><code>SERVICE_UNAVAILABLE</code></td> + <td><p>The server couldn't process the request in time. Retry the same request, but you must:<br> +<ul> + <li>For HTTP: Honor the {@code Retry-After} header if it is included in the response from the +GCM Connection Server.</li> + <li>Implement exponential back-off in your retry mechanism. (e.g. if you waited one second +before the first retry, wait at least two second before the next one, then 4 seconds and so on). +If you're sending multiple messages, delay each one independently by an additional random amount +to avoid issuing a new request for all messages at the same time.</li> + <li>For CCS: The initial retry delay should be set to 1 second.</li> +</ul> + <p>Senders that cause problems risk being blacklisted.</p></td> + </tr> + <tr> + <td>Internal Server Error</td> + <td>500 or 200 + error:InternalServerError</td> + <td><code>INTERNAL_SERVER_ +<br /> +ERROR</code></td> + <td>The server encountered an error while trying to process the request. You could retry +the same request following the requirements listed in "Timeout" (see row above). If the error persists, please +report the problem in the {@code android-gcm group}.</td> + </tr> + <tr> + <td>Device Message Rate Exceeded</td> + <td>200 + error: +<br />DeviceMessageRate +<br /> +Exceeded</td> + <td><code>DEVICE_MESSAGE_RATE<br /> +_EXCEEDED</code></td> + <td>The rate of messages to a particular device is too high. Reduce the +number of messages sent to this device and do not immediately retry sending to this device.</td> + </tr> + <tr> + <td>Connection Draining</td> + <td>N/A</td> + <td><code>CONNECTION_DRAINING</code></td> + <td>The message couldn't be processed because the connection is draining. This happens because +periodically, XMPP (CCS) needs to close down a connection to perform load balancing. Retry the message over +another XMPP connection.</td> + </tr> +</table> diff --git a/docs/html/google/gcm/server.jd b/docs/html/google/gcm/server.jd index 20e2b2e..004fd0e 100644 --- a/docs/html/google/gcm/server.jd +++ b/docs/html/google/gcm/server.jd @@ -7,9 +7,9 @@ page.title=Implementing GCM Server <h2>In this document</h2> <ol class="toc"> - <li><a href="#choose">Choosing a GCM Connection Server</a></li> <li><a href="#role">Role of the 3rd-party Application Server</a></li> - <li><a href="#send-msg">Sending Messages</a> + <li><a href="#choose">Choosing a GCM Connection Server</a></li> + <li><a href="#send-msg">Sending Messages</a> <ol class="toc"> <li><a href="#target">Target</a></li> @@ -17,7 +17,18 @@ page.title=Implementing GCM Server <li><a href="#params">Message parameters</a> </ol> </li> - <li><a href="#receive">Receiving Messages</a> </li> + <li><a href="#adv">Messaging Concepts and Best Practices</a> + + <ol class="toc"> + + <li><a href="#collapsible">Send-to-Sync vs. Messages with Payload</a></li> + <li><a href="#ttl">Setting an Expiration Date for a Message</a></li> + <li><a href="#multi-senders">Receiving Messages from Multiple Senders</a> + <li><a href="#lifetime">Lifetime of a Message</a> + <li><a href="#throttling">Throttling</a> + </ol> + +</li> </li> </ol> @@ -25,6 +36,7 @@ page.title=Implementing GCM Server <h2>See Also</h2> <ol class="toc"> +<li><a href="server-ref.html">Server Reference</a></li> <li><a href="gs.html">Getting Started</a></li> <li><a href="client.html">Implementing GCM Client</a></li> <li><a href="ccs.html">Cloud Connection Server (XMPP)</a></li> @@ -37,388 +49,392 @@ page.title=Implementing GCM Server </div> -<p>The server side of Google Cloud Messaging (GCM) consists of 2 components:</p> +<p>The server side of Google Cloud Messaging (GCM) consists of two components:</p> <ul> <li>Google-provided <strong>GCM Connection Servers</strong> -take messages from a 3rd-party application server and send them to a GCM-enabled -Android application (the "client app") running on a device. For example, +take messages from a <a href="{@docRoot}google/gcm/server.html#role">3rd-party app server</a> +and send them to a GCM-enabled +application (the "client app") running on a device. For example, Google provides connection servers for <a href="{@docRoot}google/gcm/http.html"> -HTTP</a> and <a href="{@docRoot}google/gcm/ccs.html">CCS</a> (XMPP).</li> +HTTP</a> and <a href="{@docRoot}google/gcm/ccs.html">XMPP (CCS)</a> (XMPP).</li> <li>A <strong>3rd-party application server</strong> that you must implement. This application -server sends data to a GCM-enabled Android application via the chosen GCM connection server.</li> +server sends data to a GCM-enabled client app via the chosen GCM connection server.</li> </ul> </p> +<p>A full GCM implementation requires both a client implementation and a server +implementation. For more +information about implementing the client side, see <a href="client.html"> +Implementing GCM Client</a>.</p> + + +<h2 id="role">Role of the 3rd-party Application Server</h2> + +<p>Before you can write client apps that use the GCM feature, you must +have an application server that meets the following criteria:</p> + +<ul> + <li>Able to communicate with your client.</li> + <li>Able to fire off properly formatted requests to the GCM server.</li> + <li>Able to handle requests and resend them as needed, using +<a href="http://en.wikipedia.org/wiki/Exponential_backoff">exponential back-off.</a></li> + <li>Able to store the API key and client registration IDs. In HTTP, the API key is +included in the header of POST requests that send messages. In XMPP, the API key is +used in the SASL PLAIN authentication request as a password to authenticate the connection.</li> + <li>Able to generate message IDs to uniquely identify each message it sends. Message IDs +should be unique per sender ID.</li> +</ul> + <p>Here are the basic steps you follow to implement your 3rd-party app server:</p> <ul> <li>Decide which GCM connection server(s) you want to use. Note that if you want to use - upstream messaging from your client applications, you must use CCS. For a more detailed + upstream messaging from your client applications, you must use XMPP (CCS). For a more detailed discussion of this, see <a href="#choose"> Choosing a GCM Connection Server</a>.</li> - <li>Decide how you want to implement your app server. For example: + <li>Decide how you want to implement your app server. We provide helper libraries and code +samples to assist you with your 3rd-party app server implementation. For example: <ul> <li>If you decide to use the HTTP connection server, you can use the GCM server helper library and demo app to help in implementing your app server.</li> <li>If you decide to use the XMPP connection server, you can use the provided Python or Java <a href="http://www.igniterealtime.org/projects/smack/"> Smack</a> demo apps as a starting point.</li> - <li>Note that Google AppEngine does not support connections to CCS.</li> + <li>Note that Google AppEngine does not support connections to XMPP (CCS).</li> </ul> </li> </ul> </li> </ul> -<p>A full GCM implementation requires both a client implementation and a server -implementation. For more -information about implementing the client side, see <a href="client.html"> -Implementing GCM Client</a>.</p> <h2 id="choose">Choosing a GCM Connection Server</h2> <p>Currently GCM provides two connection servers: <a href="{@docRoot}google/gcm/http.html"> -HTTP</a> and <a href="{@docRoot}google/gcm/ccs.html">CCS</a> (XMPP). You can use them -separately or in tandem. CCS messaging differs from GCM HTTP messaging in the following ways:</p> +HTTP</a> and <a href="{@docRoot}google/gcm/ccs.html">XMPP (CCS)</a>. You can use them +separately or in tandem. XMPP (CCS) messaging differs from HTTP messaging in the following ways:</p> <ul> <li>Upstream/Downstream messages <ul> - <li>GCM HTTP: Downstream only: cloud-to-device. </li> - <li>CCS: Upstream and downstream (device-to-cloud, cloud-to-device). </li> + <li>HTTP: Downstream only, cloud-to-device up to 4KB of data. </li> + <li>XMPP (CCS): Upstream and downstream (device-to-cloud, cloud-to-device), + up to 4 KB of data. </li> </ul> </li> - <li>Asynchronous messaging + <li>Messaging (synchronous or asynchronous) <ul> - <li>GCM HTTP: 3rd-party app servers send messages as HTTP POST requests and -wait for a response. This mechanism is synchronous and causes the sender to block -before sending another message.</li> - <li>CCS: 3rd-party app servers connect to Google infrastructure using a -persistent XMPP connection and send/receive messages to/from all their devices -at full line speed. CCS sends acknowledgment or failure notifications (in the + <li>HTTP: Synchronous. 3rd-party app servers send messages as HTTP POST requests and +wait for a response. This mechanism is synchronous and blocks the sender from sending +another message until the response is received.</li> + <li>XMPP (CCS): Asynchronous. 3rd-party app servers send/receive messages to/from all their +devices at full line speed over persistent XMPP connections. +XMPP (CCS) sends acknowledgment or failure notifications (in the form of special ACK and NACK JSON-encoded XMPP messages) asynchronously.</li> </ul> </li> <li>JSON <ul> - <li>GCM HTTP: JSON messages sent as HTTP POST.</li> - <li>CCS: JSON messages encapsulated in XMPP messages.</li> + <li>HTTP: JSON messages sent as HTTP POST.</li> + <li>XMPP (CCS): JSON messages encapsulated in XMPP messages.</li> + </ul> + </li> + <li>Plain Text + <ul> + <li>HTTP: Plain Text messages sent as HTTP POST.</li> + <li>XMPP (CCS): Not supported.</li> + </ul> + </li> + <li>Multicast downstream send to multiple registration IDs. + <ul> + <li>HTTP: Supported in JSON message format.</li> + <li>XMPP (CCS): Not supported.</li> </ul> </li> </ul> -<h2 id="role">Role of the 3rd-party Application Server</h2> -<p>Before you can write client Android applications that use the GCM feature, you must -have an application server that meets the following criteria:</p> +<h2 id="send-msg">Sending Messages</h2> -<ul> - <li>Able to communicate with your client.</li> - <li>Able to fire off properly formatted requests to the GCM server.</li> - <li>Able to handle requests and resend them as needed, using -<a href="http://en.wikipedia.org/wiki/Exponential_backoff">exponential back-off.</a></li> - <li>Able to store the API key and client registration IDs. The -API key is included in the header of POST requests that send -messages.</li> - <li>Able to generate message IDs to uniquely identify each message it sends. Message IDs -should be unique per sender ID.</li> -</ul> +<p>This section gives an overview of sending messages. For details of message syntax, +see <a href="{@docRoot}google/gcm/server-ref.html">Server Reference</a>.</p> -<h2 id="send-msg">Sending Messages</h2> +<h3>Overview</h3> <p>Here is the general sequence of events that occurs when a 3rd-party application -server sends a message:</p> +server sends a message (the details vary depending on the platform):</p> <ol> - <li>The application server sends a message to GCM servers.</li> - <li>Google enqueues and stores the message in case the device is offline.</li> - <li>When the device is online, Google sends the message to the device.</li> - <li>On the device, the system broadcasts the message to the specified Android -application via Intent broadcast with proper permissions, so that only the targeted -Android application gets the message. This wakes the Android application up. -The Android application does not need to be running beforehand to receive the message.</li> - <li>The Android application processes the message. </li> + <li>The 3rd-party app server sends a message to GCM servers.</li> + <li>The GCM connection server enqueues and stores the message if the device is offline.</li> + <li>When the device is online, GCM connection server sends the message to the device.</li> + <li>The client app processes the message. </li> </ol> -<p>The following sections describe the basic requirements for -sending messages.</p> +<h3>Implement send request</h3> + +<p>The following sections describe the basic components involved in +sending a request. See the <a href="{@docRoot}google/gcm/server-ref.html">Server Reference</a> +for details.</p> -<h3 id="target">Target</h3> +<h4 id="target">Target</h4> <p>Required. When your app server sends a message in GCM, it must specify a target.</p> -<p>For HTTP you must specify the target as one of:</p> +<p>For HTTP you must specify the target as one of the following:</p> <ul> <li><code>registration_ids</code>: For sending to 1 or more devices (up to 1000). When you send a message to multiple registration IDs, that is called a multicast message.</li> <li><code>notification_key</code>: For sending to multiple devices owned by a single user.</li> </ul> -<p>For CCS (XMPP):</p> +<p>For CCS (XMPP) you must specify the target as:</p> <ul> -<li>You must specify the target as the "to" field, where the "to" +<li>{@code to}: This field may contain a single registration ID or a notification key. -CCS does not support multicast messaging.</li> +XMPP (CCS) does not support multicast messaging.</li> </ul> -<h3 id="payload">Payload</h3> -<p>Optional. If you are including a payload in the message, you use the <code>data</code> -parameter to include the payload. This applies for both HTTP and CCS.</p> - -<h3 id="params">Message parameters</h3> - -<p>The following table lists the parameters that a 3rd-party app server might -include in the JSON messages it sends to a connection server. See the "Where Supported" -column for information about which connection servers support that particular -parameter.</p> - -<p class="table-caption" id="table1"> - <strong>Table 1.</strong> Message Parameters JSON (CCS and HTTP).</p> - -<table> - <tr> - <th>Parameter</th> - <th>Description</th> -<th>Where Supported</th> -</tr> - <td><code>to</code></td> -<td>In CCS, this parameter is used in place of <code>registration_ids</code> to -specify the recipient of a message. Its value must be a registration ID. -The value is a string. Required.</td> -<td>CCS</td> -</tr> -<tr> -<td><code>message_id</code></td> -<td>In CCS, this parameter uniquely identifies a message in an XMPP connection. -The value is a string that uniquely identifies the associated message. Required.</td> -<td>CCS</td> -</tr> -<tr> -<td><code>message_type</code></td> -<td>In CCS, this parameter indicates a special status message, typically sent by the system. -However, your app server also uses this parameter to send an 'ack' or 'nack' -message back to the CCS connection server. For more discussion of this topic, see -<a href="ccs.html">Cloud Connection Server</a>. The value is a string. Optional.</td> -<td>CCS</td> -<tr> - <td><code>registration_ids</code></td> - <td>This parameter specifies a string array containing the list of devices -(registration IDs) receiving the -message. It must contain at least 1 and at most 1000 registration IDs. To send a -multicast message, you must use JSON. For sending a single message to a single -device, you could use a JSON object with just 1 registration id, or plain text -(see below). A request must include a recipient—this can be either a -registration ID, an array of registration IDs, or a {@code notification_key}. -Required.</td> -<td>HTTP</td> -</tr> - <tr> - <td><code>notification_key</code></td> - <td>This parameter specifies a string that maps a single user to multiple -registration IDs associated -with that user. This allows a 3rd-party server to send a single message to -multiple app instances (typically on multiple devices) owned by a single user. -A 3rd-party server can use {@code notification_key} as the target for a message -instead of an individual registration ID (or array of registration IDs). The maximum -number of members allowed for a {@code notification_key} is 20. For more discussion -of this topic, see <a href="notifications.html">User Notifications</a>. Optional. -</td> -<td style="width:100px">HTTP. This feature is supported in CCS, but you use it by -specifying a notification key in the "to" field.</td> -</tr> - <tr> - <td><code>collapse_key</code></td> - <td>This parameter specifies an arbitrary string (such as -"Updates Available") that is used -to collapse a group of like messages -when the device is offline, so that only the last message gets sent to the -client. This is intended to avoid sending too many messages to the phone when it -comes back online. Note that since there is no guarantee of the order in which -messages get sent, the "last" message may not actually be the last -message sent by the application server. Messages with collapse keys are also called -<a href="#s2s">send-to-sync messages</a>. -<br> -<strong>Note:</strong> GCM allows a maximum of 4 different collapse keys to be -used by the GCM server -at any given time. In other words, the GCM server can simultaneously store 4 -different send-to-sync messages per device, each with a different collapse key. -If you exceed -this number GCM will only keep 4 collapse keys, with no guarantees about which -ones they will be. See <a href="adv.html#collapsible">Advanced Topics</a> for more -discussion of this topic. Optional.</td> -<td>CCS, HTTP</td> -</tr> - <tr> - <td><code>data</code></td> - <td>This parameter specifies a JSON object whose fields represents the -key-value pairs of the message's -payload data. If present, the payload data will be -included in the Intent as application data, with the key being the extra's name. -For instance, <code>"data":{"score":"3x1"}</code> would result in an intent extra -named <code>score</code> whose value is the string <code>3x1</code>. -There is no limit on the number of key/value pairs, though there is a limit on -the total size of the message (4kb). The values could be any JSON object, but we -recommend using strings, since the values will be converted to strings in the GCM -server anyway. If you want to include objects or other non-string data types -(such as integers or booleans), you have to do the conversion to string yourself. -Also note that the key cannot be a reserved word (<code>from</code> or any word -starting with <code>google.</code>). Using words defined in this table as field -names (such as <code>collapse_key</code>) could yield unpredictable outcomes and -is not recommended. Optional.</td> -<td>CCS, HTTP</td> -</tr> - <tr> - <td><code>delay_while_idle</code></td> - <td>This parameter indicates that the message should not be sent immediately -if the device is idle. The server will wait for the device to become active, and -then only the last message for each <code>collapse_key</code> value will be -sent. The default value is <code>false</code>, and must be a JSON boolean. Optional.</td> -<td>CCS, HTTP</td> -</tr> - <tr> - <td><code>time_to_live</code></td> - <td>This parameter specifies how long (in seconds) the message should be kept on GCM -storage if the device is offline. Optional (default time-to-live is 4 weeks, and must be set as -a JSON number).</td> -<td>CCS, HTTP</td> -</tr> -<tr> - <td><code>restricted_package_name</code></td> - <td>This parameter specifies a string containing the package -name of your application. When set, messages -are only sent to registration IDs that match the package name. Optional. - </td> -<td>HTTP</td> -</tr> -<tr> - <td><code>dry_run</code></td> - <td>This parameter allows developers to test a request without actually -sending a message. Optional. The default value is <code>false</code>, and must -be a JSON boolean. - </td> -<td>HTTP</td> -</tr> -<tr> - <td><code>delivery_receipt_requested</code></td> - <td>This parameter lets you request confirmation of message delivery. When -this parameter is set to <code>true</code>, CCS sends a -delivery receipt when a device confirms that it received a message sent by CCS. -The default value is <code>false</code>, and must be a JSON boolean. Optional.<br /> -This parameter relates to <a href="{@docRoot}google/gcm/ccs.html#receipts"}> -delivery receipts</a>. -</td> - <td>CCS</td> -</tr> -<tr> - <td><code>message_status</code></td> - <td>This parameter specifies the status of the receipt message. -The parameter appears inside the -<code>"data"</code> field of a -delivery receipt message. Currently the only possible value -is <code>MESSAGE_SENT_TO_DEVICE</code>, which indicates that a device acknowledges -receiving a message sent by CCS.<br /> -This parameter relates to <a href="{@docRoot}google/gcm/ccs.html#receipts"}> -delivery receipts</a>.</td> - <td>CCS</td> -</tr> -<tr> - <td><code>original_message_id</code></td> - <td>The value of this parameter is the ID of the original message that the server sent to -the device. This parameter appears inside the <code>"data"</code> field of a -delivery receipt message. <br /> -This parameter relates to <a href="{@docRoot}google/gcm/ccs.html#receipts"}> -delivery receipts</a>.</td> - <td>CCS</td> -</tr> -<tr> - <td><code>device_registration_id</code></td> - <td>For the purpose of tracking the delivery receipt, this parameter lists -the registration ID of the device to which a given message was sent. This parameter -appears inside the <code>"data"</code> field of a -delivery receipt message. <br /> -This parameter relates to <a href="{@docRoot}google/gcm/ccs.html#receipts"}> -delivery receipts</a>.</td> - <td>CCS</td> -</tr> - -</table> +<h4 id="options">Options</h4> + +<p>There are various options the 3rd-party app server can set when sending a downstream +message to a client app. See the <a href="{@docRoot}google/gcm/server-ref.html#table1"> +Server Reference</a> for details. Here are a few examples of possible options:</p> + +<ul> + <li>{@code collapse_key}: whether a message should be "send-to-sync" or a "message with +payload".</li> + <li>{@code time_to_live}: setting an expiration date for a message.</li> + <li>{@code dry_run}: Test your server. <p>If you want to test your request (either JSON or plain text) without delivering -the message to the devices, you can set an optional HTTP or JSON parameter called +the message to the devices, you can set an optional HTTP parameter called <code>dry_run</code> with the value <code>true</code>. The result will be almost identical to running the request without this parameter, except that the message will not be delivered to the devices. Consequently, the response will contain fake IDs for the message and multicast parameters.</p> +</li> +</ul> -<p>If you are using plain text instead of JSON, the message parameters must be set as -HTTP parameters sent in the body, and their syntax is slightly different, as -described in the following table: - -<p class="table-caption" id="table2"> - <strong>Table 2.</strong> Message Parameters Plain Text (HTTP only).</p> -<table> - <tr> - <th>Parameter</th> - <th>Description</th> - </tr> - <tr> - <td><code>registration_id</code></td> - <td>This parameter specifies the registration ID of the single device -receiving the message. -Required.</td> - </tr> - <tr> - <td><code>collapse_key</code></td> - <td>Same as JSON (see previous table). Optional.</td> - </tr> - <tr> - <td><code>data.<key></code></td> - - <td>This parameter specifies payload data, expressed as parameters -prefixed with <code>data.</code> and -suffixed as the key. For instance, a parameter of <code>data.score=3x1</code> would -result in an intent extra named <code>score</code> whose value is the string -<code>3x1</code>. There is no limit on the number of key/value parameters, though -there is a limit on the total size of the message. Also note that the key cannot -be a reserved word (<code>from</code> or any word starting with -<code>google.</code>). Using words defined in this table as field -names (such as <code>collapse_key</code>) could yield unpredictable outcomes and -is not recommended. Optional.</td> - - </tr> - <tr> - <td><code>delay_while_idle</code></td> - <td>This parameter specifies whether messages should be delivered when the device -is asleep. A value of <code>1</code> or <code>true</code> indicates -<code>true</code>, and anything else indicates <code>false</code>. Optional. The default -value is <code>false</code>.</td> - </tr> - <tr> - <td><code>time_to_live</code></td> - <td>Same as JSON (see previous table). Optional.</td> - </tr> -<tr> - <td><code>restricted_package_name</code></td> - <td>Same as JSON (see previous table). Optional. - </td> -</tr> -<tr> - <td><code>dry_run</code></td> - <td>Same as JSON (see previous table). Optional. - </td> -</tr> -</table> - -<h2 id="receive">Receiving Messages</h2> - -<p>This is the sequence of events that occurs when an Android application -installed on a mobile device receives a message:</p> +<h4 id="payload">Payload</h4> +<p>Optional. If you are including a payload in the message, you use the <code>data</code> +parameter to include the payload. This applies for both HTTP and XMPP.</p> + +<p>See the <a href="{@docRoot}google/gcm/server-ref.html">Server Reference</a> for details on sending +and receiving messages.</p> + +<h2 id="adv">Messaging Concepts and Best Practices</h2> + +<p>This section has a discussion of general messaging topics.</p> + +<h3 id="collapsible">Send-to-Sync vs. Messages with Payload</h3> + +<p>Every message sent in GCM has the following characteristics:</p> +<ul> + <li>It has a payload limit of 4096 bytes.</li> + <li>By default, it is stored by GCM for 4 weeks.</li> +</ul> + +<p>But despite these similarities, messages can behave very differently depending +on their particular settings. One major distinction between messages is whether +they are collapsed (where each new message replaces the preceding message) or not +collapsed (where each individual message is delivered). Every message sent in GCM +is either a "send-to-sync" (collapsible) message or a "message with +payload" (non-collapsible message).</p> + +<h4 id="s2s">Send-to-sync messages</h4> + +<p>A send-to-sync (collapsible) message is often a "tickle" that tells +a mobile application to sync data from the server. For example, suppose you have +an email application. When a user receives new email on the server, the server +pings the mobile application with a "New mail" message. This tells the +application to sync to the server to pick up the new email. The server might send +this message multiple times as new mail continues to accumulate, before the application +has had a chance to sync. But if the user has received 25 new emails, there's no +need to preserve every "New mail" message. One is sufficient. Another +example would be a sports application that updates users with the latest score. +Only the most recent message is relevant. </p> + +<p>GCM allows a maximum of 4 different collapse keys to be used by the GCM server +at any given time. In other words, the GCM server can simultaneously store 4 +different send-to-sync messages per device, each with a different collapse key. +For example, Device A can have A1, A2, A3, and A4. Device B can have B1, B2, B3, +and B4, and so on. If you exceed this number GCM will only keep 4 collapse keys, with no +guarantees about which ones they will be.</p> + +<h3 id="payload">Messages with payload</h3> + +<p>Unlike a send-to-sync message, every "message with payload" +(non-collapsible message) is delivered. The payload the message contains can be +up to 4kb. For example, here is a JSON-formatted message in an IM application in +which spectators are discussing a sporting event:</p> + +<pre class="prettyprint pretty-json">{ + "registration_id" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...", + "data" : { + "Nick" : "Mario", + "Text" : "great match!", + "Room" : "PortugalVSDenmark", + }, +}</pre> + +<p>A "message with payload" is not simply a "ping" to the +mobile application to contact the server to fetch data. In the aforementioned IM +application, for example, you would want to deliver every message, because every +message has different content. To specify a non-collapsible message, you simply +omit the <code>collapse_key</code> parameter. Thus GCM will send each message +individually. Note that the order of delivery is not guaranteed.</p> + +<p>GCM will store up to 100 non-collapsible messages. After that, all messages +are discarded from GCM, and a new message is created that tells the client how +far behind it is.</p> + +<p>The application should respond by syncing with the server to recover the +discarded messages. </p> + +<h4 id="which">Which should I use?</h4> + <p>If your application does not need to use non-collapsible messages, collapsible +messages are a better choice from a performance standpoint. However, if you use +collapsible messages, remember that <strong>GCM only allows a maximum of 4 different collapse +keys to be used by the GCM server per registration ID at any given time</strong>. You must +not exceed this number, or it could cause unpredictable consequences.</p> + +<h3 id="ttl">Setting an Expiration Date for a Message</h3> +<p>You can use the <code>time_to_live</code> parameter in the send request +to specify the maximum lifespan of a message. +The value of this parameter must be a duration from 0 to 2,419,200 seconds, and +it corresponds to the maximum period of time for which GCM will store and try to +deliver the message. Requests that don't contain this field default to the maximum +period of 4 weeks.</p> +<p>Here are some possible uses for this feature:</p> +<ul> + <li>Video chat incoming calls</li> + <li>Expiring invitation events</li> + <li>Calendar events</li> +</ul> +<h4 id="bg">Background </h4> +<p>GCM usually delivers messages immediately after they are sent. However, +this might not always be possible. For example, if the platform is Android, +the device could be turned off, offline, or otherwise unavailable. +Or the sender itself might request +that messages not be delivered until the device becomes active by using the +<code>delay_while_idle</code> flag. Finally, GCM might intentionally delay messages +to prevent an application from consuming excessive resources and negatively +impacting battery life.</p> + +<p>When this happens, GCM will store the message and deliver it as soon as it's +feasible. While this is fine in most cases, there are some applications for which +a late message might as well never be delivered. For example, if the message is +an incoming call or video chat notification, it will only be meaningful for a +small period of time before the call is terminated. Or if the message is an +invitation to an event, it will be useless if received after the event has ended.</p> + +<p>Another advantage of specifying the expiration date for a message is that GCM +will never throttle messages with a <code>time_to_live</code> value of 0 seconds. +In other words, GCM will guarantee best effort for messages that must be delivered +"now or never." Keep in mind that a <code>time_to_live</code> value of +0 means messages that can't be delivered immediately will be discarded. However, +because such messages are never stored, this provides the best latency for +sending notifications.</p> + +<p>Here is an example of a JSON-formatted request that includes TTL:</p> +<pre class="prettyprint pretty-json"> +{ + "collapse_key" : "demo", + "delay_while_idle" : true, + "registration_ids" : ["xyz"], + "data" : { + "key1" : "value1", + "key2" : "value2", + }, + "time_to_live" : 3 +}, +</pre> + + +<h3 id="multi-senders">Receiving Messages from Multiple Senders</h3> + +<p>GCM allows multiple parties to send messages to the same application. For +example, suppose your application is an articles aggregator with multiple +contributors, and you want each of them to be able to send a message when they +publish a new article. This message might contain a URL so that the application +can download the article. Instead of having to centralize all sending activity in +one location, GCM gives you the ability to let each of these contributors send +its own messages.</p> + +<p>To make this possible, all you need to do is have each sender generate its own +project number. Then include those IDs in the sender field, separated by commas, +when requesting a registration. Finally, share the registration ID with your +partners, and they'll be able to send messages to your application using their +own authentication keys.</p> + +<p>Note that there is limit of 100 multiple senders.</p> + +<h3 id="lifetime">Lifetime of a Message</h3> + +<p>When a 3rd-party server posts a message to GCM and receives a message ID back, +it does not mean that the message was already delivered to the device. Rather, it +means that it was accepted for delivery. What happens to the message after it is +accepted depends on many factors.</p> + +<p>In the best-case scenario, if the device is connected to GCM, the screen is on, +and there are no throttling restrictions (see <a href="#throttling">Throttling</a>), +the message will be delivered right away.</p> + +<p>If the device is connected but idle, the message will still be +delivered right away unless the <code>delay_while_idle</code> flag is set to true. +Otherwise, it will be stored in the GCM servers until the device is awake. And +that's where the <code>collapse_key</code> flag plays a role: if there is already +a message with the same collapse key (and registration ID) stored and waiting for +delivery, the old message will be discarded and the new message will take its place +(that is, the old message will be collapsed by the new one). However, if the collapse +key is not set, both the new and old messages are stored for future delivery. +Collapsible messages are also called <a href="#s2s">send-to-sync messages</a>.</p> + +<p class="note"><strong>Note:</strong> There is a limit on how many messages can +be stored without collapsing. That limit is currently 100. If the limit is reached, +all stored messages are discarded. Then when the device is back online, it receives +a special message indicating that the limit was reached. The application can then +handle the situation properly, typically by requesting a full sync. +<br><br> +Likewise, there is a limit on how many <code>collapse_key</code>s you can have for +a particular device. GCM allows a maximum of 4 different collapse keys to be used +by the GCM server per device +any given time. In other words, the GCM server can simultaneously store 4 different +send-to-sync messages, each with a different collapse key. If you exceed this number +GCM will only keep 4 collapse keys, with no guarantees about which ones they will be. +See <a href="#s2s">Send-to-sync messages</a> for more information. +</p> + +<p>If the device is not connected to GCM, the message will be stored until a +connection is established (again respecting the collapse key rules). When a connection +is established, GCM will deliver all pending messages to the device, regardless of +the <code>delay_while_idle</code> flag. If the device never gets connected again +(for instance, if it was factory reset), the message will eventually time out and +be discarded from GCM storage. The default timeout is 4 weeks, unless the +<code>time_to_live</code> flag is set.</p> + +<p>Finally, when GCM attempts to deliver a message to the device and the +application was uninstalled, GCM will discard that message right away and +invalidate the registration ID. Future attempts to send a message to that device +will get a <code>NotRegistered</code> error. See <a href="#unreg"> +How Unregistration Works</a> for more information.</p> +<p>Although is not possible to track the status of each individual message, the +Google Cloud Console stats are broken down by messages sent to device, messages +collapsed, and messages waiting for delivery.</p> + +<h3 id="throttling">Throttling</h3> +<p>To prevent abuse (such as sending a flood of messages to a device) and +to optimize for the overall network efficiency and battery life of +devices, GCM implements throttling of messages using a token bucket +scheme. Messages are throttled on a per application and per <a href="#collapsible">collapse +key</a> basis (including non-collapsible messages). Each application +collapse key is granted some initial tokens, and new tokens are granted +periodically therefter. Each token is valid for a single message sent to +the device. If an application collapse key exhausts its supply of +available tokens, new messages are buffered in a pending queue until +new tokens become available at the time of the periodic grant. Thus +throttling in between periodic grant intervals may add to the latency +of message delivery for an application collapse key that sends a large +number of messages within a short period of time. Messages in the pending +queue of an application collapse key may be delivered before the time +of the next periodic grant, if they are piggybacked with messages +belonging to a non-throttled category by GCM for network and battery +efficiency reasons.</p> -<ol> - <li>The system receives the incoming message and extracts the raw key/value -pairs from the message payload, if any.</li> - <li>The system passes the key/value pairs to the targeted Android application -in a <code>com.google.android.c2dm.intent.RECEIVE</code> Intent as a set of -extras.</li> - <li>The Android application extracts the raw data -from the <code>com.google.android.c2dm.intent.RECEIVE</code><code> </code>Intent -by key and processes the data.</li> -</ol> -<p>See the documentation for each connection server for more detail on how it -handles responses.</p> diff --git a/docs/html/google/google_toc.cs b/docs/html/google/google_toc.cs index 0c48a0a..4e8e638 100644 --- a/docs/html/google/google_toc.cs +++ b/docs/html/google/google_toc.cs @@ -169,22 +169,15 @@ <span class="en">HTTP</span></a></li> </ul> </li> + <li><a href="<?cs var:toroot?>google/gcm/server-ref.html"> + <span class="en">Server Reference</span></a> + </li> <li><a href="<?cs var:toroot?>google/gcm/notifications.html"> <span class="en">User Notifications</span></a> </li> - <li><a href="<?cs var:toroot?>google/gcm/adv.html"> - <span class="en">Advanced Topics</span></a> - </li> <li><a href="<?cs var:toroot?>google/gcm/c2dm.html"> <span class="en">Migration</span></a> </li> - <li id="gcm-tree-list" class="nav-section"> - <div class="nav-section-header"> - <a href="<?cs var:toroot ?>reference/gcm-packages.html"> - <span class="en">Reference</span> - </a> - <div> - </li> </ul> </li> |