0

So far I have succeeded with send messages to one device using the registration Id and authtoken with signed C2DM role account. Now i have send messages to multiple users . I dont know how to achieve this.

Could anyone help to overcome this 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 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());

    }
VaaS
  • 637
  • 2
  • 12
  • 18
  • Run a for loop with message and registration id :) if u need further assistance post code. – Raghav Jun 27 '12 at 10:52
  • I agree with iNan, You should paste some code to get help. first thing you need to register to the server(from android) and send the token received from C2DM, when you got new msg to push ,the server loops through all registered users(that he has their tokens) and sends each token+msg to C2DM – Li3ro Jun 27 '12 at 10:59
  • something they asking google auth token code for each device – VaaS Jun 27 '12 at 11:11
  • Hello VaaS dont you save registration id of users in database?? – Raghav Jun 27 '12 at 11:25
  • iNan ,how to know each phone id and when app is running on other phone. could you explain. I am still confuse.. – VaaS Jun 27 '12 at 11:29
  • Well Vaas each phone has an unique deviceId, when you get a registrationId for a device immediately you post it and DeviceId to database. DeviceId primaryKey and registationId is value. Database design something like KeyValue pair. Then in above code dynamically fetch registrationId from database. – Raghav Jun 27 '12 at 11:33
  • You can get device id using `String deviceId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID)` – Raghav Jun 27 '12 at 11:39
  • Hi iNan, So at time installing and running app for first time we have to send the items i.e as you said device id and registration id to server and in server we have to save it DB and through the loop we will send messages. correct – VaaS Jun 27 '12 at 12:15
  • Thanks you so much iNan for make me clear. – VaaS Jun 27 '12 at 12:42

1 Answers1

0

I know you're dealing with C2DM, but Google just released C2dM successor, GCM that allows sending notification to 1000 devices in one HTTP post. If your app is not in the market yet, I suggest migrating to using GCM now before it's deployed. Posting a notification to multiple devices in GCM is as simple as putting all device registration ids in a JSON array and send it to google's server, like this:

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

But if you insist on using C2DM, you just need to loop sending the notification, by iterating through the device registration ids.

azgolfer
  • 15,087
  • 4
  • 49
  • 46