androidpush-notificationgoogle-cloud-messagingpubnubgcmlistenerservice

Not able to read data from GCM Push notification using PubNub


I am getting pushnotification from PubNub. But while getting pushnotification it doesn't have any data to read on it.

1) I am publishing message to PubNub as follow:

JSONObject data = new JSONObject();
    JSONObject aps = new JSONObject();
    //Sample data
    try {
        data.put("data", "Android");
        aps.put("aps", "iOS");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Map<String, Object> payload = new HashMap<>();
    payload.put("pn_apns", aps);
    payload.put("pn_gcm", data);
    payload.put("pn_debug", true);

    pubnub.publish()
            .message(payload)
            .channel("Activity_" + prefs.getActivityDetails().activity_id)
            .async(new PNCallback<PNPublishResult>() {
                @Override
                public void onResponse(PNPublishResult result, PNStatus status) {
                    Log.i("", "");
                }
            });

This Request is success, I am getting status code 200

2) I am getting listener on GcmListenerService class. But if you see Bundle object 'data' doesn't have key called 'message' to read which we passed. Please see attached screenshot below.

Is there anything wrong on my Publish request. enter image description here

3) My debug console shows success message on devices successfully received the push notification that were registered for push messages on the target channel for each push service. '"Devices found for push notification apns: 0 gcm: 2 mpns: 0"' As shown attached screen-shot. enter image description here

Pls let me know where I am going wrong & correct me pls.


Solution

  • I got answer. Issue is on JSONObject library. Need to use JsonObject instead of JSONObject.

    Sample piece of code for payload:

    JsonObject payload = new JsonObject(); 
    JsonObject androidData = new JsonObject(); 
    androidData.addProperty(“Id”, 1); 
    androidData.addProperty(“Name”,“Marcelo”); 
    JsonObject data = new JsonObject(); 
    data.add(“data”, androidData); 
    payload.add(“pn_gcm”, data); 
    payload.addProperty(“pn_debug”, true); 
    System.out.println("before pub: " + payload);