Monday, July 16, 2012

Android - Steps to run GCM Demo


I would insist to first go through Android Docs and then try to implement using below steps.

Make sure you have installed Apache Tomcat and Apache Ant.

Steps:-

1.) Make sure you have dowloaded support library from SDK Manager -> "Google Cloud Messaging             for Android Library".
2.) This will add "gcm" folder with demo application in your SDK folder.
3.) So go to "your_sdk\extras\google\gcm\samples\gcm-demo-server\WebContent\WEB-INF\classes\api.key". And here enter your API-KEY that you got from registering to
    Google Console.

4.)
  •     Now you have to set System variables for Ant upto its "bin". So, edit Path and append it at the end using a semi-colon - "your_path\apache-ant-1.8.4\bin".
  •     Now set the user variable for "JAVA_HOME" - "C:\Program Files\Java\jdk1.6.0_23"(without "bin") and Ant upto its "bin" folder. Go to cmd and type ant.


Buildfile: build.xml does not exist!
Build failed

If you get above error means your Ant is working fine.

5.) Now go to "your_sdk\extras\google\gcm\samples\gcm-demo-server" and type ant.

If successful you will see output like,


init:
    [mkdir] Created dir: your_sdk\extras\google\gcm\samples\gcm-demo-
server\build\classes
    [mkdir] Created dir: your_sdk\extras\google\gcm\samples\gcm-demo-
server\dist

compile:
    [javac] Compiling 7 source files to your_sdk\extras\google\gcm\sa
mples\gcm-demo-server\build\classes

war:
      [war] Building war: your_sdk\extras\google\gcm\samples\gcm-demo
-server\dist\gcm-demo.war

dist:

BUILD SUCCESSFUL
Total time: 0 seconds


Also you will see a "dist" folder created with "gcm-demo.war" file.

6.) Now start your apache tomcat server and check that its working fine by running it using "http://your_host_ip:8080/"
7.) If its working fine go to "C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\copy gcm-demo.war" here.
8.) Restart your Server and open the gcm-demo in browser - "http://your_host_ip:8080/gcm-demo/"

You will see result as,

Unregistered device #0
No devices registered!

So, server side is completed now.


9.) Now, import "gcm-demo-client" Application from "your_sdk\extras\google\gcm\samples\gcm-demo-client"
10.) Open "CommonUtilities.java" - here you need to give
SERVER_URL="http://your_host_ip/gcm-demo/" and
SENDER_ID="sender id you got from google console url".
11.) Build "gcm-demo-client" into your Android Device to register to your Server

Succesfully sent message to device #0
1 device(s) registered!

You will see above output if successfully registered will a send button.

12.) Press Send Button to get Notification on your device.

You can find the sorce demo from github, it also include the gcm-demo.war file that you have to deploy. The things that are required to be change are,

1.) API Key,
2.) SENDER_ID
3.) SERVER_URL

Thanks :)

