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());
}
運行for循環消息和註冊ID :)如果你需要進一步協助郵政編碼。 – iNan
我同意iNan,你應該粘貼一些代碼來獲得幫助。首先你需要註冊到服務器(從android),併發送從C2DM收到的令牌,當你得到新的msg推送時,服務器遍歷所有註冊用戶(他有他們的令牌)併發送每個令牌+ msg給C2DM – Li3ro
他們要求每個設備的谷歌身份驗證令牌代碼 – VaaS