GCM(2)GCM Demo Application

本文介绍了如何使用Google Cloud Messaging (GCM)进行消息推送的全过程,包括设置服务器、客户端设备及解决常见错误的方法。通过实际案例,展示了如何发送和接收定制化消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

GCM(2)GCM Demo Application

This demo consists of the following pieces:

1. A web server containing a page where you can send messages

2. 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.


1. From the SDK Manager, install Extras> Google Cloud Messaging for Android Library


2. 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:


1. 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.


2. 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值