blob: d4bb45e05c2ebb545741dfb1ff0646b8abac63eb [file] [log] [blame]
Katie McCormick0ab93262013-11-01 18:10:51 -07001page.title=Overview
Katie McCormick76c6d862012-06-25 09:56:29 -07002@jd:body
3
4<div id="qv-wrapper">
5<div id="qv">
6
Katie McCormick76c6d862012-06-25 09:56:29 -07007<h2>In this document</h2>
8
9<ol class="toc">
Katie McCormick0ab93262013-11-01 18:10:51 -070010 <li><a href="#key">Key Concepts</a></li>
11 <li><a href="#arch">Architectural Overview</a></li>
kmccormick01371752014-12-12 15:00:37 -080012 <li><a href="#lifecycle">Lifecycle Flow</a></li>
13 <li><a href="#reg">Register to enable GCM</a></li>
Katie McCormick76c6d862012-06-25 09:56:29 -070014</ol>
15
Katie McCormick76c6d862012-06-25 09:56:29 -070016</div>
17</div>
18
kmccormick01371752014-12-12 15:00:37 -080019<p>Google Cloud Messaging (GCM) is a free service that enables developers
20to send downstream messages (from servers to GCM-enabled client apps), and
21upstream messages (from the GCM-enabled client apps to servers).
22This could be a lightweight message telling the client app
Katie McCormick0ab93262013-11-01 18:10:51 -070023that there is new data to be fetched from the server (for instance, a "new email"
kmccormick01371752014-12-12 15:00:37 -080024notification informing the app that it is out of sync with the back end),
Katie McCormick0ab93262013-11-01 18:10:51 -070025or it could be a message containing up to 4kb of payload
Katie McCormick4e11e3e2012-08-29 15:10:00 -070026data (so apps like instant messaging can consume the message directly). The GCM
kmccormick01371752014-12-12 15:00:37 -080027service handles all aspects of queueing of messages and delivery to and from
28the target client app.</p>
Katie McCormick76c6d862012-06-25 09:56:29 -070029
Katie McCormick0ab93262013-11-01 18:10:51 -070030
31<h2 id="key">Key Concepts</h2>
32
Katie McCormick76c6d862012-06-25 09:56:29 -070033<p>This table summarizes the key terms and concepts involved in GCM. It is
34divided into these categories:</p>
35<ul>
Katie McCormick0ab93262013-11-01 18:10:51 -070036 <li><strong>Components</strong> &mdash; The entities that play a primary role in
Katie McCormick76c6d862012-06-25 09:56:29 -070037GCM.</li>
38 <li><strong>Credentials</strong> &mdash; The IDs and tokens that are used in
kmccormick01371752014-12-12 15:00:37 -080039GCM to ensure that all parties have been authenticated, and
Katie McCormick76c6d862012-06-25 09:56:29 -070040that the message is going to the correct place.</li>
41</ul>
42
Katie McCormick0ab93262013-11-01 18:10:51 -070043<p class="table-caption" id="table1">
44 <strong>Table 1.</strong> GCM components and credentials.</p>
45
Katie McCormick76c6d862012-06-25 09:56:29 -070046<table>
47 <tr>
48 <th colspan="2">Components</th>
49 </tr>
kmccormick01371752014-12-12 15:00:37 -080050<tr>
Katie McCormick0ab93262013-11-01 18:10:51 -070051 <td><strong>GCM Connection Servers</strong></td>
kmccormick01371752014-12-12 15:00:37 -080052 <td>The Google-provided servers involved in sending messages between the
533rd-party app server and the client app.</td>
54 </tr>
55 <tr>
56 <td><strong>Client App</strong></td>
57 <td>A GCM-enabled client app that communicates with a 3rd-party app server.</td>
58 </tr>
59 <tr>
60 <td><strong>3rd-party App Server</strong></td>
61 <td>An app server that you write as part of implementing
62GCM. The 3rd-party app server sends data to a client app via
63the GCM connection server.</td>
Katie McCormick76c6d862012-06-25 09:56:29 -070064 </tr>
65 <tr>
Katie McCormick0ab93262013-11-01 18:10:51 -070066 <th colspan="2">Credentials</th>
Katie McCormick76c6d862012-06-25 09:56:29 -070067 </tr>
68 <tr>
Katie McCormick6b372622013-11-15 16:02:58 -080069 <td id="senderid"><strong>Sender ID</strong></td>
Katie McCormick0ab93262013-11-01 18:10:51 -070070 <td>A project number you acquire from the API console, as described in
71<a href="gs.html#create-proj">Getting Started</a>. The sender
72ID is used in the <a href="#register">registration process</a> to identify a
kmccormick01371752014-12-12 15:00:37 -0800733rd-party app server that is permitted to send messages to the client app.</td>
Katie McCormick76c6d862012-06-25 09:56:29 -070074 </tr>
75 <tr>
Katie McCormick6b372622013-11-15 16:02:58 -080076 <td id="apikey"><strong>Sender Auth Token</strong></td>
kmccormick01371752014-12-12 15:00:37 -080077 <td>An API key that is saved on the 3rd-party app
78server that gives the app server authorized access to Google services.
79The API key is included in the header of POST requests.
80</td>
81 </tr>
82 <tr>
83 <td><strong>Application ID</strong></td>
84 <td>The client app that is registering to receive messages. How this is implemented
85is platform-dependent. For example, an Android app
86is identified by the package name from the <a href="client.html#manifest">manifest</a>.
87This ensures that the messages are targeted to the correct Android app.</td>
88 </tr>
89 <tr>
90 <td><strong>Registration ID</strong></td>
91 <td>An ID issued by the GCM servers to the client app that allows
92it to receive messages. Note that registration IDs must be kept secret.
93
94</td>
Katie McCormick76c6d862012-06-25 09:56:29 -070095 </tr>
kmccormickeeaac812013-04-18 10:57:59 -070096
Katie McCormick76c6d862012-06-25 09:56:29 -070097</table>
98
Katie McCormick0ab93262013-11-01 18:10:51 -070099<h2 id="arch">Architectural Overview</h2>
100
101<p>A GCM implementation includes a Google-provided
102connection server, a 3rd-party app server that interacts with the connection
kmccormick01371752014-12-12 15:00:37 -0800103server, and a GCM-enabled client app. For example, this diagram shows GCM
104communicating with a client app on an Android device:</p>
Katie McCormick0ab93262013-11-01 18:10:51 -0700105
106<img src="{@docRoot}images/gcm/GCM-arch.png">
107
108<p class="img-caption">
109 <strong>Figure 1.</strong> GCM Architecture.
110</p>
111
112<p>This is how these components interact:</p>
113<ul>
114 <li>Google-provided <strong>GCM Connection Servers</strong> take messages from
kmccormick01371752014-12-12 15:00:37 -0800115a 3rd-party app server and send these messages to a
116GCM-enabled client app (the &quot;client app&quot;).
Katie McCormick0ab93262013-11-01 18:10:51 -0700117Currently Google provides connection servers for <a href="http.html">HTTP</a>
118and <a href="ccs.html">XMPP</a>.</li>
kmccormick01371752014-12-12 15:00:37 -0800119 <li>The <strong>3rd-Party App Server</strong> is a component that you
Katie McCormick0ab93262013-11-01 18:10:51 -0700120implement to work with your chosen GCM connection server(s). App servers send
121messages to a GCM connection server; the connection server enqueues and stores the
kmccormick01371752014-12-12 15:00:37 -0800122message, and then sends it to the client app.
Katie McCormick0ab93262013-11-01 18:10:51 -0700123For more information, see <a href="server.html">Implementing GCM Server</a>.</li>
kmccormick01371752014-12-12 15:00:37 -0800124 <li>The <strong>Client App</strong> is a GCM-enabled client app.
125To receive GCM messages, this app must register with GCM and get a
Katie McCormick0ab93262013-11-01 18:10:51 -0700126registration ID. If you are using the <a href="ccs.html">XMPP</a> (CCS) connection
kmccormick01371752014-12-12 15:00:37 -0800127server, the client app can send "upstream" messages back to the 3rd-party app server.
Katie McCormick0ab93262013-11-01 18:10:51 -0700128For more information on how to implement the client app, see
kmccormick01371752014-12-12 15:00:37 -0800129the documentation for your platform.</li>
Katie McCormick0ab93262013-11-01 18:10:51 -0700130</ul>
131
132<h2 id="lifecycle">Lifecycle Flow</h2>
Katie McCormick76c6d862012-06-25 09:56:29 -0700133
Katie McCormick76c6d862012-06-25 09:56:29 -0700134<ul>
kmccormick01371752014-12-12 15:00:37 -0800135 <li><strong>Register to enable GCM</strong>. A client app registers to receive messages.
136For more discussion, see <a href="#register">Register to enable GCM</a>.</li>
137 <li><strong>Send and receive downstream messages</strong>.
138 <ul>
139 <li>Send a message. A 3rd-party app server sends messages to the client app:
140 <ol>
141 <li>The 3rd-party app server <a href="server.html#send-msg">sends a message</a>
142to GCM connection servers.</li>
143 <li>The GCM connection server enqueues and stores the message if the device is offline.</li>
144 <li>When the device is online, the GCM connection server sends the message to the device. </li>
145 <li>On the device, the client app receives the message according to the platform-specific implementation.
146See your platform-specific documentation for details.</li>
147 </ol>
148 </li>
149 <li>Receive a message. A client app
150receives a message from a GCM server. See your platform-specific documentation for details
151on how a client app in that environment processes the messages it receives.</li>
152 </ul>
153</li>
Katie McCormick0ab93262013-11-01 18:10:51 -0700154
kmccormick01371752014-12-12 15:00:37 -0800155 <li><strong>Send and receive upstream messages</strong>. This feature is only available if
156you're using the <a href="ccs.html">XMPP Cloud Connection Server</a> (CCS).
157<ul>
158 <li>Send a message. A client app sends messages to the 3rd-party app server:
159 <ol>
160 <li>On the device, the client app sends messages to XMPP (CCS).See your platform-specific
161 documentation for details on how a client app can send a message to XMPP (CCS).</li>
162 <li>XMPP (CCS) enqueues and stores the message if the server is disconnected.</li>
163 <li>When the 3rd-party app server is re-connected, XMPP (CCS) sends the message to the 3rd-party app server.</li>
164 </ol>
165 </li>
166 <li>Receive a message. A 3rd-party app server receives a message from XMPP (CCS) and then does the following:
167 <ol>
168 <li>Parses the message header to verify client app sender information.
169 <li>Sends "ack" to GCM XMPP connection server to acknowledge receiving the message.
170 <li>Optionally parses the message payload, as defined by the client app.
171 </ol>
172</li>
Katie McCormick76c6d862012-06-25 09:56:29 -0700173</ul>
174
kmccormick01371752014-12-12 15:00:37 -0800175</li>
Katie McCormick76c6d862012-06-25 09:56:29 -0700176
Katie McCormick76c6d862012-06-25 09:56:29 -0700177
kmccormick01371752014-12-12 15:00:37 -0800178</ul>
179
180<h2 id="reg">Register to enable GCM</h2>
181
182<p>Regardless of the platform you're developing on, the first step
183a client app must do is register with GCM. This section covers some of the general
184best practices for registration and unregistration. See your platform-specific docs for
185details on writing a GCM-enabled client app on that platform.</p>
186
187<h3 id="reg-state">Keeping the Registration State in Sync</h3>
188<p>Whenever the app registers as described in
189<a href="{@docRoot}google/gcm/client.html">Implementing GCM Client</a>,
190it should save the registration ID for future use, pass it to the
1913rd-party server to complete the registration, and keep track of
192whether the server completed the registration. If the server fails
193to complete the registration, the client app should retry passing the
194registration ID to 3rd-party app server to complete the registration.
195If this continues to fail, the client app should unregister from GCM.</p>
196
197<p>There are also two other scenarios that require special care:</p>
198<ul>
199 <li>Client app update</li>
200 <li>Backup and restore
201 </li>
202</ul>
203<p><bold>Client app update:</bold> When a client app is updated, it should invalidate its existing registration
204ID, as it is not guaranteed to work with the new version. The recommended way to achieve
205this validation is by storing the current app version when a registration
206ID is stored. Then when the app starts, compare the stored value with
207the current app version. If they do not match, invalidate the stored data
208and start the registration process again.</p>
209
210<p><bold>Backup and restore: </bold> You should not save the registration ID when an app is
211backed up. This is because the registration ID could become invalid by the time
212the app is restored, which would put the app in an invalid state
213(that is, the app thinks it is registered, but the server and GCM do not
214store that registration ID anymore&mdash;thus the app will not get more
215messages). The best practice is to initiate the registration process as if the app has been
216installed for the first time.</p>
217
218<h4 id="canonical">Canonical IDs</h4>
219<p>If a bug in the app triggers multiple
220registrations for the same device, it can be hard to reconcile state and you might
221end up with duplicate messages.</p>
222<p>GCM provides a facility called &quot;canonical registration IDs&quot; to easily
223recover from these situations. A canonical registration ID is defined to be the ID
224of the last registration requested by your app. This is the ID that the
225server should use when sending messages to the device.</p>
226<p>If later on you try to send a message using a different registration ID, GCM
227will process the request as usual, but it will include the canonical registration
228ID in the <code>registration_id</code> field of the response. Make sure to replace
229the registration ID stored in your server with this canonical ID, as eventually
230the ID you're using will stop working.</p>
231
232<h3 id="retry">Automatic Retry Using Exponential Back-Off</h3>
233
234<p>When registration or unregistration fails, the app should retry the failed operation.</p>
235<p>In the simplest case, if your app attempts to register and GCM is not a
236fundamental part of the app, the app could simply ignore the error
237and try to register again the next time it starts. Otherwise, it should retry the
238previous operation using exponential back-off. In exponential back-off, each time
239there is a failure, it should wait twice the previous amount of time before trying
240again.
Katie McCormick76c6d862012-06-25 09:56:29 -0700241</p>
Katie McCormick76c6d862012-06-25 09:56:29 -0700242
kmccormick01371752014-12-12 15:00:37 -0800243<h3 id="unreg">Unregistration</h3>
Katie McCormick76c6d862012-06-25 09:56:29 -0700244
kmccormick01371752014-12-12 15:00:37 -0800245<p>This section explains when you should unregister in GCM and what happens
246when you do.</p>
Katie McCormick76c6d862012-06-25 09:56:29 -0700247
kmccormick01371752014-12-12 15:00:37 -0800248<h4 id="unreg-why">Why you should rarely unregister</h4>
249
250<p>You should only need to unregister in rare cases, such as
251if you want an app to stop receiving messages, or if you suspect that the registration ID has
252been compromised. In general, once an app has a registration ID, you shouldn't need
253to change it.</p>
254
255<p>In particular, you should never unregister your app as a mechanism for
256logout or for switching between users, for the following reasons:</p>
257
258<ul>
259 <li>A registration ID isn't associated with a particular
260 logged in user. If you unregister and then re-register, GCM may return the same
261 ID or a different ID&mdash;there's no guarantee either way.</li>
262
263 <li>Unregistration may take up to 5 minutes to propagate.</li>
264 <li>After unregistration, re-registration may again take up to 5 minutes to
265propagate. During this time messages may be rejected due to the state of being
266unregistered, and after all this, messages may still go to the wrong user.</li>
267</ul>
268
269
270<p>To make sure that messages go to the intended user:</p>
271
272<ul>
273 <li>Your app server can maintain a mapping between the current user
274and the registration ID.</li>
275 <li>The app can then check to ensure that messages it
276receives match the logged in user.</li>
277</ul>
278
279
280<h4 id="unreg-how">How unregistration works</h4>
281
Eric Gilmoref1598d12015-03-17 15:46:15 -0700282<p>A client app can be automatically unregistered after it is uninstalled.
kmccormick01371752014-12-12 15:00:37 -0800283However, this process does not happen right away. What happens in
284this scenario is as follows:</p>
Katie McCormick76c6d862012-06-25 09:56:29 -0700285<ol>
kmccormick01371752014-12-12 15:00:37 -0800286 <li>The end user uninstalls the client app.</li>
287 <li>The 3rd-party app server sends a message to GCM server.</li>
288 <li>The GCM server sends the message to the GCM client on the device.</li>
289 <li>The GCM client on the device receives the message and detects that the client app has been
290 uninstalled; the detection details depend on the platform on which the client app is running.
291</li>
292 <li>The GCM client on the device informs the GCM server that the client app was uninstalled.</li>
293 <li>The GCM server marks the registration ID for deletion.</li>
294 <li>The 3rd-party app server sends a message to GCM.</li>
295 <li>The GCM returns a <code>NotRegistered</code> error message to the 3rd-party app server.</li>
296 <li>The 3rd-party app server deletes the registration ID.
297 </li>
Katie McCormick76c6d862012-06-25 09:56:29 -0700298</ol>
299
kmccormick01371752014-12-12 15:00:37 -0800300<p>Note that it might take a while for the registration ID be completely removed
301from GCM. Thus it is possible that messages sent during step 7 above gets a valid
302message ID as response, even though the message will not be delivered to the client app.
303Eventually, the registration ID will be removed and the server will get a
304<code>NotRegistered</code> error, without any further action being required from
305the 3rd-party server (this scenario happens frequently while an app is
306being developed and tested).</p>
Katie McCormick76c6d862012-06-25 09:56:29 -0700307