在Firebase的云功能中设置优先级Firebase消息传递

Mar*_*ode 3 push-notification ios firebase google-cloud-functions firebase-cloud-messaging

我正在使用Firebase Messaging向我的iPhone应用程序的用户发送通知.为了不在客户端公开应用程序的消息服务器密钥,我正在使用Cloud Function for Firebase将通知发送到特定主题.在此之前,我在应用程序的客户端进行了此操作,并且能够通过制作此格式的JSON来设置消息的优先级:

// Swift code in iPhone app
let body: [String: Any] = ["to": "/topics/\(currentPet)",
                            "priority" : "high",
                            "notification" : [
                                "body" : "\(events[eventType]) for \(petsName.localizedCapitalized)",
                                "title" : "\(myName.localizedCapitalized) just logged an event",
                                "data" : ["personSent": myId]
                              ]
                           ]
Run Code Online (Sandbox Code Playgroud)

现在在我的云功能中,我正在尝试制作相同通用格式的有效负载,但仍然遇到错误:

Messaging payload contains an invalid "priority" property. Valid properties are "data" and "notification".

这是我的有效负载格式和发送:

const payload = {
      'notification': {
        'title': `${toTitleCase(name)} just logged an event`,
        'body': `${events[eventType]} for ${toTitleCase(petName)}`,
        'sound': 'default',
        'data': userSent 
      },
      'priority': 'high'
    };
admin.messaging().sendToTopic(pet_Id, payload);
Run Code Online (Sandbox Code Playgroud)

有人知道如何设定优先权吗?我应该手动做HTTP POST而不是使用admin.messaging().sendToTopic()吗?

Fra*_*len 15

使用Admin SDK发送邮件Firebase Cloud Messaging文档中:

// Set the message as high priority and have it expire after 24 hours.
var options = {
  priority: "high",
  timeToLive: 60 * 60 * 24
};

// Send a message to the device corresponding to the provided
// registration token with the provided options.
admin.messaging().sendToDevice(registrationToken, payload, options)
  .then(function(response) {
    console.log("Successfully sent message:", response);
  })
  .catch(function(error) {
    console.log("Error sending message:", error);
  });
Run Code Online (Sandbox Code Playgroud)

不同之处在于优先级(和示例中的ttl)作为单独的options参数而不是在有效负载中传递.