解析为 Firebase 增强推送通知

Parse to Firebase enhanced push notifications

我正在将 Parse 迁移到 Firebase,但我在使用增强推送通知时遇到了问题。

解析数据(iOS 端)就像:

{"ast":
  {"alert": {
    {"body": "body_test",
     "title": "title_test",
     "description": "description",
     "endpoint-proposal": "https://.."
     "launch-image": "https://..."
  },
  "sound": "chime",
  ... 
}

使用 Firebase API ast 标签是 ['notification']['body'].

如果我发送

['notification']['body'] = 'Hello' 

它完美运行并生成以下推送:

{"ast":
  {"alert": "Hello"}
}...

所以,这里的问题是,我需要在那个标签中发送字典(警报),但我不能这样做,因为 firebase 将值设置为字符串。

python中的示例:

alert = dict()
alert['title'] = 'title'
alert['description'] = 'description'
alert['endpoint-proposal'] = 'https://..'
alert['launch-image'] = 'https://..'

fcm_payload['notification']['body'] = alert 

send_push()

在iOS这边我得到:

[AnyHashable("gcm.message_id"): 0:123456789,
 AnyHashable("aps"): {
    alert = "{\"body\": \"body\",
              \"launch-image\": \"https://...\",
              \"endpoint-proposal\": \"https://...\",
              \"description\": \"description\",
              \"title\": \"title\"}";
}]

始终为字符串 :S

有什么方法可以将警报作为字典发送吗?

json.loads() 应该给你一个字典。

notification body parameter will always be treated by FCM as a String. It's just the behavior. What you'll have to do is make use of the data 负载并放入您自定义的 key-value 对:

On iOS, if the message is sent via APNS, it represents the custom data fields. If it is sent via FCM connection server, it would be represented as key value dictionary in AppDelegate application:didReceiveRemoteNotification:.

可以在 Receiving Messages in iOS 文档中查看更多详细信息。我认为对于你的情况,你只需要在你的有效负载中一起使用 notificationdata 参数。