Retrieve values from notification using JSON PAYLOAD android

622 Views Asked by At

I am in a situation , where my webservice sends me notification which includes

{
    "apiKey": "xxxxx-xxx-xxx-xxxx-xxxxxxx",
    "appKey": "xxxxx-xxx-xxx-xxxx-xxxxxxx",
    "sendAll": true,
    "content": {
        "subject": "Test Lottery",
        "message": "Notification Msg",
        "action": {
            "type": "DEFAULT",
            "data": "url|intent|...",
            "labe": "label"
        },
        "payload": "{'uid':'1','type':'LD'}",
        "sound": "default.caf",
        "badge": "+1"
    }
}

On click of the notification which appeared on top , I need to get the uid and type

I am using Xtify for push notification.....

1

There are 1 best solutions below

2
Pragnesh Ghoda  シ On BEST ANSWER

Try This..

String JSON ="YOUR JSON RESULT STRING";
try {
    JSONObject jsonresult = new JSONObject(JSON);

    String apiKey= jsonresult.getString("apiKey");
    String appKey= jsonresult.getString("appKey");
    boolean sendAll= jsonresult.getBoolean("sendAll");

    JSONObject contentObject = jsonresult.getJSONObject("content");
    String subject= jsonresult.getString("subject");
    String message= jsonresult.getString("message");
    String sound= jsonresult.getString("sound");
    String badge= jsonresult.getString("badge");

    JSONObject actionObject = jsonresult.getJSONObject("action");

    String type= jsonresult.getString("type");
    String data= jsonresult.getString("data");
    String labe= jsonresult.getString("labe");

    JSONObject payloadObject = jsonresult.getJSONObject("payload");
    // do your code..

    }
} catch (JSONException e) {
    e.printStackTrace();
    //catching exceptions if any...
}