I am using firebase notifications in my chat app and I am getting it to my device.My question is how to hide remote notification at the particular view controller.when the app is in the foreground I am using
UNUserNotificationCenter, will present delegate method
to display the banner but when I am in chatViewController I don't want to show the notification banner, sound, alert like whats app feature I want..Sorry for my English
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])
}
You can find current view controller using this methods:
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
}
}
}
And than, in func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
add this code:
if self.window?.rootViewController?.topViewController is ChatViewController {
completionHandler([])
} else {
completionHandler([.alert, .badge, .sound])
}
Specify UNNotificationPresentationOptionNone to silence the notification completely.
UNNotificationPresentationOptionNone
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With