GCM(2)GCM Demo Application
GCM(2)GCM Demo Application
This demo consists of the following pieces:
-
A web server containing a page where you can send messages
-
An Android application that receives and displays such messages.
Setting Up GCM in the GCM(1) blog.
Setting Up the Server …snip...
Using App Engine for Java Long time since I last play with app engine, https://appengine.google.com.
-
From the SDK Manager, install Extras> Google Cloud Messaging for Android Library
-
The sample project is under this directory
/opt/android-sdk/extras/google/gcm/samples/gcm-demo-appengine
Setting Up the Device The client demo is here.
/opt/android-sdk/extras/google/gcm/samples/gcm-demo-client
Error Message: [C2DMReg] handleRequest caught javax.net.ssl.SSLException: Not trusted server certificate
javax.net.ssl.SSLException: Not trusted server certificate
at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshacke
at android.net.SSLCertificateSocketFactory.verifyHostname
Error Message: Intent service name: GCMIntentService-1044806436104-1
GCMRegistrar internal error: retry receiver class not set yet.
GCMBaseIntentService handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null
Solution: The problem is not code related, but link to the test phone. The test phone do not have a SIM, and the clock was not set. There is a wrong time there.
Error Message on Server Side: com.google.android.gcm.demo.server.SendMessageServlet sendSingleMessage: Exception posting Message() com.google.android.gcm.server.InvalidRequestException: HTTP Status Code: 401 at com.google.android.gcm.server.Sender.sendNoRetry(Sender.java:177) at com.google.android.gcm.demo.server.SendMessageServlet.sendSingleMessage(SendMessageServlet.java:88) at com.google.android.gcm.demo.server.SendMessageServlet.doPost(SendMessageServlet.java:68) at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166) at com.google.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:102) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:35) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418) at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:266) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:326) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923) at com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:76) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:146) at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:447) at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:454) at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:461) at com.google.tracing.TraceContext.runInContext(TraceContext.java:703) at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:338) at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:330) at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:458) at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:251) at java.lang.Thread.run(Thread.java:679)
Solution: It store an old wrong key in the database, just delete it and replace with this new key.
Key for server apps (with IP locking).
Error Message while I am using the emulator: java.lang.unsupportedoperationexception device does not have package com.google.android.gsf
Solution: The virtual device I create has no GOOGLE API support. I will create another AVD.
I made the demo works in my own projects 4mymessage and EasyRestClientAndroid.
And I change some point from the original example:
- That is the content sent by server side
Message message = new Message.Builder().addData("message", "Message I want you to know.").build();
I change the Message.Builder to carry some customized message, and this message can be replaced by my business logic part.
-
How to receive and display the customized message On the client side protected void onMessage(Context context, Intent intent) { Log.i(TAG, "Received message"); String message = getString(R.string.gcm_message);
String extra_message = intent.getStringExtra("message"); message = message + extra_message;
displayMessage(context, message); // notifies user generateNotification(context, message); }
We can get the message from getStringExtra method.
References: https://developer.android.com/google/gcm/demo.html
http://sillycat.iteye.com/blog/586786
http://sillycat.iteye.com/blog/586787
http://sillycat.iteye.com/blog/770453
http://sillycat.iteye.com/blog/774529
http://sillycat.iteye.com/blog/774534
http://sillycat.iteye.com/blog/772524
http://www.techques.com/question/1-5104500/javax.net.ssl.SSLHandshakeException:-Could-not-verify-SSL-certificate-for:-https:--android.apis.google.com-c2dm-send
http://stackoverflow.com/questions/2642777/trusting-all-certificates-using-httpclient-over-https
http://stackoverflow.com/questions/2012497/accepting-a-certificate-for-https-on-android
http://androidmyway.wordpress.com/2012/07/09/gcm-demo/
http://stackoverflow.com/questions/11398470/google-cloud-messaging-gcm-service-not-available
http://android.amolgupta.in/2012/07/google-cloud-messaging-gcm-tutorial.html
http://stackoverflow.com/questions/11339445/com-google-android-gsf-package-couldnt-be-found