当应用程序处于终止状态并且我收到推送通知时。如果我查看,我希望应用程序正常加载,但它应该在 rootViewController
上显示一个 viewController
,我为其添加了一个关闭按钮,并且每当我点击此按钮时我想简单地关闭这个viewController
。
在我从通知打开应用程序时调用的方法中,我已经这样做了:
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let tabBarController = self.window!.rootViewController as! UITabBarController
self.window?.rootViewController = tabBarController
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
apptVC = storyboard.instantiateViewController(withIdentifier: "NotificationVC") as! NotificationsViewController
window?.makeKeyAndVisible()
window?.rootViewController?.present(apptVC, animated: true, completion: nil)
completionHandler()
}
}
但不幸的是,当我这样做时,应用程序崩溃了。我已经使用 firebase
实现了推送通知。我希望应用程序能够像它一样正常运行,就像当用户收到通知时在启动时在 Root View Controller 上呈现一个 View Controller 一样。
最佳答案
所以你需要首先满足标签栏 Controller :
let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController
并使用您想要的 View Controller 来满足所有 UINavigationControllers
的需求:
let firstNavigationController = storyboard.instantiateViewiController(withIdentifier: "YourFirstNavigationController") as! UINavigationController
let NotificationVC = storyboard.instantiateViewController(withIdentifier: "NotificationVC") as! NotificationsViewController
并将它们全部设置为标签栏 Controller 的viewControllers
:
tabBarController.viewControllers = [firstNavigationController]
该 View Controller 应该存在哪个导航 Controller ?例如:
tabBarController.selectedViewController == firstNavigationController {
firstNavigationController.present(NotificationVC, animated: true, completion: nil)
}
这是最后一件事:
self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()
注意这只是推荐,因此我使用了一个UNaviationController
所以我创建了一个名为 present()
的函数。
func present() {
let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController
let firstNavigationController = storyboard.instantiateViewiController(withIdentifier: "YourFirstNavigationController") as! UINavigationController
let NotificationVC = storyboard.instantiateViewController(withIdentifier: "NotificationVC") as! NotificationsViewController
tabBarController.viewControllers = [firstNavigationController]
tabBarController.selectedViewController == firstNavigationController {
firstNavigationController.present(NotificationVC, animated: true, completion: nil)
}
self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()
}
}
但是即使我认为这没有用,它也没有用。每当点击通知时,我们都需要采取行动。我们应该检查一个 Action 标识符
,如下所示:
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
switch response.actionIdentifier {
case UNNotificationDefaultActionIdentifier:
self.present()
completionHandler()
default:
break;
}
}
}
希望对你有帮助
关于ios - 当点击通知时,将 viewController 呈现在 rootViewController 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46924878/