blob: e568c8a35e9853e753ad9f46de89ee3904e8dab2 [file] [log] [blame]
page.title=Optimizing Server-Initiated Network Use
trainingnavtop=true
@jd:body
<div id="tb-wrapper">
<div id="tb">
<h2>This lesson teaches you to</h2>
<ol>
<li><a href="#gcm">Send Server Updates with GCM</a>
</ol>
</div>
</div>
<p>
Network traffic sent by server programs to your app can be challenging to optimize. A
solution to this problem is for your appp to periodically poll the server to check for updates.
This approach can waste network connection and power when your app starts up a device's radio,
only to receive an answer that no new data is available. A far more efficient approach would be
for the to notify your app when it has new data, but figuring out how to send a notification
from your server to potentially thousands of devices was previously no easy feat.
</p>
<p>
The <a href="https://developers.google.com/cloud-messaging/gcm">Google Cloud Messaging</a> (GCM)
service solves this communication problem by allowing your servers to send notifications to
instances of your app wherever they are installed, enabling greater network efficiency and
lowering power usage.
</p>
<p>
This lesson teaches you how to apply the GCM service to reduce network use for server-initiated
actions and reduce battery consumption.
</p>
<h2 id="gcm">Send Server Updates with GCM</h2>
<p>
Google Cloud Messaging (GCM) is a lightweight mechanism used to transmit brief messages from an
app server to your app. Using GCM, your app server uses a message-passing
mechanism to notify your app that there is new data available. This approach eliminates network
traffic that your app would perform, by not contacting a backend server for new data when no
data is available.
</p>
<p>
An example use of GCM is an app that lists speaker sessions at a conference. When sessions are
updated on your server, the server sends a brief message to your app telling it updates are
available. Your app can then call the server to update the sessions on the device only when
the server has new data.
</p>
<p>
GCM is more efficient than having your app poll for changes on the server. The GCM service
eliminates unnecessary connections where polling would return no updates, and it avoids running
periodic network requests that could cause a device's radio to power up. Since GCM can be used by
many apps, using it in your app reduces the total number of network connections needed on a
device and allows the device radio to sleep more often.
</p>
<p>
For more information about GCM and how to implement it for your app, see
<a href="https://developers.google.com/cloud-messaging/gcm">Google Cloud Messaging</a>.
</p>
<p class="note">
<strong>Note:</strong> When using GCM, your app can pass messages in normal or high priority.
Your server should typically use <a href=
"https://developers.google.com/cloud-messaging/concept-options#setting-the-priority-of-a-message">
normal priority</a> to deliver messages. Using this priority level prevents devices from being
woken up if they are inactive and in a low-power <a href=
"https://developer.android.com/training/monitoring-device-state/doze-standby.html">Doze</a>
state. Use high priority messages only if absolutely required.
</p>