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 :)

74 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
  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
  23. When i am running ant command in step 5 it's giving some error and build failed.Can you help me on this.

    ReplyDelete
  24. How can I get registration Id for GCM?

    ReplyDelete
  25. Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

    Mobile App Development

    ReplyDelete
  26. Very nice post.Thanks for sharing such a informative post.

    Startup Product Development

    ReplyDelete
  27. Glad to know about this one. really interesting and useful. Thank you.

    ReplyDelete
  28. Thanks for providing such a useful info. Keep sharing posts like this..
    DevOps Online Training

    ReplyDelete
  29. Thanks for such a great article here. I was searching for something like this for quite a long time and at last, I’ve found it on your blog. It was definitely interesting for me to read about their market situation nowadays.iot certification chennai | iot training courses in chennai | iot training center in chennai | best iot training centre in chennai

    ReplyDelete
  30. Untuk memenangkan permainan, maka para penjudi harus mampu untuk membaca cara lawan dalam bermain. Memang kita tidak akan bertatap muka dalam bermain judi online
    asikqq
    http://dewaqqq.club/
    http://sumoqq.today/
    interqq
    pionpoker
    bandar ceme terpercaya
    freebet tanpa deposit
    paito warna
    syair sgp

    ReplyDelete
  31. Great Post,really it was very helpful for us.
    Yaaron Studios is one of the rapidly growing editing studios in Hyderabad. We are the best Video Editing services in Hyderabad. We provides best graphic works like logo reveals, corporate presentation Etc. And also we gives the best Outdoor/Indoor shoots and Ad Making services.
    Best video editing services in Hyderabad,ameerpet
    Best Graphic Designing services in Hyderabad,ameerpet­
    Best Ad Making services in Hyderabad,ameerpet­

    ReplyDelete
  32. I would like to thank you for the efforts you had made for writing this wonderful piece of writing.

    Chatbot app development company in Qatar
    Blockchain Development Company in Qatar

    ReplyDelete
  33. Great Post,really it was very helpful for us.Thanks a lot for sharing! I found this blog to be very useful!!

    Augmented Reality App development

    ReplyDelete
  34. Very Well said thank you for the great article!

    free ebook for mobile app development

    ReplyDelete
  35. Thanks for providing such a useful info. Keep sharing posts like this.
    Eye Health

    ReplyDelete
  36. Very nice post.Thanks for sharing such a informative post.

    Find Content writers

    ReplyDelete
  37. Hey! Well written article. I would suggest this blog to my friends because he is working in one of the mobile app development company in India as an android app developer. I will share this article to him which will helps for his upcoming works. Thanks for sharing information with us.

    ReplyDelete
  38. I really enjoyed your blog Thanks for sharing such an informative post.

    digital marketing course in hubli

    ReplyDelete
  39. This is a very good article and nice collection of information , would love to read more of such blogs and also know our services

    This is a very good article and nice collection of information , would love to read more of such blogs and also know our services

    ReplyDelete
  40. Feeling good to read such a informative blog, mostly i eagerly search for this kind of blog. I really found your blog informative and unique, waiting for your new blog to read.
    Digital marketing Service in Delhi
    SMM Services
    PPC Services in Delhi
    Website Design & Development Packages
    SEO Services Packages
    Local SEO services
    E-mail marketing services
    YouTube plans

    ReplyDelete
  41. Hii, i came across this blog while random browing and found great content. Such information adds up new things in the memory for staying updated in this society. If you also want to visit some good web pages then do check out our website https://digiroads.in for more offers and discounts on digital marketing services.
    digital marketing company in Jaipur
    PPC company in Jaipur
    seo company in Jaipur
    social media marketing company in Jaipur
    content writers in Jaipur
    website development company in Jaipur

    ReplyDelete
  42. This comment has been removed by the author.

    ReplyDelete
  43. Very interesting article to read it. I would like to thank you for the efforts you had made for writing this wonderful article. This article inspired me to read more. Keep sharing on updated posts…

    Learn Digital Marketing Course in Bangalore with Live Project Work & Case Studies taught by Ranjan Jena (10Yrs Trainer). 100% Guarantee to Clear Job Interview

    ReplyDelete
  44. This comment has been removed by the author.

    ReplyDelete
  45. Hi, I Read your blog and I feel it is a very wonderful, informative blog . There are a lot of good information on this blog, I'd like to Share and I think people will get a lot of support from this blog. Thank you for sharing this informative blog, please keep up and share some unique posts with us in the future
    ecommerce website development services
    Mobile App Development Company in bangalore
    SEO company services
    Web Design Company
    Digital Marketing Agency
    Packers and Movers
    Service Apartments in Bangalore

    ReplyDelete
  46. https://www.sellbitbuy.net/paxful-clone-script
    https://www.sellbitbuy.net/uniswap-clone-script

    ReplyDelete
  47. HI guys,
    This is a very good post, and I like it very much. For us, it's insightful and helpful. For a long time, I've been looking for this and I'm just really pleased to say that I'm working with this stuff as well. Thanks for sharing this with us.

    Digital Marketing Company in Jaipur
    Digital Marketing company In Delhi
    Digital Marketing Company in Bangalore
    SEO Company in Jaipur
    Website development Company in Jaipur
    PPC Company in Jaipur
    Digital Marketing Company in USA

    ReplyDelete
  48. Very interesting blog. Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome. You can also check my articles as well.

    Best Homecare Services in Bangalore
    caregiver services in bangalore

    ReplyDelete
  49. Wonderful article. It's very useful.
    It looks like you have put lot of work into this.
    SMARS designs jewelry to run along with your ever-changing wardrobe. A piece of Jewelry can either make or break your entire look; therefore, every unique outfit needs a different piece of jewelry to compliment it. But looking at the prices of traditional jewelry, we usually find occasions like festivals or ceremonies to buy it. And these adorable pieces spend most of their lives in the lockers. Komal, the founder of SMARS, understood this gap in the market. Every single piece is limited edition and walks hand-in-hand with trends. Adored by customers from all over the world, we ensure the quality delivery of our high-end, Indian fashion costume jewelry. Shop online for latest collection of Kundan, antique and temple jewelry in India check out necklace sets, earrings, bangles, chokers for girls and many more Indian jewelry sets for women available with free shipping across India.
    Take a look: Latest Fancy Chokers For Girls Online

    ReplyDelete
  50. Having read your article. I appreciate you are taking the time and the effort for putting this useful information together.

    BSc 1st Year Admit Card
    BSc 2nd Year Admit Card
    BSc 3rd Year Admit Card

    ReplyDelete