我在我的聊天应用程序中使用 firebase 通知,我正在将它发送到我的设备。我的问题是如何在特定的 View Controller 中隐藏远程通知。当应用程序位于我正在使用的前台时
UNUserNotificationCenter, will present delegate method
显示横幅,但是当我在 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])
}
最佳答案
您可以使用以下方法找到当前 View Controller :
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
}
}
}
然后,在 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])
}
关于ios - 当应用程序在 swift 3 中处于前台时,隐藏特定 View Controller 的远程通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48660314/