blob: 19151b94e1c03e216cb1c149790215ac97255a3f [file] [log] [blame]
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 for Android (GCM) 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>