2012-07-04 151 views
7

我正在閱讀有關GCM的內容,同時我正在嘗試文檔(extras/google/gcm/gcm-server/)extras/google/gcm/gcm-client/中給出的示例代碼。無法從服務器發佈消息:Google雲消息傳遞

客戶端(設備)的註冊過程工作正常。但是,當我嘗試向註冊設備發送消息時,即使添加了一個設備或者將多個設備添加到服務器,也會給我提供錯誤。

這裏是代碼:

@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 

我怎樣才能解決這個問題?

+0

是否有有效deviceIds存在於數據存儲? –

+0

我如何識別它們?通常,這是由設備從GCM返回的設備ID發送的字符串或字符串列表。 –

+1

谷歌小組討論:https://groups.google.com/forum/#!topic/android-gcm/ew9quCXJSjQ –

回答

0

我不知道這是否會幫助,但關於401,the docs say

「401:表示對ClientLogin AUTH_TOKEN用於驗證發件人是無效的。」

0

在Google API的控制檯中,確保您使用瀏覽器應用程序密鑰(作爲文檔的示例是瀏覽器應用程序)和服務器應用程序的密鑰。

使用使用

ant war 

新的密鑰,並再次嘗試的過程中,註冊設備和發送消息再次生成war文件的項目。

0

在谷歌API's控制檯,請確保您使用的瀏覽器應用程序鍵(如該文檔的示例是瀏覽器應用程序)和服務器應用程序的關鍵字。

然後,使用新密鑰重新生成war文件項目。

通過註冊設備併發送消息再次嘗試該過程。

3

你可以按照this guide產生的Android應用程序瀏覽器鍵

相關問題