到目前为止,我已经成功地使用注册 ID 和带有签名 C2DM 角色帐户的 authtoken 向一台设备发送消息。现在我已经向多个用户发送消息。我不知道如何实现这一点。
任何人都可以帮助克服这个问题。
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());
}