Play with Google Play Services 6 - Push Notification
If you haven’t set up Google Play services SDK yet, please follow this tutorial. Today, we demonstrate how to use the cloud messaging / push notification.
Enable Cloud Messaging API
First, create a project in Google Developers Console for your app, and enable the Google Cloud Messaging for Android API. The project number will be used as the GCM sender ID.
Then, create a server key in Public API access with your server’s IP address (for testing purposes, you can use e.g. 0.0.0.0/0 to allow anybody to send messages with the generated API key). The generated API key will be used to authenticate your server.
Note: For devices running Android older than 4.0.4, it requires users to set up a Google account before using GCM.
Now, update your AndroidManifest.xml file, specifying the required permissions:
Register The Client
The following snippet shows how to register a client for cloud messaging:
Note that Google’s GCM server doesn’t handle localization nor message scheduling, so the client might also need to upload e.g. the preferred language and timezone to the server to improve user experiences.
In your AndroidManifest.xml, specify a listener:
Then you can handle the message yourself:
Send a Message
To send a push message, you can simply send a POST request to Google’s GCM server. The URL of the server is: https://gcm-http.googleapis.com/gcm/send.
The HTTP header must contain the following two:
- Content-Type: application/json
The HTTP body is a JSON object, something like this:
The fields of the
data object represent the key-value pairs of the message’s payload data. With the above example, the client will receive an intent with an extra of key
key_1 and value
value_1, and another extra of key
key_2 and value
priority can be either
normal (default) or
high. The messages marked as
high priority will be sent immediately, even when the device is in Doze mode. For
normal priority messages, they will be batched for devices in Doze mode, and will be discarded if the message expires while the device is in Doze mode.
For a complete list of allowed fields in the posted JSON, check here.
The response can contain the following status code:
- 200: The message is successfully processed.
- 400: The request JSON object is malformed.
- 401: The sender fails to authenticate itself.
- 5xx: Server error, the sender should respect the
Retry-Afterand retry later.
More details of how to parse the response messages can be found here.
That’s it for today. Happy hacking and keep reading!