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



我正在使用 Firebase 消息功能向我的 iPhone 应用的用户发送通知。为了不在客户端暴露应用程序的消息传递服务器密钥,我正在使用适用于Firebase的Cloud Function将通知发送到特定主题。在此之前,我从应用程序的客户端执行此操作,并且能够通过制作以下格式的 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]
]
]

现在在我的云函数中,我正在尝试制作相同常规格式的有效负载,但不断遇到错误:

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

有谁知道我将如何完成优先级设置?我应该手动执行HTTP POST而不是使用admin.messaging().sendToTopic()吗?

来自 Firebase 云消息文档,了解如何使用管理员 SDK 发送消息:

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

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

最新更新