Firebase云消息传递- iOS徽章

lhcgjxsq  于 2023-02-10  发布在  iOS
关注(0)|答案(4)|浏览(158)

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

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

hfwmuf9z1#

问题是,徽章是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
         }
       }
     }
   }
 }

我从这里开始举例

f1tvaqid

f1tvaqid2#

尝试添加包含badge属性的aps节点和有效负载

const payload = {

    ...

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

wfveoks03#

下面的有效载荷对我来说在Android和iOS上都有效:

payload = {
      notification: {
        "title": "Message Title",
        "body": "Message Body",
        "click_action": ".MainActivity",
        "badge": "1",
      },
      data: {
        "my_message_type": "foo",
        "my_id": "bar",
      },
    };
q43xntqr

q43xntqr4#

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

相关问题