android - How to send push messages to multiple users using C2DM? -
so far have succeeded send messages 1 device using registration id , authtoken signed c2dm role account. have send messages multiple users . dont know how achieve this.
could overcome issue.
java servlet code,
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 grouping messages , last sent message // same key going sent phone when phone // ready message if not beginning data.append("&collapse_key=test"); // here message sending, key1 can changed whant // or if whant send more 1 can (i think, not tested // yet), testing message here. data.append("&data.key1=testing message c2dm"); // if want message wait phone not idle set // 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)) { // server temporarily unavailable sb.append("responsecode = " + responsecode); } else { if (responsecode.equals(401)) { // auth_token used validate sender invalid sb.append("responsecode = " + responsecode); } else { if (responsecode.equals(200)) { // check 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 + " empty response 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()); }
i know you're dealing c2dm, google released c2dm successor, gcm allows sending notification 1000 devices in 1 http post. if app not in market yet, suggest migrating using gcm before it's deployed. posting notification multiple devices in gcm simple putting device registration ids in json array , send google's server, this:
{ "registrations_ids": [reg_id1, reg_id2, reg_id3, reg_id10, reg_id100], "data" : "payload" }
but if insist on using c2dm, need loop sending notification, iterating through device registration ids.
Comments
Post a Comment