Get data from GCM notification

31,412

Solution 1

If you are using the new GCM library, then you need to create a class that extends IntentService, this is where the GCM library will notify you when a GCM message is received. Please take a look at MyIntentService.java sample:

@Override
public final void onHandleIntent(Intent intent) {
    try {
        String action = intent.getAction();
        if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) {
            handleRegistration(intent);
        } else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) {
            handleMessage(intent);
        }
    } finally {
        synchronized(LOCK) {
            sWakeLock.release();
        }
    }
}

private void handleMessage(Intent intent) {
    String id = intent.getExtra("id");
}

If you are not using the GCM library, then the GCM response is coming to you in an intent in your receiver, then you can use intent's getExtras().getString() to retrieve the key/value pair from your GCM notification. e.g.

// intent come in in your onReceive method of your BroadcastReceiver:
public onReceive(Context context, Intent intent) {
   // check to see if it is a message
   if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
      String id = intent.getExtras().getString("id");
      String other_key = intent.getExtras().getString("other_key");

      // if your key/value is a JSON string, just extract it and parse it using JSONObject
      String json_info = intent.getExtras().getString("json_info");
      JSONObject jsonObj = new JSONObject(json_info);          
   }
}

Solution 2

The best way to get it as a json representation is adding your data as a json object.

{
    "registration_ids" : [
        "id1",
        "id2"
    ],
    "data" : {
        "my_json_object": {
            "text" :"This is my message",
            "title":"Some title"
        }
    },
    "collapse_key":"12345"
}

Then to parse your object just:

String json = getIntent().getExtras().getString("my_json_object");
JsonObject jObject = new JsonObject(json);
Share:
31,412
mysho
Author by

mysho

Updated on May 13, 2020

Comments

  • mysho
    mysho about 4 years

    Is there a way to get data from "GCM notification". Here is a part of my json string which I send with gcm: {"data":{"id":"123"}}. I need get value of id in my app, but I don't know how ... thanks a lot.

  • mysho
    mysho almost 12 years
    Ok, but how can I get here (protected void onMessage(Context context, Intent intent)) notification string ?
  • azgolfer
    azgolfer almost 12 years
    Updated my answer, take a look.
  • Nam Vu
    Nam Vu almost 12 years
    How can i get gcm_notification_string from Intent?
  • azgolfer
    azgolfer almost 12 years
    @ZuzooVn, see my updated answer. I made a slight mistake in an earlier answer. You can't get raw string from what you sent in your GCM server, because the OS will parse it into key/value pairs and send it as intent to your receiver. However, you can assign a JSON string to a key, and decode it in your app.
  • Roger Alien
    Roger Alien over 11 years
    is there a way to parse bundle to json?:)
  • chris-tulip
    chris-tulip over 11 years
    you can use the GSON library to serialize objects into JSON strings
  • Allen
    Allen over 10 years
    Can a registration type of action arrive? And if so, how do I get the registration Id from it?
  • da Bich
    da Bich over 9 years
    and the best way that i've found thusfar to "add your data as a json object" is to : String json = gson.toJson([YOUR_OBJECT]); Message msg = new Message.Builder().addData("message", json).build(); gcmresult = sender.send(msg, [YOUR GCM CLIENT REG ID],5);
  • Wang'l Pakhrin
    Wang'l Pakhrin about 9 years
    How do i know what is the get string?
  • Venkatesh
    Venkatesh about 8 years
    Add your GCmBroadcastReceviver class and GcmIntentService class to receive notification