summaryrefslogtreecommitdiffstats
path: root/docs/html/google/gcm/gcm.jd
blob: 3d6594da8c9d9b1178e2af6f9390bbd9dd0acfe0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
page.title=Overview
@jd:body

<div id="qv-wrapper">
<div id="qv">

<h2>In this document</h2>

<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>
</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
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),
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>

<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>

<p>This table summarizes the key terms and concepts involved in GCM. It is
divided into these categories:</p>
<ul>
  <li><strong>Components</strong> &mdash; The entities that play a primary role in
GCM.</li>
  <li><strong>Credentials</strong> &mdash; The IDs and tokens that are used in
different stages of GCM to ensure that all parties have been authenticated, and
that the message is going to the correct place.</li>
</ul>

<p class="table-caption" id="table1">
  <strong>Table 1.</strong> GCM components and credentials.</p>

<table>
  <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>
  <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>
  </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>
  </tr>
  <tr>
    <th colspan="2">Credentials</th>
  </tr>
  <tr>
    <td id="senderid"><strong>Sender ID</strong></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>
  </tr>
  <tr>
    <td><strong>Application ID</strong></td>
    <td>The Android application that is registering to receive messages. The Android application
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>
  </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>
  </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>

<h2 id="arch">Architectural Overview</h2>

<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>

<img src="{@docRoot}images/gcm/GCM-arch.png">

<p class="img-caption">
  <strong>Figure 1.</strong> GCM Architecture.
</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 &quot;client app&quot;) running on a device.
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
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.
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
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.
For more information on how to implement the client app, see
<a href="client.html">Implementing GCM Client</a>.</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><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>

<h3 id="register">Enable GCM</h3>

<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>

<h3 id="push-process">Send a message</h3>

<p>Here is the sequence of events that occurs when the application server sends a 
message:</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>

<p> An Android application can  unregister GCM if it no longer wants to receive 
messages.</p>

<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>

<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>