【问题标题】:Problem when sending 2-layer-json-payload from FCM to iOS devices将 2-layer-json-payload 从 FCM 发送到 iOS 设备时出现问题
【发布时间】:2018-09-25 02:50:06
【问题描述】:

我在从 FCM 服务器发送推送通知时遇到问题。以前,我们为此使用 APNS,我的服务器和客户端创建这样的有效负载。而且效果很好。

{
  "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"
}

现在,我们转向使用 FCM 作为 APNS 的替代品,如您所知,FCM 服务器将接收到消息,将其转换为 APNS 格式,然后将其发送到 APNS 服务器,APNS 服务器将转换后的消息发送给客户端.但首先,我必须像这样遵循其有效载荷的格式。

{
  "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
}

从 FCM 到 APNS 的转换消息不是我想要的。

{
  "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"
}

如你所见,“data”键的值不再像之前的 JSONObject,它变成了一个字符串。我的问题是:如何让 FCM 服务器了解主“数据”中的子“数据”对象是 JSONObject,而不是在将我的消息转换为 APNS 的有效负载时的字符串?

谢谢!

【问题讨论】:

    标签: ios firebase apple-push-notifications firebase-cloud-messaging


    【解决方案1】:

    在您的 FCM 有效负载 (reference for the FCM payload parameters here) 中:

    • badge 应该是字符串
    • mutable_content 应该在 notification 之外
    • delay_while_idle is deprecated
    • data 消息只能保存键值对。您正在传递一个 (data) JSON 对象,该对象预计不会起作用。

    为了完成这项工作,我能想到的最快方法是将您的 data JSON 对象的内容放在外部(即与 contentId 相同的级别,然后像这样格式化您的 FCM 有效负载:

    {
      "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
    }
    

    但是根据您的客户端代码,这无法正常工作,但我希望您了解要点。干杯!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      • 2019-12-02
      • 1970-01-01
      • 2021-08-27
      • 2011-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多