【问题标题】:Firebase Cloud Messaging - iOS BadgesFirebase 云消息传递 - iOS 徽章
【发布时间】:2019-06-29 11:03:51
【问题描述】:

我希望我的 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;
})

【问题讨论】:

    标签: ios firebase firebase-cloud-messaging


    【解决方案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】:

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

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

    【讨论】:

      【解决方案3】:

      以下负载在 Android 和 iOS 上都适用于我:

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

      【讨论】:

        猜你喜欢
        • 2017-06-30
        • 2019-07-12
        • 2020-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-31
        相关资源
        最近更新 更多