phonegap / phonegap-plugin-push

Register and receive push notifications
MIT License
1.94k stars 1.92k forks source link

Only first JSON level of additional data parsed on iOS #1713

Open maximilianloy opened 7 years ago

maximilianloy commented 7 years ago

Expected Behaviour

additionalData is:

{
  attr_1: "1",
  attr_2: "value_2",
  coldstart: false,
  content_available: "true",
  foreground: true,
  google.message_id: "0:1494235805190722%c505ff5ff9fd7ecd",
  more: {
    nested_attr_a: 3,
    nested_attr_b: "value_b"
  }
}

Actual Behaviour

additionalData is:

{
  attr_1: "1",
  attr_2: "value_2",
  coldstart: false,
  content_available: "true",
  foreground: true,
  google.message_id: "0:1494235805190722%c505ff5ff9fd7ecd",
  more: "{\"nested_attr_a\":3,\"nested_attr_b\":\"value_b\"}"
}

Reproduce Scenario (including but not limited to)

POST push message to GCM (https://gcm-http.googleapis.com/gcm/send)

POST payload:

{
  "to": "/topics/4109191475",
  "priority": "high",
  "content_available": true,
  "data": {
    "attr_1": 1,
    "attr_2": "value_2",
    "more": {
     "nested_attr_a": 3,
     "nested_attr_b": "value_b"
    }
  }
}

Steps to Reproduce

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

iOS 10.3.1 iPhone 5s + 6

(Android) What device vendor (e.g. Samsung, HTC, Sony...)

Nexus 5

Cordova CLI version and cordova platform version

cordova --version: 6.5.0
cordova platform version android: 6.2.2
cordova platform version ios: 4.4.0

Plugin version

1.10.3

Sample Push Data Payload

{
    "uniqueId": 4109191475,
    "attr_1": 1,
    "attr_2": "value_2",
    "more": {
     "nested_attr_a": 3,
     "nested_attr_b": "value_b"
    }
  }

Sample Code that illustrates the problem

Logs taken while reproducing problem

macdonst commented 7 years ago

@maximilianloy can you give me the full push payload? Also, how do you send the push info as I can't reproduce?

maximilianloy commented 7 years ago

@macdonst I updated the issue. Thank you for your help!

fredgalvao commented 7 years ago

If an additional property is not a js primitive, it should not be stringified, basically.

macdonst commented 7 years ago

@maximilianloy ah, you are using GCM on iOS then? My test was with APNS on iOS.

fredgalvao commented 7 years ago

Related to #1710?

maximilianloy commented 7 years ago

@fredgalvao sorry, but I do not understand. Is there any other way to send nested objects?

fredgalvao commented 7 years ago

@maximilianloy I was just making a note, because that other issue (#1710) is most probably the same root issue as this one: unproper parsing/stringifying of a nested object from the payload. If one is fixed, the other will probably be fixed by consequence.

maximilianloy commented 7 years ago

This is my current workaround, (recursively) iterating through the data and checking if it needs to be parsed:

function parseIfJson(str) {
  try {
    return JSON.parse(str);
  } catch (e) {
    return str;
  }
}

function iterateParse(obj) {
  for (var property in obj) {
    if (obj.hasOwnProperty(property)) {
      obj[property] = parseIfJson(obj[property]);
      if (typeof obj[property] == 'object') {
        iterate(obj[property]);
      }
    }
  }
  return obj;
}
var push = PushNotification.init({...});
push.on('notification', function(data) {
  var reparsedJson = iterateParse(data);
  console.log('Push data', reparsedJson.additionalData);
  push.finish();
});
macdonst commented 7 years ago

@maximilianloy yeah, this seems to be a bug with the way GCM on iOS delivers the info to the plugin. When I send via APNS on iOS with a payload of:

{"aps":{"alert":"Testing.. (2)","badge":1,"sound":"default", "data": {
    "attr_1": 1,
    "attr_2": "value_2",
    "more": {
     "nested_attr_a": 3,
     "nested_attr_b": "value_b"
    }
  }
}}

then the additionalData object is correctly setup.

ghost commented 7 years ago

Could that be the root cause for this issue https://github.com/phonegap/phonegap-plugin-push/issues/1577#issuecomment-300920397 as well?

fredgalvao commented 7 years ago

@bardu Considering the following payload json as the one the OP reports there, I'd say it could indeed:

{
  "data": {
    "title": "",
    "message": "",
    "image": "",
    "extra": {//nestedObject
      "url": ""
    }
  },
  "to": "device-id",
  "notification": {
    "title": "",
    "body": ""
  }
};
ghost commented 7 years ago

@fredgalvao That's what I'm thinking too!

maximilianloy commented 7 years ago

This is still the case using FCM and version 2.0.0