有没有办法使用google的firebase发送静音APNS? 似乎如果应用程序在后台,它将始终向用户显示通知。
感谢?
答案 0 :(得分:31)
您可以使用FCM服务器API发送静默APNS消息 https://firebase.google.com/docs/cloud-messaging/http-server-ref
特别需要使用:
此参数指定邮件的自定义键值对 有效载荷。
例如,使用数据:{“score”:“3x1”}:
在iOS上,如果邮件是通过APNS发送的,则表示自定义数据 领域。如果它是通过FCM连接服务器发送的,那就是 在AppDelegate中表示为键值字典 应用:didReceiveRemoteNotification:
密钥不应该是保留字(“from”或任何单词起始 用“google”或“gcm”)。不要使用此中定义的任何单词 table(例如collapse_key)。
建议使用字符串类型中的值。您必须转换值 对象或其他非字符串数据类型(例如,整数或布尔值) 串
在iOS上,使用此字段表示APNS中可用的内容 有效载荷。发送通知或消息并将其设置为时 是的,一个非活动的客户端应用程序被唤醒。在Android上,数据消息唤醒 默认情况下应用程序。在Chrome上,目前不支持。
完整文档: https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http-messages-json
答案 1 :(得分:27)
对于使用FCM服务器的真正静默通知(前景和背景),请使用以下字段:
"to" : "[token]",
"content_available": true,
"priority": "high",
"data" : {
"key1" : "abc",
"key2" : 123
}
注意:请确保您使用" content_available" 不"内容可用"与FCM。它已转换为APNS,否则将无法正确接收。这种差异使我绊倒了一段时间。
答案 2 :(得分:8)
我在博客上详细解释了这个主题。 http://blog.boxstory.com/2017/01/how-to-send-silent-push-notification-in.html
**关键点是:" content_available:true"
{
"to" : "<device>",
"priority": "normal",
"content_available": true, <-- this key is converted to 'content-available:1'
"notification" : {
"body" : "noti body",
"title" : "noti title",
"link": "noti link "
}
}
注意:如果发送了上面的示例JSON,则通知将是 对用户可见。使用下面如果您不希望用户看到 推送通知。
{
"to": "<device>",
"priority": "normal",
"content_available": true <-- this key is converted to 'content-available:1'
}
答案 3 :(得分:0)
对于不使用其他答案中所示的Legacy HTTP
而使用最新的v1 HTTP protocol
的人,我终于找到了发送静默通知的正确方法。
使用firebase-admin
的NodeJS示例:
const message = {
apns: {
payload: {
aps: {
"content-available": 1,
alert: ""
}
}
}
};
admin
.messaging()
.send(message)
.then(response => {
// Response is a message ID string.
console.log("Successfully sent message:", response);
})
.catch(error => {
console.log("Error sending message:", error);
});
说明:
apns
中的有效负载似乎没有被v1 HTTP protocol
中的Firebase转换,因此您需要原始的"content-available": 1
。alert: ""
也很有必要。如果您曾经尝试使用Pusher
之类的方式发送静默通知,则会发现只有content-available
无法触发它。相反,添加其他字段,例如sound
或alert
可以使其工作。参见Silent Push Notification in iOS 7 does not work。由于Firebase禁止空声音,因此我们可以为此使用空警报。答案 4 :(得分:0)
其他解决方案对我不起作用。 我想要一种可以将数据消息发送到iOS和Android的解决方案。
通过测试,我发现,当我的iOS应用程序在后台运行时,可靠地发送数据消息的唯一方法是包括一个空的通知有效负载。
此外,如其他答案所述,您需要包括content_available
和priority
。
要使用curl命令进行测试,您需要FCM server key和应用中的FCM令牌。
仅适用于iOS的示例curl命令。 (可靠的数据消息,没有可见的通知)
curl -X POST \
https://fcm.googleapis.com/fcm/send \
-H 'authorization: key=server_key_here' \
-H 'content-type: application/json' \
-d '{
"to": "fcm_token_here",
"priority": "high",
"content_available": true,
"notification": {
"empty": "body"
},
"data": {
"key1": "this_is_a_test",
"key2": "abc",
"key3": "123456",
}
}'
用您自己的替换上面的server_key_here
和fcm_token_here
。
当应用在后台运行且不应显示UI消息时,应调用AppDelegate
类中的以下方法。
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
//get data from userInfo
completionHandler(UIBackgroundFetchResult.newData)
}
这是使用云功能和打字稿发送给主题的方法。
const payload = {
notification: {
empty: "message"
},
data: {
key1: "some_value",
key2: "another_value",
key3: "one_more"
}
}
const options = {
priority: "high",
contentAvailable: true //wakes up iOS
}
return admin.messaging().sendToTopic("my_topic", payload, options)
.then(response => {
console.log(`Log some stuff`)
})
.catch(error => {
console.log(error);
});
以上内容似乎始终适用于iOS,有时甚至适用于Android。 我得出的结论是,我的后端将需要确定平台,然后才能发送推送通知以达到最有效的效果。