2012-06-27 69 views
0

到目前爲止,我已經成功地使用註冊標識和authtoken與已簽名的C2DM角色帳戶向一臺設備發送消息。現在我已經發送消息給多個用戶。我不知道如何實現這一點。如何使用C2DM將推送消息發送給多個用戶?

任何人都可以幫助解決這個問題。

的Java Servlet代碼,

public void doGet(HttpServletRequest req, HttpServletResponse resp) 
     throws IOException { 


     resp.setContentType("text/plain"); 
     StringBuilder data = new StringBuilder(); 

     data.append("registration_id=" + ServerConfig.DeviceRegistrationID_S); 

     // Collapse key is for grouping messages and only the last sent message 
     // with the same key going to be sent to the phone when the phone is 
     // ready to get the message if its not from the beginning 
     data.append("&collapse_key=test"); 



     // Here is the message we sending, key1 can be changed to what you whant 
     // or if you whant to send more then one you can do (i think, not tested 
     // yet), Testing is the message here. 
     data.append("&data.key1=Testing Message from C2DM"); 

     // If you want the message to wait to the phone is not idle then set 
     // this parameter 

// data.append(」 & delay_while_idle = 1" );

 byte[] postData = data.toString().getBytes("UTF-8"); 

     StringBuilder sb = new StringBuilder(); 

     sb.append(ServerConfig.AuthToken + " - "); 

     try { 
      // Send data 
      URL url = new URL("https://android.clients.google.com/c2dm/send"); 

      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setDoOutput(true); 
      conn.setUseCaches(false); 
      conn.setRequestMethod("POST"); 
      conn.setRequestProperty("Content-Type", 
        "application/x-www-form-urlencoded;charset=UTF-8"); 
      conn.setRequestProperty("Content-Length", 
        Integer.toString(postData.length)); 
      conn.setRequestProperty("Authorization", "GoogleLogin auth=" 
        + ServerConfig.AuthToken); 

      OutputStream out = conn.getOutputStream(); 
      out.write(postData); 
      out.close(); 

      Integer responseCode = conn.getResponseCode(); 
      if (responseCode.equals(503)) { 
       // the server is temporarily unavailable 
       sb.append("responseCode = " + responseCode); 
      } else { 
       if (responseCode.equals(401)) { 
        // AUTH_TOKEN used to validate the sender is invalid 
        sb.append("responseCode = " + responseCode); 
       } else { 
        if (responseCode.equals(200)) { 

         // Check for updated token header 
         String updatedAuthToken = conn 
           .getHeaderField("Update-Client-Auth"); 
         if (updatedAuthToken != null) { 
          ServerConfig.AuthToken = updatedAuthToken; 
          sb.append("updatedAuthToken = \"" 
            + updatedAuthToken + "\""); 
         } 

         String responseLine = new BufferedReader(
           new InputStreamReader(conn.getInputStream())) 
           .readLine(); 

         if (!sb.toString().equals("")) { 
          sb.append(" - "); 
         } 

         if (responseLine == null || responseLine.equals("")) { 
          sb.append("Got responsecode " 
            + responseCode 
            + " but a empty response from Google AC2DM server"); 
         } else { 
          sb.append(responseLine); 
         } 
        } else { 
         sb.append("responseCode = " + responseCode); 
        } 
       } 
      } 
     } catch (Exception e) { 
      if (!sb.toString().equals("")) { 
       sb.append(" - "); 
      } 

      sb.append("Exception: " + e.toString()); 
     } 

     resp.getWriter().println(sb.toString()); 

    } 
+0

運行for循環消息和註冊ID :)如果你需要進一步協助郵政編碼。 – iNan

+0

我同意iNan,你應該粘貼一些代碼來獲得幫助。首先你需要註冊到服務器(從android),併發送從C2DM收到的令牌,當你得到新的msg推送時,服務器遍歷所有註冊用戶(他有他們的令牌)併發送每個令牌+ msg給C2DM – Li3ro

+0

他們要求每個設備的谷歌身份驗證令牌代碼 – VaaS

回答

0

我知道你正在處理C2DM,但谷歌剛剛發佈C2DM接班人,GCM,允許在一個HTTP郵寄通知,1000個設備。如果您的應用尚未投放市場,我建議在部署之前立即遷移到使用GCM。張貼在GCM多個設備的通知是把所有設備註冊ID的JSON陣列並將其發送到谷歌的服務器上,這樣簡單:

{ 
    "registrations_ids": [reg_id1, reg_id2, reg_id3, reg_id10, reg_id100], 
    "data" : "payload" 
} 

但是,如果你堅持使用C2DM,您只需要循環發送通知,通過遍歷設備註冊標識。