2017-07-13 97 views
1

所以我在我的应用程序中有这个“静音聊天室”功能。 如果聊天室被静音,从该聊天室的通知数据中包含“静音= 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) 

被调用时,它会立即显示通知提醒。

所以我怎么想静音聊天室(不显示警报和静音),当应用程序在后台...

非常感谢!

+0

你不应该设置在推警报或音键,但在应对无声推 – Paulw11

回答

2

达到此目的的最佳方法。

如果您chatroommute那么你server已经知道,因为服务器是与标志mute : 1

所以发送push notification,后端必须检查,如果mutetrue然后不加alert & sound关键字push payload。即使应用程序在后台/前台,这将工作。

样品有效载荷:

{ 
    "aps" : { 
     "content-available" : 1, 
     "badge" : 8 
    } 
    //Custom data here. 
} 
+0

嘿嘿,谢谢,我会要求我的后端,而产生一个本地通知尝试删除这两个关键字 – Ian

+0

不客气:) – Subramanian

0

你必须实现一个接受布尔值的应用程序状态的Web服务。 当您的应用程序在后台时,您可以使用布尔值true来调用此Web服务。 ,对于服务器端来说,当这个布尔值为真时,则不需要从后端发送通知。