火力基地无声弹片通知



有没有办法使用谷歌的firebase发送静默APNS?似乎如果应用程序在后台,它总是会向用户显示通知。

谢谢?

您可以使用FCM服务器API发送静默式APNS消息https://firebase.google.com/docs/cloud-messaging/http-server-ref

特别需要使用:

  • 数据字段:

此参数指定消息的自定义键值对有效载荷。

例如,使用数据:{"分数":"3x1"}:

在iOS上,如果消息是通过APNS发送的,则表示自定义数据字段。如果通过FCM连接服务器发送表示为AppDelegate中的键值字典应用程序:didReceiveRemoteNotification:。

密钥不应该是保留字("from"或任何以用";谷歌;或";gcm")。不要使用本文中定义的任何单词表(例如collapse_key)。

建议使用字符串类型的值。您必须转换中的值对象或其他非字符串数据类型(例如,整数或布尔值)字符串

  • content_available字段:

在iOS上,使用此字段表示APNS中可用的内容有效载荷。发送通知或消息时,此设置为的确,一个不活动的客户端应用程序被唤醒了。在Android上,数据消息唤醒默认情况下为应用程序。在Chrome上,目前不支持。

完整文档:https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http消息json

对于使用FCM服务器的真正静默通知(前台和后台),请使用以下字段:

"to" : "[token]",
"content_available": true,
"priority": "high",
"data" : {
"key1" : "abc",
"key2" : 123
}

注意:请确保您在FCM中使用"content_available">而不是"content-available"。它已转换为APNS,否则将无法正常接收。这种差异使我有一段时间感到困惑。

我在博客中详细解释了这个主题。http://blog.boxstory.com/2017/01/how-to-send-silent-push-notification-in.html

**关键点是:"content_available:true">

这是示例JSON

{
"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'
}

对于那些不使用其他答案中显示的Legacy HTTP并使用最新v1 HTTP protocol的人,我终于找到了发送静默通知的正确方法。

使用firebase-admin:的NodeJS示例

const message = {
apns: {
payload: {
aps: {
"content-available": 1,
alert: ""
}
}
},
token: "[token here - note that you can also replace the token field with `topic` or `condition` depending on your targeting]"
};
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
  • CCD_ 7也是必要的。如果你尝试使用Pusher之类的东西发送静默通知,你会发现只有content-available无法触发它。相反,添加soundalert之类的额外字段可以使其工作。请参阅iOS 7中的静音推送通知不起作用。由于Firebase禁止空声音,我们可以使用空警报

其他解决方案对我不起作用。我想要一个同时向iOS和Android发送数据消息的解决方案。

通过测试,我发现当我的iOS应用程序在后台时,可靠地发送数据消息的唯一方法是包含一个空的通知负载。

此外,正如其他答案所提到的,您需要包括content_availablepriority

要使用curl命令进行测试,您需要FCM服务器密钥和应用程序中的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_herefcm_token_here替换为您自己的

当应用程序处于后台并且不应显示任何UI消息时,应调用AppDelegate类中的以下方法。

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
//get data from userInfo
completionHandler(UIBackgroundFetchResult.newData)
}

以下是使用云函数和typescript发送到主题的方法。

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。我得出的结论是,在发送推送通知之前,我的后端需要确定平台才能最有效。

我需要向主题发送计划通知。上面的一切对我来说都不太奏效,但我终于得到了应用程序代表application(application:didReceiveRemoteNotification:fetchCompletionHandler:)的一致调用。以下是我在index.js云函数文件中完成的操作的完整示例(请注意,您需要"apns-push-type": "background""apns-priority": "5"标头,以及aps对象中的"content-available": 1条目):

const admin = require("firebase-admin");
const functions = require("firebase-functions");
exports.sendBackgroundFetchNotification = functions.pubsub.schedule("every 1 hours").onRun((context) => {
const message = {
data: {},
apns: {
headers: {
"apns-push-type": "background",
"apns-priority": "5",
},
payload: {
aps: {
"content-available": 1,
"alert": {},
},
},
},
topic: "[Topic_Name_Here]",
};
return admin
.messaging()
.send(message)
.then(response => {
// Response is a message ID string.
console.log("Successfully sent message:", response);
return null;
})
.catch(error => {
console.log("Error sending message:", error);
return null;
});
});

如果你不想在部署后等待函数触发,只需进入谷歌云控制台功能部分(https://console.cloud.google.com/functions/list)并点击函数名称,然后点击";测试";最后点击";测试功能";。

值得注意的是,此代码使用FCM较新的HTTP v1协议,该协议允许您根据苹果的规范构建消息对象(下面有有用的链接)。

https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messageshttps://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apnshttps://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification

最新更新