Firebase FCM - 为什么我得到(有效载荷错误,无效的'android'(或'apns')属性)

敢说敢作敢为, 无怨无恨无悔。这篇文章主要讲述Firebase FCM - 为什么我得到:有效载荷错误,无效的' android' (或' apns' )属性相关的知识,希望能为你提供帮助。
我正在使用云功能(node.js)向设备发送通知。我的有效负载设置如下:

const payload = { notification: { title: payloadSender, body: payloadMessage, }, data: { chatId: chatId, }, android: { priority: 'normal', collapse_key: chatId, //todo how to set badge? notification: { sound: 'messageSent.wav', }, }, apns: { headers: { 'apns-priority': '5', 'apns-collapse-id': chatId, }, payload: { aps: { badge: newUnreads, sound: 'messageSent.wav', 'content-available': 1, } }} };

根据Firebase文档,您可以使用“android”和“apns”字段来设置特定的行为。以下是FCM发送的消息的JSON表示found here:
{ "name": string, "data": { string: string, ... }, "notification": { object(Notification) }, "android": { object(AndroidConfig) }, "webpush": { object(WebpushConfig) }, "apns": { object(ApnsConfig) },// Union field target can be only one of the following: "token": string, "topic": string, "condition": string // End of list of possible types for union field target. }

为什么我得到错误Messaging payload contains an invalid "android" property. Valid properties are "data" and "notification".Messaging payload contains an invalid "apns" property. Valid properties are "data" and "notification".
答案我无法根据您的帖子确定您使用的是哪个版本,但最好注意平台覆盖功能仅适用于v1而不适用于Legacy。
另外,我不确定你是否刚从示例有效负载中删除了一些项目,但是有很多不必要的逗号(,)在那里打破了JSON。尝试使用在线JSON格式化程序来仔细检查您的有效负载。我试过你的一个,并在删除所有错误后结束了这个:
{ "notification": { "title": "payloadSender", "body": "payloadMessage" }, "data": { "chatId": "chatId" }, "android": { "priority": "normal", "collapse_key": "chatId", //todo how to set badge? IIRC, Badges can be enabled via method inside the Android Notification builder "notification": { "sound": "messageSent.wav" } }, "apns": { "headers": { "apns-priority": "5", "apns-collapse-id": "chatId" }, "payload": { "aps": { "badge": "newUnreads", "sound": "messageSent.wav", "content-available": 1 // Double check this one if you are to actually use content-available or content_available for FCM } }} }

【Firebase FCM - 为什么我得到(有效载荷错误,无效的' android' (或' apns' )属性)】只需根据需要再次切换变量。

    推荐阅读