在 Cloud Function for Firebase 中设置优先级 Firebase Messaging
Setting priority Firebase Messaging in Cloud Function for Firebase
我正在使用 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]
]
]
现在在我的云函数中,我正在尝试制作相同通用格式的有效载荷,但将 运行 保留在错误中:
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 Cloud Messaging documentation on sending messages with the Admin 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
参数传递,而不是在有效负载中传递。
我正在使用 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]
]
]
现在在我的云函数中,我正在尝试制作相同通用格式的有效载荷,但将 运行 保留在错误中:
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 Cloud Messaging documentation on sending messages with the Admin 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
参数传递,而不是在有效负载中传递。