当应用程序在swift 3中处于前台时,隐藏特定viewcontroller上的远程通知

cha*_*nnu 4 push-notification ios swift3 swift4

我在我的聊天应用程序中使用firebase通知,我将它发送到我的设备.我的问题是如何在特定的视图控制器上隐藏远程通知.当应用程序在前台我正在使用

UNUserNotificationCenter将提供委托方法

显示横幅,但当我在chatViewController时,我不想显示通知横幅,声音,警报,如我想要的应用程序功能..请为我的英语

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    let userInfo:[AnyHashable:Any] =  notification.request.content.userInfo
    print("\(userInfo)")

    completionHandler([.alert, .badge, .sound])
}
Run Code Online (Sandbox Code Playgroud)

Ily*_*bet 6

您可以使用以下方法找到当前视图控制器:

extension UIViewController {
    var topViewController: UIViewController? {
        return self.topViewController(currentViewController: self)
    }

    private func topViewController(currentViewController: UIViewController) -> UIViewController {
        if let tabBarController = currentViewController as? UITabBarController,
            let selectedViewController = tabBarController.selectedViewController {
            return self.topViewController(currentViewController: selectedViewController)
        } else if let navigationController = currentViewController as? UINavigationController,
            let visibleViewController = navigationController.visibleViewController {
            return self.topViewController(currentViewController: visibleViewController)
       } else if let presentedViewController = currentViewController.presentedViewController {
            return self.topViewController(currentViewController: presentedViewController)
       } else {
            return currentViewController
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

而且,func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)添加此代码:

if self.window?.rootViewController?.topViewController is ChatViewController {
    completionHandler([])
} else {
    completionHandler([.alert, .badge, .sound])
}
Run Code Online (Sandbox Code Playgroud)