Google announced the release of Google Cloud Messaging, aka GCM, as a replacement for its Cloud to Device Messaging (C2DM) protocol in one of the past Google IO Conferences.
Google Cloud Messaging for Android (GCM) is a service that allows you to send data from your server to your users’ Android-powered device. This could be a lightweight message telling your app there is new data to be fetched from the server (for instance, a movie uploaded by a friend), or it could be a message containing up to 4kb of payload data (so apps like instant messaging can consume the message directly).
The GCM service handles all aspects of queueing of messages and delivery to the target Android application running on the target device. GCM is completely free no matter how big your messaging needs are, and there are no quotas.
Previously they were supported through C2DM(Cloud to Device Messaging framework) but that API has been deprecated and the Google Cloud Messaging services adds enhancements above and beyond what C2DM offered.
Here are the primary characteristics of Google Cloud Messaging (GCM):
- It allows 3rd-party application servers to send messages to their Android applications.
- An Android application on an Android device doesn’t need to be running to receive messages. The system will wake up the Android application via Intent broadcast when the message arrives, as long as the application is set up with the proper broadcast receiver and permissions.
- It does not provide any built-in user interface or other handling for message data. GCM simply passes raw message data received straight to the Android application, which has full control of how to handle it. For example, the application might post a notification, display a custom user interface, or silently sync data.
- It requires devices running Android 2.2 or higher that also have the Google Play Store application installed, or an emulator running Android 2.2 with Google APIs. However, you are not limited to deploying your Android applications through Google Play Store.
- It uses an existing connection for Google services. For pre-3.0 devices, this requires users to set up their Google account on their mobile devices. A Google account is not a requirement on devices running Android 4.0.4 or higher.
- Components – The physical entities that play a role in GCM.
- Credentials – The IDs and tokens that are used in different stages of GCM to ensure that all parties have been authenticated, and that the message is going to the correct place.
|Mobile Device||The device that is running an Android application that uses GCM. This must be a 2.2 Android device that has Google Play Store installed, and it must have at least one logged in Google account if the device is running a version lower than Android 4.0.4. Alternatively, for testing you can use an emulator running Android 2.2 with Google APIs.|
|3rd-party Application Server||An application server that developers set up as part of implementing GCM in their applications. The 3rd-party application server sends data to an Android application on the device via the GCM server.|
|GCM Servers||The Google servers involved in taking messages from the 3rd-party application server and sending them to the device.|
|Sender ID||A project number you acquire from the API console, as described in Getting Started. The sender ID is used in the registration process to identify an Android application that is permitted to send messages to the device.|
|Application ID||The Android application that is registering to receive messages. The Android application is identified by the package name from the manifest. This ensures that the messages are targeted to the correct Android application.|
|Registration ID||An ID issued by the GCM servers to the Android application that allows it to receive messages. Once the Android application has the registration ID, it sends it to the 3rd-party application server, which uses it to identify each device that has registered to receive messages for a given Android application. In other words, a registration ID is tied to a particular Android application running on a particular device.|
|Google User Account||For GCM to work, the mobile device must include at least one Google account if the device is running a version lower than Android 4.0.4.|
|Sender Auth Token||An API key that is saved on the 3rd-party application server that gives the application server authorized access to Google services. The API key is included in the header of POST requests that send messages.|
Here are the primary processes involved in cloud-to-device messaging:
- Enabling GCM. An Android application running on a mobile device registers to receive messages.
- Sending a message. A 3rd-party application server sends messages to the device.
- Receiving a message. An Android application receives a message from a GCM server.
Google API Console
- Creating a Google API project
- Enabling the GCM Service
- Obtaining an API Key
- Copy the gcm.jar file into your application classpath.
- Write the my_app_package.GCMIntentService class
- Write your application’s main activity
GCM replaces C2DM
Android Cloud to Device Messaging (C2DM) is deprecated. Developers must use GCM for new development. The migration of the app implies some small changes.
Sending messages to GCM devices requires a few changes:
- The request should be sent to a new endpoint: https://android.googleapis.com/gcm/send.
- The Authorization header of the request should contain the API key generated during sign up. This key replaces the deprecated ClientLogin Auth token.
Using GCM in apps from Roweb Portfolio – HEY YOU
GCM is used in one of our projects called Hey You.
Hey You is a location-based social app which allows you to search people who match your interests list. This application finds your best matches and helps you to socialize and meet new people and friends. Thanks to the smart user suggestion system of Hey you, the user can easily reach the most appropriate user profiles.
The main features of this great app are:
- CREATE ACCOUNT – the user has the possibility to create an account and create his interests list.
- MAPS – the user location is displayed on the map and also the position of the other users.
- PRIVACY SETTINGS – the user can or not to share his details and also to be or not visible for the other users.
- SELECT MOOD – according with his feelings and condition the user can select his mood which will be displayed on the map.
- DYNAMIC PROFILE – the matching criteria of user can be changed and the search will be made according with this new interests list.
- LIVE CHAT – the user sends and receives messages. The chat room is public or private.
- PUSH NOTIFICATION – on every action (new message, new invitation to chat) the user is notified in real time.
And also we use many technologies to obtain a excellent app like:
- JSON content type structure
- Google Cloud Messaging
- Google Maps V2
- SQLite local database using GreenDAO ORM
- presentation layer: XML
- Android 2.2 +
The users have the possibility to chat using GCM. The fun part of Google Cloud Messaging is to actually receive push messages from the cloud.
In the app the user can create one or many chat rooms and the messages are received in real time. Even if the user is not online the messages will be saved in Cloud and when the device will have an internet connection then all cached messages will be displayed.
In this post you have seen how the message flow has changed when using Google Cloud Messaging. With the previous model you had to contact your server directly from the device, now you only tell Google Services what to do with the new upstream messaging model. Google’s services take care of actually delivering the message to your server.
You also have learned about how to set up the project in Google’s developer console and that you have to sign in to use the new features.
Finally you have seen how easy the Android code actually is and what the benefits of upstream messages are.
The main thing to remember though is Find a tool for the purpose, not a purpose for the tool! If you need it, it’s there – but don’t try and wedge it in where it’s not required!