7

我正在阅读有关 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 

我该如何解决这个问题?

4

5 回答 5

3

您可以按照本指南为 Android 应用程序生成浏览器密钥

于 2013-12-11T12:30:48.887 回答
2

按照这个链接的步骤。我有这个链接的工作,它的工作对我来说很好

http://lalit3686.blogspot.in/2012/07/android-steps-to-run-gcm-demo.html

于 2012-08-07T12:36:36.913 回答
0

我不确定这是否有帮助,但关于 401,文档说

“401:表示用于验证发件人的ClientLogin AUTH_TOKEN无效。”

于 2012-07-17T14:17:34.593 回答
0

在 Google API 的控制台中,确保使用浏览器应用程序密钥(因为文档示例是浏览器应用程序)和服务器应用程序密钥。

再次使用新密钥生成war文件项目

ant war

并再次尝试该过程,注册设备并发送消息。

于 2012-07-18T15:50:24.793 回答
0

在 Google API 的控制台中,确保您使用浏览器应用程序密钥(因为文档示例是浏览器应用程序)和服务器应用程序密钥。

然后,使用新密钥重新生成 war 文件项目。

通过注册设备并发送消息再次尝试该过程。

于 2013-11-04T12:38:34.850 回答