根据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\"."}
已经好几天了,所以希望我能得到一些帮助。
提前致谢!
答案 0 :(得分:2)
所以我刚刚意识到(经过几天的搜索)包反应本机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));
}
您可以使用此方法在数据对象中使用嵌套对象。遗憾的是,文档并不是很清楚,直到现在我还没有意识到有一个例子。当然这可能就是我。
答案 1 :(得分:1)
当使用data
消息有效负载时,它声明使用String的键值对,因此你可以做的是将custom_notification
的值作为JSON字符串,将它们封装在{ {1}}。
对于提供的样本有效负载,您实际使用" "
参数中的FCM_TOKEN
吗?您应该用实际令牌替换它。