【发布时间】:2020-12-12 03:13:52
【问题描述】:
我需要使用 Firebase Cloud Messaging 在我的 iOS 应用中进行静默通知/后台通知,这样即使用户没有点击推送通知,我也可以在应用中进行更新。
苹果关于后台通知的文档在here,据说
要发送后台通知,请使用以下命令创建远程通知 一个 aps 字典,仅包含有效负载中的内容可用键
来自该文档的后台通知的示例负载如下所示:
{
"aps" : {
"content-available" : 1
},
"acme1" : "bar",
"acme2" : 42
}
所以我在使用云功能节点 JS 发送 FCM 时创建了自己的有效负载。我的代码是这样的
const userToken = device.token
const payload = {
data: {
notificationID : notificationID,
creatorID : moderatorID,
creatorName: creatorName,
title : title,
body : body,
createdAt : now,
imagePath : imagePath,
type: type
},
apps : {
"content-available" : 1 // to force background data update in iOS
}
}
await admin.messaging().sendToDevice(userToken,payload)
我尝试发送但出现错误:
'消息负载包含无效的“应用程序”属性。有效的 属性是“数据”和“通知”。
所以添加“apps”属性是不允许的,但是iOS文档说我需要在payload中添加“content-available”。
看了here的其他回答,据说payload应该这样写
{
"to" : "[token]",
"content_available": true,
"priority": "high",
"data" : {
"key1" : "abc",
"key2" : abc
}
但我很困惑如何编写可以在 iOS 中触发后台通知的有效负载 FCM
【问题讨论】:
标签: ios node.js google-cloud-functions firebase-cloud-messaging