禁用特定 VC 的推送通知

时间:2021-04-06 08:44:47

标签: ios swift push-notification

我正在处理聊天屏幕并接收聊天消息的推送通知,但是我想在该特定用户的聊天屏幕打开时禁用推送,我已经解决了一些问题,但没有帮助

之前的代码

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}

我试过像这样改变

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo        
    let keyWindow = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
    
    if var topController = keyWindow?.rootViewController {
        while let presentedViewController = topController.presentedViewController {
            topController = presentedViewController
        }
        
        if let vc = (topController as? UINavigationController)?.viewControllers.last as? MessagesViewController {
            if vc.conversation.id == (userInfo["gcm.notification.channelID"] as? String) {
                completionHandler([])
                return
            }
            else {
                completionHandler([.alert, .badge, .sound])
            }
        }
        else {
            completionHandler([.alert, .badge, .sound])
        }
    }
    else {
        completionHandler([.alert, .badge, .sound])
    }

}

每当通知代码执行 completionHandler([]) 代码行但它并没有阻止推送可见

1 个答案:

答案 0 :(得分:0)

这是那些陷入相同情况的人的答案,我不确定发生了什么,但这对我有用

代替空数组completionHandler([]) 不写这行就好了

相关问题