Google Cloud Messaging (GCM)allows your Cloud Endpoints to send notifications to registered Android devices whenever the state of a resource changes. For example, suppose a user uses a Note application from 2 devices: Device A and Device B. If the user is adding a note from Device A. Google Cloud Messaging can be used in the Cloud Endpoint for the insert operation to ping Device B to indicate that a note has been added.
To add GCM to an Endpoint:
-
In the Google APIs Console, enable Google Cloud Messaging:
-
In the Google APIs Console, create a server API key to enable App Engine
to securely communicate with the Google Cloud Messaging Server:
-
In the App Engine project, locate
MessageEndpoint.java
, which is a helper class for pinging all registered devices. UpdateMessageEndpoint.API_KEY
to specify the API key obtained in the previous step. -
In the App Engine project, modify the generated Endpoint class to send pings
to registered devices. To illustrate, refer to the following code snippet
which would be inserted in the insertNote(Note) method to send a ping to all
registered devices:
MessageEndpoint.sendMessage(user.getEmail() + ":" + note.getId() + ":insert");
-
In the Android project, locate
GCMIntentService.java
, which is a helper class for registering the device and performing actions when a message is received from GCM.-
Specify the Google APIs Project Number in
GCMIntentService.java
in the PROJECT_NUMBER field. The project number can be obtained from the URL to the API console dashboard for your project. For xample:https://code.google.com/apis/console/#project: your-project-number
- Modify GCMIntentService.onMessage() to handle the incoming GCM notification.
-
In your Android project, call
GCMIntentService.register(..)
from your application (eg: in theonCreate()
of your activity) to register the device with the Google Cloud Messaging Server and App Engine.
-
Specify the Google APIs Project Number in