How to Implement Google Cloud Messaging?


Hi guys, I will explain and show you a simple GCM Implementation.

gcm

Because C2DM has been officially deprecated as of June 26, 2012 :p

Refer: https://developers.google.com/android/c2dm/

Requirements:

1. Apache Ant – http://ant.apache.org/bindownload.cgi

2. Apache Tomcat – http://tomcat.apache.org/download-70.cgi

First of all you need to get an API key from Google API Console.

Getting your Sender ID

  • STEP 1.  Register Here .
  • STEP 2.  Click Create project. Your browser URL will change to something like:
    " https://code.google.com/apis/console/#project:4815162342 "

    Take note of the value after#project: (4815162342 in this example). This is your project ID, and it will be used later on as the GCM sender ID. This Id will be used by the Android Device while Registering for Push Notification.

  • STEP 3. Choose Service tab from the left side menu on the web page. and turn on “ Google Cloud Messaging for Android “
  • STEP 4. Go to API Access tab from the left menu of web page.

Note: If you going to use a static IP, provide it. If u don’t have, don worry.

Just press Create new Server key and note down the generated API key.

Refer: http://developer.android.com/guide/google/gcm/gs.html

Google has already some things to implement this. Just grab it from

http://code.google.com/p/gcm/source/checkout (You need to have git installed in ur PC)

if you don’t have git, get it from here – https://rapidshare.com/files/1274880597/gcm.zip

Server Implementation:

1. After downloading or else gitting, just extract and/or open up

gcm\samples\gcm-demo-server\WebContent\WEB-INF\classes

2. Open “api.key” with some text editor, some sample text will be there. Replace it with your API key.

3. open cmd there and navigate to the path(root of gcm-demo-server), you could find a build.xml.

Just give “ant war” in cmd. you will get a “gcm-demo.war” inside “dist” folder (gcm-demo-server\dist)

4. Copy that war file and paste it to “webapps” folder of tomcat.

Just check it whether its working “http://localhost:8080/gcm-demo”

It should give like the thing below.

Great your server implementation is over now.

Client Implementation:

Download GCMDemo (Client side) from here – https://rapidshare.com/files/3183517979/GCMDemo.zip

Extract and add it to IDE

If you have ADT or some issues with tools, follow the below things:

For this just create android project in your IDE with some name, eg: “GCMDemo”

Goto “gcm\samples\gcm-demo-client”, do the following:

1. Open up “AndroidManifest.xml” and copy it to the same xml which is in android project.

2.Open up “res” folder, you could find some folders. Just drag and drop it to “res”  folder in your android project.

3.Open up “src” folder,you will find a folder named as “src”. Just drag and drop it to “src” folder in your android project.

4.Open up “libs” folder,you will find a file named as “gcm.jar”. Just drag and drop it to “libs” folder in your android project.

For both of the steps, you need to add some spice ups for client implementation.

Open up android project in IDE. Goto “src  >> com.google.android.gcm.demo.app >> CommonUtilities.java” and do the following.

1. Replace “your_gcm-demo_url” with your server url, static IP users give your static IP.

static final String SERVER_URL = “your_gcm-demo_url”;  In my case “http://192.168.1.9/gcm-demo”

For non-static IP users:

Instead of giving “localhost, 127.0.0.1”, you should give system internal IP which u have mentioned in your network config. Because both of them are giving SocketException. So use system IPv4 Address.

To get it , XP and vista  users type “ipconfig” in your cmd. In W7, Goto Control panel>>Networking and sharing center>> Change adapter settings>>Local Area Connection”

Double click and click details where you could find your IP named as “Ipv4 Address”

2. Replace “your_sender_id_from_api_console_url” with your sender ID, like 4815162342 in this example

static final String SENDER_ID = “your_sender_id_from_api_console_url”;

Uffff! Just run your application in emulator now. Wait! wait! Just add an google account in your emulator. And then run your application. you can get following result.

Now refresh your server URL. you should get whats shown below.

Press “Send Message”. You will get a notification in your emulator.

Voila!! You did it!

If u have any doubts catch me on – https://twitter.com/satheesh420

