0

Per a Google Cloud Messaging For Android (GCM) Simple Tutorial, I have created an android application for push notification using GCM services. Now I successfully get the notification in a device or emulator but problem is that I am getting only in one device (device used for testing purpose), not in other application installed devices.

final String regId = GCMRegistrar.getRegistrationId(this);

Using this code I am getting the registration id, the output I have copied from logcat window and send manually to my .Net developer. He copied that id in server side and thus for that device I am getting the notification. Please help me to send that registration id dynamically to the server side(Asp.net server). So that he can store those ids in database and pass array of registration ids to Google GCM Server. I think then only we can get notification for all devices(installed application). Because now in the server side, we are passing the data to GCM server in this way:

string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + deviceIDs + "";

Please correct me if my procedure is going wrong somewhere.

1 Answer 1

1

Basically what you'll want to do is open a data connection to your web server and send your gcmid to it with some other identifying factor of the phone (such as a userid or the phone id) you'll want to do this in your OnRegistered Method of the GCMIntentService class. I send a Form Post request to the webserver. On the webserver you will probably want to store the information in a database. I use a multidimensional array for my form variables

public String formPost(String[][] Vars) {

    String url = "put url of web server here";
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    String tmp = null;
    String rturn = null;
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        for (int i = 0; i < Vars.length; i++) {
            nameValuePairs.add(new BasicNameValuePair(Vars[i][0], Vars[i][1]));
        }
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        String ttmp = parseISToString(response.getEntity().getContent());
        Log.i("test", ttmp);
        if (ttmp.contains("Success")) {
            rturn = ttmp;
            Log.i("test", "Success:" + ttmp);
        } else {
            rturn = ttmp;
            Log.i("test", "Fail:" + ttmp);
        }

    } catch (ClientProtocolException e) {

    } catch (IOException e) {
    } /*
     * catch (RuntimeException e){ Context context =
     * getApplicationContext(); CharSequence text =
     * "There was an error try again later."; int duration =
     * Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text,
     * duration);toast.show(); Log.i("test",e.getMessage()); finish(); }
     */
    return rturn;

}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your reply David. Having some of the queries. That i edited above.
Hi Zhydian, thank you for ur response, having some other quaries,
it will be great if clear the too, i had given my doubt as an another question..please refer the link..stackoverflow.com/questions/14318115/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.