31 comments:

  1. Thank you for such a great explanation

    ReplyDelete
  2. I don understand step 4,
    Now set the user variable for "JAVA_HOME" - "C:\Program Files\Java\jdk1.6.0_23"(without "bin") and Ant upto its "bin" folder. Go to cmd and type ant.

    I already add JAVA_HOME(C:\Program Files\Java\jre6\) and PATH(;%JAVA_HOME%\bin;) to system environment. still not working..

    'ant' is not recognized as an internal or external command,
    operable program or batch file.

    ReplyDelete
    Replies
    1. Setting JAVA_HOME - http://wso2.org/project/wsas/java/1.1/docs/setting-java-home.html

      ant - You have to Edit System Variables and add "apache-ant-1.8.4\bin" that is path of your ant upto "bin".

      Delete
  3. I am not able to execute step5.
    I went up to "my_sdk\extras\google\gcm\samples\gcm-demo-server" in Command prompt and typed ant

    The response is''ant' is not recognized as an internal or external command,
    operable program or batch file.'

    I could successfully execute the step up to 4.


    Thanks in advance and thanks for this blog

    ReplyDelete
  4. That means you haven't set the System variable path for Ant - follow step number 4.

    ReplyDelete
  5. I got the Project Id and API Key but not able to run the application.
    I have implemented all the steps described here, but my application is not running its showing force close after step 10.I am using ubuntu 12.Please help me.

    ReplyDelete
  6. I tried this its working Thanks to Lalit Poptani! Try this!!

    ReplyDelete
  7. Nice tutorial working fine Thanks!!!!!!!!!!!!!!!!!!!!

    ReplyDelete
  8. thanks a lot.............. thanks thanks!!!!

    ReplyDelete
  9. Hi,
    i am unable to execute step 8. When i try to open the gcm-demo.war file using url "localhost:8080/gcm-demo"
    it gives an error saying
    "Not Found

    The requested URL /gcm-demo was not found on this server."
    Instead i have already copied the gcm-demo.war file into the webapps folder of Tomcat 7.0.
    and my apache HTTP Server is running successfully and shows output "It works".

    ReplyDelete
    Replies
    1. Me also have same problem. can you give solution?

      Delete
  10. After 11th step, I installed GCMDemo-debug.apk on my android device and started the GCM Demo apps and got following :
    Trying(attempt 1/5) to register device on Demo Server.
    Trying(attempt 2/5) to register device on Demo Server.
    Trying(attempt 3/5) to register device on Demo Server.
    Trying(attempt 4/5) to register device on Demo Server.
    Trying(attempt 5/5) to register device on Demo Server.
    Could not register device on Demo Server after 5 attempts.
    From GCM : recoverable error
    (SERVICE_NOT_AVAILABLE).
    From GCM: device successfully unregistered!

    Please help :(

    ReplyDelete
    Replies
    1. Plz let me know if it's fixed, m also looking for solution

      Delete
    2. hi, i have have the same problem....have you a solution?? plz

      Delete
    3. make sure you have Google Account in your Device. If you are testing in Emulator then also you need to add Google Account in it.

      Delete
  11. This comment has been removed by the author.

    ReplyDelete
  12. if anyone not able to run ANT then try this,
    Go to bin folder of ANT.....
    C:\Ant\apache-ant-1.8.4\bin .... and then type ant as below
    C:\Ant\apache-ant-1.8.4\bin>ant
    Then try this
    C:\Ant\apache-ant-1.8.4\bin>ant -buildfile C:\MyEclipse\SDK\extras\google\gcm\gcm-server\build.xml
    It will run the build.xml from SDK

    In the above my SDK was installed at C:\MyEclipse\SDK ,
    Don't forget to change to your SDK location....





    ReplyDelete
  13. Thanks alot!
    Very nice and quick tutorial.
    After hours spent on the official android and google tutorial,
    I read this one and everything worked!
    Keep up the good work :)

    ReplyDelete
  14. Hi,
    i can register but i can not send message. i got error when i send message.

    java.net.ConnectException: Connection timed out: connect
    java.net.PlainSocketImpl.socketConnect(Native Method)
    java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    java.net.Socket.connect(Socket.java:529)
    com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:550)
    com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
    sun.net.NetworkClient.doConnect(NetworkClient.java:163)
    sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
    sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
    sun.net.www.protocol.https.HttpsClient.(HttpsClient.java:272)
    sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:329)
    sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
    sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)
    sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
    sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:904)
    sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:230)
    com.google.android.gcm.server.Sender.post(Sender.java:476)
    com.google.android.gcm.server.Sender.post(Sender.java:455)
    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:82)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

    thank you very much.

    ReplyDelete
  15. I have read the steps to run GCM demo on the Android. It is really good steps steps explained in this blog that is very helpful me and other user of Android So thanks for that.

    Android App Development

    ReplyDelete
  16. How did you configue SSL for tomcat? the send message does not work

    ReplyDelete
  17. I have successed until the gcm-demo.war file creation under dist folder. But I couldn:t find "C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\" path in my pc. What should I do?

    ReplyDelete
    Replies
    1. download tomcat package in official site. Its a package..

      Delete
  18. Hello Lalit,

    Thanks for your post. I followed each steps you mentioned. Device registered to server but can not push message from server. It is giving below errors:


    type Exception report

    message HTTP Status Code: 401

    description The server encountered an internal error that prevented it from fulfilling this request.

    exception

    com.google.android.gcm.server.InvalidRequestException: HTTP Status Code: 401
    com.google.android.gcm.server.Sender.sendNoRetry(Sender.java:177)
    com.google.android.gcm.server.Sender.send(Sender.java:121)
    com.google.android.gcm.demo.server.SendAllMessagesServlet.doPost(SendAllMessagesServlet.java:82)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    note The full stack trace of the root cause is available in the Apache Tomcat/7.0.41 logs.

    ReplyDelete
    Replies
    1. Hello Everybody,

      I found the solution. It may help someone who have same problem. I added API key to file samples/gcm-demo-server/WebContent/WEB-INF/classes/api.key, and it worked.

      Thank you for your post.

      Delete
  19. I got this error while running apps :
    recoverable error (SERVICE_NOT_AVAILABLE)

    ReplyDelete
  20. Your tutorial works properly in my machine..:) good job
    but what about android mobile device i have installed apk ,it gives me successfully added but i wont get any notification from my serverside..
    and one more is i want to use another machine tomcat then tell mi process for that i changed ip and all but it wont works..
    please tell mii..
    Thanks in advance..

    ReplyDelete
  21. Your tutorial works properly in my machine..:) good job
    but what about android mobile device i have installed apk ,it gives me successfully added but i wont get any notification from my serverside..
    and one more is i want to use another machine tomcat then tell mi process for that i changed ip and all but it wont works..
    please tell mii..
    Thanks in advance.

    ReplyDelete
  22. I got 404 error in gcm-server.. I am also follow your steps. other step completely work. But when i open page into browser i got 404 Error.

    Tnx adv.

    ReplyDelete