Ios 如何使用Firebase云消息发送静默通知/后台通知?
我需要使用Firebase Cloud Messaging在iOS应用程序中发出无声通知/后台通知,这样即使用户没有点击推送通知,我也可以在应用程序中进行更新 苹果关于后台通知的文档在,据说 要发送后台通知,请使用创建远程通知 一种aps字典,仅包括有效负载中的内容可用密钥 该文档中后台通知的示例有效负载如下所示:Ios 如何使用Firebase云消息发送静默通知/后台通知?,ios,node.js,google-cloud-functions,firebase-cloud-messaging,Ios,Node.js,Google Cloud Functions,Firebase Cloud Messaging,我需要使用Firebase Cloud Messaging在iOS应用程序中发出无声通知/后台通知,这样即使用户没有点击推送通知,我也可以在应用程序中进行更新 苹果关于后台通知的文档在,据说 要发送后台通知,请使用创建远程通知 一种aps字典,仅包括有效负载中的内容可用密钥 该文档中后台通知的示例有效负载如下所示: { "aps" : { "content-available" : 1 }, "acme1"
{
"aps" : {
"content-available" : 1
},
"acme1" : "bar",
"acme2" : 42
}
const userToken = [YOUR_TOKEN];
const payload = {
"data": {
"story_id": "story_12345"
},
"notification": {
"title": "Breaking News"
}
};
const options = {
"contentAvailable": true
};
admin.messaging().sendToDevice(userToken, payload, options).then(function(response) {
console.log('Successfully sent message:', response);
}).catch(function(error) {
console.log('Error sending message:', error);
});
因此,在使用云功能节点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)
{
"to" : "[token]",
"content_available": true,
"priority": "high",
"data" : {
"key1" : "abc",
"key2" : abc
}
我尝试发送,但出现错误:
'消息传递负载包含无效的“apps”属性。有效的
属性是“数据”和“通知”
所以不允许添加“apps”属性,但iOS文档说我需要在有效负载中添加“content available”
我在中读到了另一个答案,据说有效载荷应该这样写
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)
{
"to" : "[token]",
"content_available": true,
"priority": "high",
"data" : {
"key1" : "abc",
"key2" : abc
}
但是我不知道如何编写能够在iOS中触发后台通知的有效负载FCM。根据您收到的错误消息,您应该删除apps属性,因为数据和通知属性被认为是有效的 现在,关于您在别处找到的有效负载,这是指用于通过Firebase云消息传递将消息从应用程序服务器传递到客户端应用程序的HTTP语法。您可以参考以了解有关新HTTP v1 API的更多信息 要回答您的问题,当您在Node.js运行时使用云函数使用该方法发送通知时,您需要在函数的options参数中传递 contentAvailable选项执行以下操作:当发送通知或消息并将其设置为true时,将唤醒非活动客户端应用程序,并通过APNs将消息作为静默通知发送,而不是通过FCM连接服务器发送 因此,您的云功能可能如下所示:
{
"aps" : {
"content-available" : 1
},
"acme1" : "bar",
"acme2" : 42
}
const userToken = [YOUR_TOKEN];
const payload = {
"data": {
"story_id": "story_12345"
},
"notification": {
"title": "Breaking News"
}
};
const options = {
"contentAvailable": true
};
admin.messaging().sendToDevice(userToken, payload, options).then(function(response) {
console.log('Successfully sent message:', response);
}).catch(function(error) {
console.log('Error sending message:', error);
});