AppDelegate - How to push my viewcontroller instead of present, the reason is that I want to have the tab bar on my next viewcontroller with navigation bar too. I tryed this: "controller.navigationController?.pushViewController(controller, animated: true)" but It gives me fatal error, found nil while unwraping optional value. Any idea?
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
if Defaults.hasKey(.logged), let logged = Defaults[.logged], logged == true {
let userInfo = notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
let topWindow = UIWindow(frame: UIScreen.main.bounds)
topWindow.rootViewController = UIViewController()
topWindow.windowLevel = UIWindowLevelAlert + 1
let alert = UIAlertController(title: userInfo["title"] as? String, message: userInfo["body"] as? String, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("Show Product", comment: "confirm"), style: .default, handler: {(_ action: UIAlertAction) -> Void in
if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ProductDetailsViewController") as? ProductDetailsViewController {
controller.productFromNotificationByCode(code: userInfo["product_code"] as! String, productID: userInfo["product_id"] as! String)
if let window = self.window, let rootViewController = window.rootViewController {
var currentController = rootViewController
while let presentedController = currentController.presentedViewController {
currentController = presentedController
}
currentController.present(controller, animated: true, completion: nil)
}
}
}))
alert.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: "cancel"), style: .destructive, handler: {(_ action: UIAlertAction) -> Void in
topWindow.isHidden = true
}))
topWindow.makeKeyAndVisible()
topWindow.rootViewController?.present(alert, animated: true, completion: { _ in })
}
}
completionHandler([])
}