46 thoughts on “How to Implement Google Cloud Messaging?

  1. I have some queries !!
    Can I upload the server side on an online domain ? If yes, then how ?
    How can I make changes to the page server side and server side project ?

  2. I successfully made changes to the project as were required
    but
    yeah I need to know server side changes while uploading server files to an external server

    • @khurram sorry for the late response. I guess you are aware of the thing that external server will be running on static IP only. To make it to run, Just you need to put the war file the static Ip’s tomcat folder.

  3. hi i am trying to implement gcm by following your tutorial but on server side when i create war file and placed in webapps folder and typed “http://localhost:8080/gcm-demo” but it is showing Object not found!

    The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again. what i am doing wrong here please help me out

  4. Hi,
    I have some queries , i am getting everything like device registered and unregistered in my device ,but when i click on send message in browser i wont get any notification or message in my device ,please help me out …

      • Facing same issue, everything okay as in tutorial but after clicking “send Message” there is no notification on my device..!! LogCat is below.

        11-13 15:37:00.069: D/OpenGLRenderer(22764): Enabling debug mode 0
        11-13 15:37:00.204: V/GCMBroadcastReceiver(22764): onReceive: com.google.android.c2dm.intent.REGISTRATION
        11-13 15:37:00.204: V/GCMBroadcastReceiver(22764): GCM IntentService class: com.google.android.gcm.demo.app.GCMIntentService
        11-13 15:37:00.209: V/GCMBaseIntentService(22764): Acquiring wakelock
        11-13 15:37:00.229: V/GCMBaseIntentService(22764): Intent service name: GCMIntentService-780004520994-1
        11-13 15:37:00.229: E/GCMRegistrar(22764): internal error: retry receiver class not set yet
        11-13 15:37:00.229: V/GCMRegistrar(22764): Registering receiver
        11-13 15:37:00.234: D/GCMBaseIntentService(22764): handleRegistration: registrationId = APA91bFT1W1nBlSU5WDOQ9kf-t0kIzCAZ34ewxMFbAVF4ilrKxnRtybTs9L9mLPEty6Hh9D79jsICIzUp-ZGVQXx8tt4-0nJoY2DJARd2cxsWvD6L0Nc1x-Lm3KSb5Xqs7ByT7lorlE4dhLVRShaQL758GFP5QAPCQ, error = null, unregistered = null
        11-13 15:37:00.234: D/GCMRegistrar(22764): resetting backoff for com.google.android.gcm.demo.app
        11-13 15:37:00.234: V/GCMRegistrar(22764): Saving regId on app version 1
        11-13 15:37:00.239: I/GCMIntentService(22764): Device registered: regId = APA91bFT1W1nBlSU5WDOQ9kf-t0kIzCAZ34ewxMFbAVF4ilrKxnRtybTs9L9mLPEty6Hh9D79jsICIzUp-ZGVQXx8tt4-0nJoY2DJARd2cxsWvD6L0Nc1x-Lm3KSb5Xqs7ByT7lorlE4dhLVRShaQL758GFP5QAPCQ
        11-13 15:37:00.239: I/GCMDemo(22764): registering device (regId = APA91bFT1W1nBlSU5WDOQ9kf-t0kIzCAZ34ewxMFbAVF4ilrKxnRtybTs9L9mLPEty6Hh9D79jsICIzUp-ZGVQXx8tt4-0nJoY2DJARd2cxsWvD6L0Nc1x-Lm3KSb5Xqs7ByT7lorlE4dhLVRShaQL758GFP5QAPCQ)
        11-13 15:37:00.239: D/GCMDemo(22764): Attempt #1 to register
        11-13 15:37:00.244: V/GCMDemo(22764): Posting ‘regId=APA91bFT1W1nBlSU5WDOQ9kf-t0kIzCAZ34ewxMFbAVF4ilrKxnRtybTs9L9mLPEty6Hh9D79jsICIzUp-ZGVQXx8tt4-0nJoY2DJARd2cxsWvD6L0Nc1x-Lm3KSb5Xqs7ByT7lorlE4dhLVRShaQL758GFP5QAPCQ’ to http://10.36.100.199:8080/DemoActivityServer//register
        11-13 15:37:00.259: V/GCMRegistrar(22764): Setting registeredOnServer status as true until 2013-11-20 15:37:00.267
        11-13 15:37:00.279: V/GCMBaseIntentService(22764): Releasing wakelock
        11-13 15:37:00.304: D/dalvikvm(22764): GC_CONCURRENT freed 172K, 7% free 12397K/13191K, paused 13ms+13ms, total 43ms

  5. Hello,
    First Thanks for this tutorial, i m a new comer for android and i trying your post. I have follows your instructions. I have downloaded all the above things. But When i go to Step of Server Implementation:

    3. open cmd there and navigate to the path(root of gcm-demo-server), you could find a build.xml.

    Just give “ant war” in cmd. you will get a “gcm-demo.war” inside “dist” folder (gcm-demo-server\dist)

    But when i run the above command “ant war ” in cmd it will display : ‘ant ‘ is not recognized as an internal or external commad…….
    Where i made mi stack please help me.
    I am waiting for your replays.
    thanks again…….

    • You need to have Java and ant path in System variables

      U need to create

      Variable Name:JAVA_HOME
      Variable Value:%Path to java% (C:\Program Files\Java\jdk1.7.0) in my case

      Variable Name:ANT_HOME
      Variable Value:%Path to ant% (C:\apache-ant-1.8.4) in my case

      Jus ping me after trying this. Sorry i was out of connectivity for some days..

  6. Thank you I don’t know what is the case with google Android and there documentation of you have week to translate their info then maybe you’ll get it working

    Ant Sucks!

    thanks to your information i got it going in no time

    Google should pay you !!!

  7. Hello bro!! nice tutorial you had!
    me successfully running the apps..
    i want to make some modification to that apps which is.
    run the file using run.bat file and directly send notification to client without pressing the send message button..can you help me with this?? thank you in advance..
    one more time! NICE TUTORIAL !!congratulation!

  8. Pingback: how does GCM (Google Cloud Messaging) works? : Android Community - For Application Development

  9. Pingback: Google GCM demo app – Server messages not being received : Android Community - For Application Development

  10. Pingback: Connecting to GCM server running at local server : Android Community - For Application Development

  11. I am getting “No devices registered!” after successfully registering the client app which is on emulator. The app showing “From Demo server: successfully added device” but when I refresh the server page as suggested I did not get (1) device as registered as expected.

  12. Thanks for the post,
    Well, I’m getting a weird problem here. Everything runs perfect. But when I send a message, I get nothing on the device. Nothing shows up in Logcat too. I tried my best to catch all those exceptions, but no use. Any idea where it would go wrong?

  13. Hi all,
    Could someone Re-post the source codes (Server & Client) please. those above are nolonger valid !
    Thanks.

  14. Pingback: GCM Demo Application's failure to register to tomcat server when tested on real Device | BlogoSfera

  15. hello thankyou your good tutorial.
    i have a problem that is to click sending button then

    HTTP Status 404 – /2/sendAll

    type Status report

    message /2/sendAll

    description The requested resource is not available.

    Apache Tomcat/6.0.37″

    this message. what me have to do

  16. hi..
    everything worked fine except when i clicked on send message button it gave me an error

    java.net.UnknownHostException: android.googleapis.com
    java.net.PlainSocketImpl.connect(Unknown Source)
    java.net.SocksSocketImpl.connect(Unknown Source)
    java.net.Socket.connect(Unknown Source)
    com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(Unknown Source)
    com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(Unknown Source)
    sun.net.NetworkClient.doConnect(Unknown Source)
    sun.net.www.http.HttpClient.openServer(Unknown Source)
    sun.net.www.http.HttpClient.openServer(Unknown Source)
    sun.net.www.protocol.https.HttpsClient.(Unknown Source)
    sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
    sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
    sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
    sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
    com.google.android.gcm.server.Sender.post(Sender.java:479)
    com.google.android.gcm.server.Sender.post(Sender.java:458)
    com.google.android.gcm.server.Sender.sendNoRetry(Sender.java:170)
    com.google.android.gcm.server.Sender.send(Sender.java:121)
    com.google.android.gcm.demo.server.SendAllMessagesServlet.doPost(SendAllMessagesServlet.java:83)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

  17. Pingback: Connecting to GCM server running at local server | BlogoSfera

  18. Hi Sateesh, I went through your blog and implemented the server side and client side code as described by you, however when I run the client side app, even after 5 attempts my device is not getting registered to the server.

  19. Pingback: Connecting to GCM server running at local server | Stackforum

  20. trying to use this on my windows 7 system and the ant thing is a mess. The ant.jar can’t find anything – I think it’s setup for windows NT. Any newer examples out there anywhere? Maybe something I can bring up with Eclipse?

Leave a reply to Tejas Jadhav Cancel reply