我正在阅读有关 GCM 的信息,同时我正在尝试 docs(extras/google/gcm/gcm-server/)
和extras/google/gcm/gcm-client/
.
客户端(设备)的注册过程工作正常。但是,当我尝试向已注册的设备发送消息时,它给了我错误,即使添加了一台设备或将多台设备添加到服务器。
这是代码:
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
List<String> devices = Datastore.getDevices();
StringBuilder status = new StringBuilder();
if (devices.isEmpty()) {
status.append("Message ignored as there is no device registered!");
} else {
List<Result> results;
// NOTE: check below is for demonstration purposes; a real application
// could always send a multicast, even for just one recipient
if (devices.size() == 1) {
// send a single message using plain post
String registrationId = devices.get(0);
Result result = sender.send(getMessage(), registrationId, 5); //THIS IS LINE NUMBER 75
results = Arrays.asList(result);
} else {
// send a multicast message using JSON
MulticastResult result = sender.send(getMessage(), devices, 5);
results = result.getResults();
}
// analyze the results
for (int i = 0; i < devices.size(); i++) {
Result result = results.get(i);
if (result.getMessageId() != null) {
status.append("Succesfully sent message to device #").append(i);
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// same device has more than on registration id: update it
logger.finest("canonicalRegId " + canonicalRegId);
devices.set(i, canonicalRegId);
status.append(". Also updated registration id!");
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
// application has been removed from device - unregister it
status.append("Unregistered device #").append(i);
String regId = devices.get(i);
Datastore.unregister(regId);
} else {
status.append("Error sending message to device #").append(i)
.append(": ").append(error);
}
}
status.append("<br/>");
}
}
req.setAttribute(HomeServlet.ATTRIBUTE_STATUS, status.toString());
getServletContext().getRequestDispatcher("/home").forward(req, resp);
}
private Message getMessage() {
Message.Builder builder = new Message.Builder();
/*Message message = new Message.Builder()
.collapseKey(collapseKey)
.timeToLive(3)
.delayWhileIdle(true)
.addData("key1", "value1")
.addData("key2", "value2")
.build();*/
return builder.build();
}
注意:代码与文档中给出的相同,我只是添加了getMessage()
.
注册一台设备时,控制台出现错误
SEVERE: Servlet.service() for servlet SendAllMessagesServlet threw exception
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:75)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:662)
注意:向 SendAllMessagesServlet.java:75 添加了注释。见上面的代码。
当不止一个
Error sending message to device #0: Unavailable
我该如何解决这个问题?