Google cloud messaging (GCM) is a service provided by google for android developers to send data to their android applications. Likewise, sending message from applications to server is also possible in this service.
Predominant characteristics of GCM
- Lightweight messages (up to 4kb of message)
- Doesn’t have any built in interfaces or libraries
- Third party servers send messages to their android applications registered with Google Cloud Server.
- Authenticates every message –like client app and server.
- Sends messages to multiple devices at a time.
Key components of GCM
- Client Android Application:
The GCM enabled Android app with the version greater than Android 2.2 needs to be installed from Google Play Store and needs to be logged into at least one google account with that device.
- 2. Third Party Application Server:
Any third party server that is registered with Google Cloud Server has to send their messages to the applications.
- 3. Google Cloud Server:
Google Cloud Server involves sending message to android application which was sent by third party registered server.
How does it work?
- 1. A project id given by Google API console for these services is to identify the third party servers to send message using this id. Before that we need to turn on these services on Cloud Console as in the image below.
- 2. Application Id registered with an android device has the ability to receive messages from the Google server. These messages are sent to Google server by a third-party server.
- 3. API key is provided by the cloud server which is registered with a third party server that sends messages to Android applications.
- 4. Registration id for the android application is issued by the Cloud Server. The application sends this id to the third party server as its identity.
Here is the diagram that shows the Project id and API key on the Google Cloud console.
How to register an Android application to receive messages?
The above steps show how to register the android application to get the messages from a third-party server.
- 1. The first permission to be set is to send registration id to the third-party server via Google Cloud server.
- 2. The application must have to log into a google account.
- 3. This is used to keep device’s processor from sleeping when the message is received.
- 4. Prevents other android applications from registering and receiving the same messages from Google cloud server. This permission is defined in the format of “Application Package+ permission.C2DMESSAGE”.
- 5. Using this permission, android application has to register with the server and receive messages.
Collapse key is set as true so the old messages can be deleted. Currently, only 100 messages will be in queue without collapsing.
The device will receive messages even when it is idle, if the value is set to ‘false’. If we set the value as ‘true’, then the message will arrive at the time the device is not idle.
The default timeout for messages are 4 weeks unless the time_to_live flag is set as true.
To prevent messages flooding your devices and to optimize the battery life of android device , use throttling that’s a token bucket scheme. Each application is granted their own collapse key even if it a single message. If collapse key exhausts, then the cloud server sends a new token and the messages are queued until the new token is created.
What happens when users uninstall the application?
- GCM server gets message from the third party server.
- The GCM server send message to the device.
- The application receives messages from GCM server and queries package manager whether they have broadcast to configure and receive messages.
- The client application informs the GCM server that the app is uninstalled.
- The GCM server marks the registration id for deletion.
- Then GCM server gets message from the third party server.
- The GCM sends an error message to the third party server.
- Then third party server deletes the registration ID.
The Google Cloud Messaging is an effective service that enables Android applications and devices to receive from and send to third-party servers.