gpt4 book ai didi

javascript - FCM 消息负载错误,数据对象中的嵌套对象出错

转载 作者:行者123 更新时间:2023-11-29 16:52:37 44 4
gpt4 key购买 nike

根据 react-native-fcm 包,您可以在 FCM 消息传递负载的数据对象中包含自定义嵌套对象。

according to this post by the package author

像这样:

var payload = {
data: {
custom_notification: {
title: 'title',
body: 'body',
priority: 'high',
id: 'id',
group: 'group'
}
}
};

这是为了在所有应用程序状态下接收提示通知,如果您只执行通知负载或数据负载,则不会发生这种情况。

当我在我的云函数中实现它时,出现以下错误:

Error: Messaging payload contains an invalid value for the "data.custom_notification" property. Values must be strings.

所以我不知道其他人如何成功使用它?

我想知道我的环境是否存在问题,或者 firebase 支持人员(在文档中)给我的以下测试负载出错了:

var payload = {
"to":"FCM_TOKEN",
"data": {
"type":"MEASURE_CHANGE",
"body": "test body",
"title": "test title",
"color":"#00ACD4",
"priority":"high",
"id": "id",
"show_in_foreground": true
}
};

我收到以下错误:

Error sending message stringify: {"code":"messaging/invalid-payload","message":"Messaging payload contains an invalid \"to\" property. Valid properties are \"data\" and \"notification\"."}

在这方面已经好几天了,希望我能在这方面得到一些帮助。

提前致谢!

最佳答案

所以我刚刚意识到(经过几天的搜索)包 react-native-fcm 使用的发送方法不同于 admin.messaging().sendToDevice(token, payload, options) .我已经使用它一段时间了,但没有意识到它实际上并不是打算与这个库一起使用的,或者至少在这种情况下。主要是因为使用 admin.messaging() 一切都运行良好,直到我想要在所有应用程序状态下提醒通知。

另一种方法是这样

  sendData(token) {
let body = {
"to": token,
"data":{
"title": "Simple FCM Client",
"body": "This is a notification with only DATA.",
"sound": "default",
"click_action": "fcm.ACTION.HELLO",
"remote": true
},
"priority": "normal"
}

this._send(JSON.stringify(body), "data");
}

_send(body, type) {
let headers = new Headers({
"Content-Type": "application/json",
"Content-Length": parseInt(body.length),
"Authorization": "key=" + FirebaseConstants.KEY
});

fetch(API_URL, { method: "POST", headers, body })
.then(response => console.log("Send " + type + " response", response))
.catch(error => console.log("Error sending " + type, error));
}

您可以使用此方法在数据对象中使用嵌套对象。不幸的是,文档对此不是很清楚,直到现在我才意识到有一个例子。当然这可能只是我。

关于javascript - FCM 消息负载错误,数据对象中的嵌套对象出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45902343/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com