18. Working with Google Cloud Messaging

Does your application need to connect to a server and download data to keep its content fresh? Does the data come in at random times, as opposed to on a schedule (such as a once-a-day news article download)? Are you considering implementing a polling mechanism so that your Android application can routinely check your application server for new content? If this is the case, you might want to look into the Google Cloud Messaging for Android (usually referred to as GCM) service instead, because it can save you a lot of work and results in more efficient use of the user’s device resources. In this chapter, we talk about this Google-provided third-party service, which is available to Android developers.

An Overview of GCM

GCM is a messaging service offered by Google that supports push-style messaging from servers and receiving messages from users. It basically allows third-party developers like you to use a single messaging service on the device instead of creating one. The GCM service is free to use and has no quota limitations.


Image Note

GCM is the successor of a Google Labs–style project named Cloud to Device Messaging (C2DM). C2DM has been deprecated, and Google is no longer accepting new users or increasing quota limits for existing users. If you are still using C2DM in your application, you are encouraged to transition to GCM. There is a great write-up on how to migrate your application over to GCM from C2DM that is easy to follow here: http://d.android.com/google/gcm/c2dm.html.


The GCM service was introduced in Android 4.1 (API Level 16). It gives your servers the ability to push data to applications efficiently. Instead of your application constantly checking for new data on its application server, a notification can be initiated from the server side. This has substantial positive ramifications in terms of device power usage and application responsiveness. In addition, your application on a user’s device is able to send messages of its own to your servers.


Image Note

Using the GCM service basically means you’re using the shared messaging channel that Google developed for its own Android applications, such as Gmail. This cuts down on the number of individual applications that must constantly stay “awake” in order to poll their application servers and check for new stuff.


Understanding GCM Message Flow

Here are the basics of how the GCM messaging process works. To push data to an application, the developer’s application server sends a simple notification to Google’s GCM servers via HTTP or Google Cloud Connection Server (CCS) using a token that a specific application installation received when it was registered. Google’s servers handle the message delivery details, pushing the data to the user’s device when it becomes available on the network. The shared messaging channel on the user’s Android device receives the message and sends out a broadcast. Your application implements a broadcast receiver, so it can wake up, receive the message, and inspect its contents. Your application can then take whatever action is necessary, such as contacting its application server for more information or to download additional content.

Understanding the Limitations of the GCM Service

Because this is a free service and all applications on the device use the same service to communicate with the Google messaging servers, there are a number of limitations to using GCM. Some of these limitations include the following:

Image The GCM service requires Android 2.2 (API Level 8) or higher.

Image The GCM service is available only on Android devices that have the Google Play store application installed. This means that devices (such as the Amazon Kindle Fire line) cannot take advantage of such services and so developers must use other alternatives.

Image Mobile devices that are running Android versions earlier than 3.0 must have their Google account set up on the device. Devices running Android version 4.0.4 or higher do not require this.

Image The messages pushed to the device have a size limit for message data payload. At the time of this writing, that limit is 4096 bytes, and messages are stored by the GCM service for a maximum of four weeks.

Image Google has imposed a throttling mechanism that is intended to prevent abuse of the service by oversending messages to a user’s device. Google does this by providing tokens for sending messages, and once all the tokens have been used, GCM will not be able to send out messages until more tokens become available. The GCM service is not intended for time-critical situations. Messages are delivered in a timely manner, but there is no time limit. Therefore, it’s not appropriate for alarm apps and the like.

Image Of course, the GCM service requires the user to have a network-enabled device. The good news is that Google has created a robust push mechanism that stores messages to be sent to the device and is fault-tolerant enough to handle the sporadic network connectivity frequently experienced with mobile devices. Still, your users may incur data charges for network activity, as normal.

These limitations are pretty reasonable for most small- and medium-size applications that are published on Google Play. Given how straightforward the implementation is, we see no reason not to use the GCM service.

If your application cannot work within these limitations, you need to use an alternative method of messaging within your application. See the alternatives discussed at the end of this chapter for some options.

Signing Up for GCM

To use Google’s GCM service, you must have a Google Developer Console account so that you can create a Google API project. You create a project number when creating a Google API project, which you will need to use as the sender ID for your application. You can create an account or log in to the Google Developer Console at https://cloud.google.com/console. Within the Google Developer Console, you need to enable the Google Cloud Messaging for Android service, and then you must create a Public API key that you will use on your application servers.


Image Note

When you incorporate GCM services into your Android applications, you are subject to additional terms of service, so make sure you read the fine print carefully to ensure that your application complies with the rules.


Incorporating GCM into Your Applications

After you have turned on GCM for Android, you are ready to begin using the service. Make sure you take the time to familiarize yourself with the documentation and sample code available at http://d.android.com/google/gcm/index.html.

