2

我能够从 android 设备注册到 appengine 一旦我向 appengine 注册,我就会在 android 应用程序上获得registrationId。使用registrationId我试图将消息从appengine服务器发送到android应用程序。

Sender sender = new Sender(App-Key);
Message message = new Message.Builder().addData("data", "hello client").build();
Result result = sender.send(message, registrationId , 5);

我已将上述代码嵌入到 http post 方法中。

但我收到以下错误:

Uncaught exception from servlet
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.server.Sender.send(Sender.java:121)
    at com.google.android.gcm.demo.server.SendAllMessagesServlet.doPost(SendAllMessagesServlet.java:86)
    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:249)
    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:135)
    at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:477)
    at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:449)
    at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:455)
    at com.google.tracing.TraceContext.runInContext(TraceContext.java:695)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:333)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:325)
    at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:453)
    at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:251)

谁能帮我解决这个问题。

4

2 回答 2

1

您应该捕获 GCMIntent 服务的 onReceiveMessage 推送意图,并使用推送意图的附加 (getExtras()) 包创建通知。

遵循本指南:http: //developer.android.com/guide/topics/ui/notifiers/notifications.html

于 2012-07-23T02:16:26.803 回答
0

我已通过对代码进行以下更改来解决此问题

对于单个 android 设备:

Sender sender = new Sender(App-Key);
Message message = new Message.Builder().build();
Result result = sender.send(message, registrationId , 5);

对于多个安卓设备:

Sender sender = new Sender(App-Key);
Message message = new Message.Builder().build();
MulticastResult result = sender.send(message, list of registrationIds , 5);

然而,通知从 appengine 发送到 android 设备,但我仍然想知道如何将消息与通知一起发送到 android 设备。

于 2012-07-20T04:05:23.507 回答