Firebase云消息传递 - iOS徽章



我希望我的云功能向特定主题发送推送通知,这很好。但是,当我添加"徽章"字段时,会发生以下错误:

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;
})

问题是徽章是iOS字段,它应该在另一个地方,就像阿里所说的那样,它在" APS"对象中。但是他没有说的是AP应该在哪里。

例如,这应该是您的消息

{
  "message":{
     "topic": "Channel123",
      "notification": {
          "title": "Test-Title",
          "body": "Test-Body",
          "badge": "1"
      },
     "data": {
         "test": "test",
         "notId": "123"
     },
     "apns": {
       "payload": {
         "aps": {
           "badge": 5
         }
       }
     }
   }
 }

我从这里举了一个例子

尝试添加包含 badge属性的aps节点

const payload = {
    ...
    "aps":{  
      "alert":"test alert",
      "badge":5,
      "sound":"default"
   }
}

以下有效载荷在我的Android和iOS上都适用于我:

payload = {
      notification: {
        "title": "Message Title",
        "body": "Message Body",
        "click_action": ".MainActivity",
        "badge": "1",
      },
      data: {
        "my_message_type": "foo",
        "my_id": "bar",
      },
    };
const payload = {
            token: "FCMToken",
            notification: {
              title: "title",
              body: "body",
            },
            apns: {
              payload: {
                aps: {
                  badge: 5,
                  sound: "default",
                },
              },
            },
          };

相关内容

  • 没有找到相关文章

最新更新