简体   繁体   English

Firebase 云消息传递 - iOS 徽章

[英]Firebase Cloud Messaging - iOS Badges

I want my Cloud Function to send a push notification to a specific topic, which works just fine.我希望我的 Cloud Function 向特定主题发送推送通知,这很好用。 However, when I'm adding the "badge" field, the following error occurs:但是,当我添加“徽章”字段时,会出现以下错误:

Unknown name "badge" at 'message.notification': Cannot find field.

What am I supposed to do if I want to specify a badge number for iOS devices?如果我想为 iOS 设备指定徽章编号,我该怎么做? The code I have looks like follows:我的代码如下所示:

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

The problem is that badge is iOS field and it should be in another place as Ali said it is in "aps" object.问题是徽章是iOS领域,它应该在另一个地方,因为阿里说它在“aps”对象中。 But what he didn't say is where aps should be.但他没有说的是aps应该在哪里。

for example this should be your message例如,这应该是您的信息

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

I took the example from here我从这里拿了例子

try to add aps node containing badge property with your payload尝试在您的有效负载中添加包含badge属性的aps节点

const payload = {

    ...

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

The following payload worked for me on both Android and iOS:以下有效负载适用于 Android 和 iOS:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM