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 :)
Thank you for such a great explanation
ReplyDeleteI don understand step 4,
ReplyDeleteNow 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.
Setting JAVA_HOME - http://wso2.org/project/wsas/java/1.1/docs/setting-java-home.html
Deleteant - You have to Edit System Variables and add "apache-ant-1.8.4\bin" that is path of your ant upto "bin".
I am not able to execute step5.
ReplyDeleteI 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
That means you haven't set the System variable path for Ant - follow step number 4.
ReplyDeleteI got the Project Id and API Key but not able to run the application.
ReplyDeleteI 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.
I tried this its working Thanks to Lalit Poptani! Try this!!
ReplyDeleteNice tutorial working fine Thanks!!!!!!!!!!!!!!!!!!!!
ReplyDeletethanks a lot.............. thanks thanks!!!!
ReplyDeleteHi,
ReplyDeletei 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".
Thanks in advance
DeleteAfter 11th step, I installed GCMDemo-debug.apk on my android device and started the GCM Demo apps and got following :
ReplyDeleteTrying(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 :(
Plz let me know if it's fixed, m also looking for solution
Deletehi, i have have the same problem....have you a solution?? plz
Deletemake sure you have Google Account in your Device. If you are testing in Emulator then also you need to add Google Account in it.
DeleteThis comment has been removed by the author.
ReplyDeleteif anyone not able to run ANT then try this,
ReplyDeleteGo 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....
Thanks alot!
ReplyDeleteVery 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 :)
Hi,
ReplyDeletei 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.
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.
ReplyDeleteAndroid App Development
How did you configue SSL for tomcat? the send message does not work
ReplyDeleteI 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?
ReplyDeleteThank nice work!
ReplyDelete