Ad

Problem When Sending 2-layer-json-payload From FCM To IOS Devices

I had a problem with sending push notification from FCM server. Previously, we use APNS for this purpose, my server and clients create payload like this. And it worked very well.

{
  "data": {
    "image": "https://premierleague-static-files.s3.amazonaws.com/premierleague/photo/2018/09/24/0e228e97-1644-4fcf-bc18-d7223d8f398f/DreamTeamGW6.png",
    "link":"https://stackoverflow.com/"
  },
  "aps": {
    "alert": "This is me",
    "sound": "default",
    "mutable-content": 1
  },
  "contentId": "123456"
}

Now, we turn to use FCM as replacement of APNS, as you've known, FCM server will receive the message, convert it to APNS format then send it to APNS server, the APNS server will send the converted message to clients. But first, I have to follow its payload's format like this.

{
  "notification": {
    "body": "This is me",
    "badge": 1,
    "sound": "default",
    "mutable-content": 1
  },
  "delay_while_idle": false,
  "data": {
    "data": {
      "image": "https://premierleague-static-files.s3.amazonaws.com/premierleague/photo/2018/09/24/0e228e97-1644-4fcf-bc18-d7223d8f398f/DreamTeamGW6.png",
      "link":"https://stackoverflow.com/"
    },
    "contentId": "123456"
  },
  "time_to_live": 10
}

And the converted message from FCM to APNS was not I desired.

{
  "data": "{\"image\":\"https:\\\/\\\/premierleague-static-files.s3.amazonaws.com\\\/premierleague\\\/photo\\\/2018\\\/09\\\/24\\\/0e228e97-1644-4fcf-bc18-d7223d8f398f\\\/DreamTeamGW6.png\",\"link\":\"https:\\\/\\\/stackoverflow.com/\\\/\"}",
  "aps": {
    "alert": "This is me",
    "sound": "default",
    "mutable-content": 1
  },
  "contentId": "123456"
}

As you see, the value of "data" key was not JSONObject like before, it became a string. My question is: How can I make the FCM server understand that the sub "data" object inside main "data" is a JSONObject, not a string when it convert my message to APNS's payload?

Thank you!

Ad

Answer

In your FCM payload (reference for the FCM payload parameters here):

  • badge should be String
  • mutable_content should be outside notification
  • delay_while_idle is deprecated
  • data messages can only hold key-value pairs. You're passing a (data) JSON object which is not expected to work.

The fastest way I could think of in order to make this work is have the contents of your data JSON object outside (i.e. same level as the contentId and then format your FCM payload like so:

{
  "mutable-content": 1
  "notification": {
    "body": "This is me",
    "badge": 1,
    "sound": "default"
  },
  "data": {
      "image": "https://premierleague-static-files.s3.amazonaws.com/premierleague/photo/2018/09/24/0e228e97-1644-4fcf-bc18-d7223d8f398f/DreamTeamGW6.png",
      "link":"https://stackoverflow.com/",
      "contentId": "123456"
  },
  "time_to_live": 10
}

But depending on your client side code, this couldn't work, but I hope you get the gist. Cheers!

Ad
source: stackoverflow.com
Ad