Browse through more Android tutorials. If you'd like to see a tutorial on any particular topic, do leave a comment in the wishlist page. We frequently post new tutorials along with app releases. You may subscribe to our newsletter to get all updates in your inbox.
Now you can get the latest Java source bundled with each app update. Install the app from Google Play and go to Settings > Extras.

«  Create a currency converter app Create a SMS app  »

Create an Instant Messaging app using Google Cloud Messaging (GCM)

DownloadDownload

Keywords: Google Cloud Messaging Google Play services ContentProvider SQLiteOpenHelper BroadcastReceiver NotificationManager SimpleCursorAdapter CursorLoader ActionBar DialogFragment ListFragment ViewBinder ContentResolver PreferenceFragment Google App Engine Google Plugin JPA Servlet

Contents
  • InstaChat
    • res
      • xml
        • pref_notification.xml
        • pref_general.xml
        • pref_messaging.xml
        • pref_headers.xml
      • layout
        • chat_list_item.xml
        • activity_chat.xml
        • main_list_item.xml
      • drawable-hdpi
        • ic_menu_share.png
        • ic_menu_allfriends.png
        • ic_launcher.png
        • ic_menu_preferences.png
        • ic_menu_edit.png
        • ic_menu_invite.png
        • ic_menu_delete.png
      • drawable-xhdpi
        • right.9.png
        • ic_contact_picture.png
        • left.9.png
        • ic_group_picture.png
      • values
        • strings.xml
        • strings_activity_settings.xml
        • styles.xml
      • drawable
        • box.xml
      • menu
        • main.xml
        • chat.xml
    • src
      • com
        • appsrox
          • instachat
            • gcm
              • GcmUtil.java
              • Constants.java
              • GcmIntentService.java
              • GcmListener.java
              • GcmBroadcastReceiver.java
              • ServerUtilities.java
            • Util.java
            • DataProvider.java
            • AddContactDialog.java
            • MessagesFragment.java
            • EditContactDialog.java
            • ChatActivity.java
            • CreateGroupTask.java
            • MainActivity.java
            • Common.java
            • SettingsActivity.java
    • AndroidManifest.xml

Instructions:
1. Install the app from Google Play.
2. Download the server application project from here. Import the project into your Eclipse workspace.
3. Deploy the server application on Google App Engine as described here.
4. Enter the Server URL (from step 3) in browser and verify the Demo Server is up and running.
5. Obtain an API key as described here.
6. Set the API key to the Demo Server through init.html page. for e.g. http://<your-app-id>.appspot.com/init.html
7. Obtain the GCM sender ID as explained here.
8. Run the InstaChat app and navigate to Settings screen. Provide the Server URL (from step 3) and GCM sender ID (from step 7)

Now the app would register itself with the server and a chat ID is generated. For testing purpose you may add a contact with same chat ID to send message to yourself.

Debugging:
1. If you can see chat ID and status as online in the main screen of the app then the app was able to successfully register with the server.
If not then check GAE (AppEngine Admin Console) logs for /register URI hits. If you don't find even after refreshing the logs it means the Server URL you entered in the settings screen of the app is incorrect.
Enter complete URL as follows:
http://<your-app-id>.appspot.com

2. Now if you add a contact with same chat ID as yours and send a message then you should receive it.
If not then check GAE logs for /chat URI hits. If the message was sent then you will see in the log details something like -
[ messageId=0:1370619773791918%1825aee100000031 ]
else in case of error -
[ errorCode=MismatchSenderId ]
The above error is due to mismatch in Sender ID (entered in app Settings screen) and the API key (while setting up server through init.html page)
Re-enter Sender ID in app settings screen and test.
API key cannot be reset through init.html page for security reasons. You'll have to redeploy the server project on appengine and then use init.html.

3. If you are able to send a message (as seen in the logs with proper messageId) but not receive it on your friends device having different chat ID. Please make sure that your friend has added you (your chat ID) in his list.

Logcat and GAE logs will help you in debugging.

Note:
Please use real device (phone or tablet) for initial testing. Don't mix projects provided in the tutorial with those on FB page.

Here is a pre-configured server details for testing the app -
String SERVER_URL = "http://gcm-12345.appspot.com";
String SENDER_ID = "475461000010";
Please use it for testing purpose only since the data is shared.

1. Overview

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.


Note: This tutorial is deprecated. Instead, please check out Firebase based chat app project on Github. Also, for video and voice calling functionality you may refer the WebRTC based project.

Here are a few screenshots of the app. Follow these instructions to run the app.

Screenshot Screenshot

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.

2. Integrating Google Play Services

The Google Play services SDK gets installed at the following location in the Android SDK directory.
	<android-sdk-folder>/extras/google/google_play_services/
						
To use the client library you need to import google-play-services_lib project into your Eclipse workspace. For this go to File > Import. Then, select Android > Existing Android Code into Workspace. Click Next.
In the Import Projects dialog, browse to the following directory and specify it as the Root Directory.
	<android-sdk-folder>/extras/google/google_play_services/libproject
						
Import Play services library Select google-play-services_lib and Copy projects into workspace. Click Finish. A copy of the library project will get imported into your workspace.

3. Create an API project

In order to obtain an API key you need to first create an API project through the Google APIs Console.
If you log in to the console for the first time then you'd be prompted to create a project. Create API project Click Create project. You'll then see the Dashboard for the API project. It's a good idea to create a new project for GCM. API console Dashboard Click on the project drop-down on the left side panel. Click Create... and enter a name for the project then click Create project. The project will be created and the Dashboard will refresh to reflect the current project.
Note down the Project Number displayed in the Dashboard Project Summary section. This number is also visible in the browser URL something like:
	https://code.google.com/apis/console/#project:4815162342
						
The project number (for eg. 4815162342) will be used later as the GCM sender ID.

4. Obtain an API Key

Next, click on Services on the left side panel to see all services provided by Google. Available Services Find Google Cloud Messaging for Android and click on the switch to activate the service. Activate Google Cloud Messaging for Android Next, click on API Access on the left side panel to open the API Access page. Simple API key Copy the API key as it will be used later, and you may log out of the console.

5. Create a new Eclipse Android project

In Eclipse, go to File > New > Project and in the New Project dialog, expand Android folder to select Android Project.
In New Android Project dialog enter the project details as follows.
Application Name: Insta Chat
Project Name: InstaChat
Package Name: com.appsrox.instachat
Minimum Required SDK: API 15
Compile With: API 15

Accept the defaults and click Next, Next, Next, Finish. The project gets created in your workspace.
Next we need to add google-play-services_lib project as a library to our project. Right click on the project and go to Properties > Android. Add Google Play services library In the Library section, click Add and then select google-play-services_lib and click OK. This will add google-play-services_lib as a library to the project so we can now use GoogleCloudMessaging API.
Share the love:  

Next Page » 1

App Gen
App Name:
Project Name:
Package:
Screens:
Splash
Login
Help
Main
List  Grid  Pager
Detail
Settings
Options:
Action Bar
Navigation Drawer
Dummy Data
Generate
Free Apps