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

时间:2019-03-26 09:30:15

标签: node.js push-notification apple-push-notifications firebase-cloud-messaging android-push-notification

我正在使用云功能(node.js)将通知发送到设备。我的有效载荷设置如下:

a | b | c  d
d | 3 | r  9
r | r| 8  u

根据Firebase文档,您可以将“ android”和“ apns”字段用于特定于设备的行为。以下是FCM发送的消息的JSON表示形式found here

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,
            }
        }

    }
};

为什么会出现错误{ "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".

1 个答案:

答案 0 :(得分:0)

我无法根据您的帖子告诉您正在使用哪个版本,但是请注意,“平台替代”功能仅适用于v1,而 旧版仅可用。

此外,我不确定是否只是从示例有效负载中删除了一些项目,但是其中有很多不必要的逗号(,)破坏了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
            }
        }

    }
}

只需根据需要再次切换变量。