所以我的应用程序中有这个“静音聊天室”功能。如果聊天室是静音的,则来自该聊天室的通知数据包含“静音= 1”,如下所示,
AnyHashable("aps"): {
"content-available" = 1;
alert = "Android @qwer: Gggggggg";
badge = 8;
sound = default;},
AnyHashable("custom"): {
a = {
comment = 423;
event = 133;
mute = 1;
};
}当我收到通知时,我会根据静音布尔值来决定天气是否显示警报/声音。
当应用程序处于前台时,当我收到通知时,它可以正常工作。
就像:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) { let userInfo = notification.request.content.userInfo
guard let data = userInfo["custom"] as? [String: Any] else { return }
guard let additional = data["a"] as? [String: Any] else {
print ("no addi")
return }
let mute = additional["mute"] as? NSNumber
if mute == 1 {
completionHandler([ .badge])
} else if mute == 0 {
completionHandler([.alert, .badge, .sound])
}}但是当应用程序处于后台时,事情就变得棘手了,
当我在后台收到通知时,
这种方法
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)将立即显示通知警报。
那么,当应用程序处于后台时,我该如何静音聊天室(不显示警报和静音).
非常感谢!
发布于 2017-07-13 09:11:13
实现这一目标的最佳方法。
如果您的chatroom是mute,那么您的server已经知道了,因为服务器正在发送带有标志mute : 1的push notification。
因此,后端必须检查,如果mute值是true,那么不要在push payload中添加alert & sound关键字。即使是应用程序在后台/前台,这也是可行的。
示例有效载荷:
{
"aps" : {
"content-available" : 1,
"badge" : 8
}
//Custom data here.
}发布于 2017-07-13 09:05:03
您必须实现一个web服务,它接受应用程序状态的布尔值。当您的应用程序处于后台时,您可以使用布尔值true调用此web服务。对于服务器端,当此布尔值为真时,则不需要从后端发送通知。
https://stackoverflow.com/questions/45075782
复制相似问题