Integrating GCM Services on the Android Client Side

The basic process for integrating GCM into your Android application is as follows:

1. Your application requires setting up the Google Play services SDK.

2. Your application requires several GCM-specific permissions.

3. If GCM messaging is required by your application, you will want to set the minimum SDK supported by your application to API Level 8.

4. Your application must register to receive GCM messages from a specific sender identifier.

5. The registration on the client side results in a registration identifier that must be delivered to the server and stored for future use.

6. Your application must implement a broadcast receiver for receiving GCM messages.

7. Your application is responsible for retrieving any data payload that is spawned by a GCM message arriving.

Integrating GCM Services on the Android Application Server Side

The process of sending notifications to Google’s GCM servers is as follows:

1. Your application server must support HTTP or CCS, which uses Extensible Messaging and Presence Protocol (XMPP) as the message transport layer. In order to make use of the CCS service, you must first sign up for it, which you can do by filling out this form: https://services.google.com/fb/forms/gcm/. Your application server should also be able to queue message requests and, ideally, perform exponential backoffs.

2. Your application server needs to maintain an authentication token and refresh it occasionally.

3. To send a message to the GCM servers, your application server must create an HTTP POST message or an XMPP <message>. This message must include information about the registration identifier, authentication token, message data, and message delivery behavior details.

4. The application server should be able to handle numerous response codes (successful and erroneous) from the GCM servers.

For more information on how to configure your application server for GCM messaging, see the GCM documentation at http://d.android.com/google/gcm/server.html.

Exploring the GCM Sample Applications

Google provides a few sample applications that help illustrate how to use GCM. There are two different client applications available, along with two different server applications, and one application showing how to use Google App Engine as your back-end server with GCM. To learn more about these applications and to download the code, check out the Google Cloud Messaging for Android project hosted by Google here: https://code.google.com/p/gcm/.

What Alternatives to GCM Exist?

In some cases, the GCM service may not be right for your application. Perhaps you are targeting devices that do not have the Google Play store installed, or your application requires a different type of message traffic from what the GCM service currently allows. In such cases, you are perfectly welcome to implement your own messaging solutions. Some options include:

Image Implement a simple server polling solution in a background Service. This works well for infrequent messages or messages that aren’t time-sensitive.

Image Leverage XMPP to implement your own messaging capabilities.

Image Find a third-party service provider that offers mobile push messaging capabilities.

Image Check the app markets through which your application is published. Some third-party app markets now publish their own push services for use by their subscribers. For example, Amazon provides notification services as part of the Amazon Web Services SDK for Android. These services can incur fees to the developer and the user.

Summary

There are numerous third-party APIs and services that can be used to create robust and interesting applications on the Android platform. One service that can greatly improve the experience your users have with their Android devices is the Google Cloud Messaging for Android service available from Google. This service allows applications to share a messaging channel, making for longer battery life and more responsive applications.

Quiz Questions

1. True or false: The GCM service was introduced in Android 2.2 (API Level 8).

2. What is the size limit for messages with the GCM service?

3. True or false: The Google Play services SDK is not required for implementing the GCM service in your application.

4. What must your client application implement in order to receive messages from the GCM service?

5. True or false: The Google Cloud Connection Server uses XMPP as the message transport layer.

Exercises

1. Review the GoogleCloudMessaging client reference documentation found here: http://d.android.com/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html.

2. Read through the “GCM Advanced Topics” documentation found here: http://d.android.com/google/gcm/adv.html.

3. Obtain an API key for the Google Cloud Messaging for Android service, download the sample applications from Google Code, and configure them to work with your API key. Then study the code to make sure you understand how the GCM service works.

References and More Information

Google Code Project: “Google Cloud Messaging for Android”:

https://code.google.com/p/gcm/

Android Google Services: “Google Cloud Messaging”:

http://d.android.com/google/gcm/index.html

Android Google Services: “Getting Started”:

http://d.android.com/google/gcm/gs.html

Android Google Services: “Implementing GCM Client”:

http://d.android.com/google/gcm/client.html

Android Google Services: “Implementing GCM Server”:

http://d.android.com/google/gcm/server.html

Android Google Services for migrating from C2DM to GCM: “Migration”:

http://d.android.com/google/gcm/c2dm.html

Android Google Services reference documentation for the GoogleCloudMessaging class:

http://d.android.com/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html

Android Google Services reference documentation for the com.google.android.gcm.server package:

http://d.android.com/reference/com/google/android/gcm/server/package-summary.html

Amazon: “AWS SDK for Android”:

http://aws.amazon.com/sdkforandroid/

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.12.170