Firebase云消息传递(FCM)- iOS徽章

5

我希望我的云函数能够向特定主题发送推送通知,这一点已经很好地实现了。但是,当我添加“badge”字段时,就会出现以下错误:

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;
})
4个回答

14
问题在于徽章是iOS领域的,应该放在另一个地方,正如阿里所说,它应该在“aps”对象中。但他没有说“aps”应该在哪里。
例如,这应该是您的消息。
{
  "message":{
     "topic": "Channel123",
      "notification": {
          "title": "Test-Title",
          "body": "Test-Body",
          "badge": "1"
      },
     "data": {
         "test": "test",
         "notId": "123"
     },
     "apns": {
       "payload": {
         "aps": {
           "badge": 5
         }
       }
     }
   }
 }

我从这里选了一个例子。


1
那你现在为什么有两个徽章字段? - Zorayr
通知中嵌套的徽章会引发错误。FirebaseMessagingError:接收到无效的JSON有效负载。在'message.notification'中未知名称“badge”。 - Binh Ho

2
尝试在负载中添加一个包含 badge 属性的 aps 节点。"Original Answer" 可以翻译成 "最初的回答"。
const payload = {

    ...

    "aps":{  
      "alert":"test alert",
      "badge":5,
      "sound":"default"
   }
}

1
以下负载在Android和iOS上都适用:
payload = {
      notification: {
        "title": "Message Title",
        "body": "Message Body",
        "click_action": ".MainActivity",
        "badge": "1",
      },
      data: {
        "my_message_type": "foo",
        "my_id": "bar",
      },
    };

这对我有效,而其他方法则没有。 - Praharsh Bhatt

1
const payload = {
            token: "FCMToken",
            notification: {
              title: "title",
              body: "body",
            },
            apns: {
              payload: {
                aps: {
                  badge: 5,
                  sound: "default",
                },
              },
            },
          };

谢谢,这是唯一在云函数中发送到iOS工作的方法。 - Kobi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接