我希望我的Cloud Function将推送通知发送到特定主题,这很好用。但是,当我添加“徽章”字段时,会发生以下错误:
Unknown name "badge" at 'message.notification': Cannot find field.
如果我想为iOS设备指定徽章编号,该怎么办?我的代码如下所示:
exports.onAddLog = functions.database.ref("/NodesLog/{id}").onCreate((change, context) => {
const payload = {
"topic": "Channel123",
"notification": {
"title": "Test-Title",
"body": "Test-Body",
"badge": "1"
},
"data": {
"test": "test",
"notId": "123"
}
}
const promise : Promise<any> = admin.messaging().send(payload)
.catch(error => console.error("Error on send: " + error))
.then(sendSuccess => console.log("Notification send "))
.catch(() => console.error("What happened?"))
return promise;
})
答案 0 :(得分:1)
尝试在您的有效负载中添加包含aps
属性的badge
节点
const payload = {
...
"aps":{
"alert":"test alert",
"badge":5,
"sound":"default"
}
}
答案 1 :(得分:1)
问题是徽章是iOS字段,并且应该放在另一个地方,就像Ali所说的那样在“ aps”对象中。但是他没有说的是aps应该在哪里。
例如,这应该是您的信息
{
"message":{
"topic": "Channel123",
"notification": {
"title": "Test-Title",
"body": "Test-Body",
"badge": "1"
},
"data": {
"test": "test",
"notId": "123"
},
"apns": {
"payload": {
"aps": {
"badge": 5
}
}
}
}
}
我以here
为例答案 2 :(得分:0)
以下有效负载在Android和iOS上均对我有效:
payload = {
notification: {
"title": "Message Title",
"body": "Message Body",
"click_action": ".MainActivity",
"badge": "1",
},
data: {
"my_message_type": "foo",
"my_id": "bar",
},
};