Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
In this tutorial we show how to use Google Cloud Messaging (GCM) to create an instant messaging app like WhatsApp or Facebook Messenger. Google Cloud Messaging for Android (GCM) is a free service provided by Google that allows sending data to an Android-powered device, and also to receive messages from the device. Here are a few screenshots of the app.
The main screen is implemented using a ListView with a custom row layout to display contact info and new message count. The ActionBar contains menu items to add new contacts and to navigate to the Settings screen. The chat screen allows user to chat with a contact and to edit contact information. An instant messaging app like this can be implemented using push notification mechanism provided by GCM where server sends messages to a device so that it need not poll the server continously for messages which drains battery power. We use the latest GoogleCloudMessaging APIs that comes with Google Play services instead of the deprecated GCM helper libraries. So to test the app you'll need a real device having Google Play Store application installed, or an emulator with Google APIs running latest version of Android.
GCM requires a server-side implementation in addition to the client implementation in the app. The client ie. the app, actually posts messages to your server which in turn sends data to the GCM server that is responsible for delivering the message to a specific Android device. You may want to read the official docs on GCM architecture. We'll use Servlets and JPA to implement the server and use free quota provided by Google App Engine (GAE) to power our server. There are several steps that need to be done before we could use GCM in our application. First of all is installing the Google Play services SDK. If you haven't done it already, you may want to visit our earlier tutorial on setting up the development environment.