使用GCM从服务器向应用程序发送消息

时间:2012-08-07 13:51:52

标签: android servlets google-cloud-messaging

我尝试从http://developer.android.com/guide/google/gcm/gs.html实施gcm 我可以将regid发送到servlet,但无法从中获取消息。

的Servlet

if("send".equals(id))
        {
            deviceHolder.put(did, reGid);
            reGid = deviceHolder.get(did);
            if(reGid== null || "".equals(reGid)){
                System.out.println("device is not registered "+did);
            }else{
                Sender sender = new Sender(myApiKey);
                Message message = new Message.Builder()
                .collapseKey("1")
                .timeToLive(3)
                .delayWhileIdle(true)
                .addData("message", "message is created at builder")
                .build();

                Result result = sender.send(message, reGid, 5);
                System.out.println("result of message is "+ result);
            }
        }

GCMINtentService:

@Override
protected void onMessage(Context arg0, Intent arg1) {
Log.i(TAG, "new message= ");
Toast.makeText(getApplicationContext(), arg1.getStringExtra("message"),
Toast.LENGTH_SHORT).show();
}

在控制台:

A?u 07, 2012 4:37:08 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [servCloud] in context with path [/servletCloud]   threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: com.google.android.gcm.server.Sender
at   org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1711)
at    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)
at servCloud.doGet(servCloud.java:75)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

A?u 07, 2012 4:40:37 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/servletCloud] has started
A?u 07, 2012 4:40:38 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/servletCloud] is completed

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

您的servlet类路径应包含GCM服务器API。确保servlet war文件中的WEB-INF / lib文件夹下有gcm-server.jar。