当应用程序处于终止状态并且我收到推送通知时。如果我查看我希望该应用正常加载,但它应该在viewController
上显示rootViewController
我为其添加了一个关闭按钮,每当我点按此按钮时,我只想忽略此{ {1}}。
在我从通知中打开应用程序时调用的方法中,我已完成此操作:
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()
}
}
实施了推送通知。我希望应用程序正常运行,就像在用户来自通知时在启动时在根视图控制器上显示视图控制器一样。
答案 0 :(得分:2)
因此,您首先需要对标签栏控制器进行内容:
let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController
使用您想要的视图控制器发送所有UINavigationControllers
:
let firstNavigationController = storyboard.instantiateViewiController(withIdentifier: "YourFirstNavigationController") as! UINavigationController
let NotificationVC = storyboard.instantiateViewController(withIdentifier: "NotificationVC") as! NotificationsViewController
并将它们全部设为标签栏控制器的viewControllers
:
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()
注意这只是推荐,因此我使用了一个UNavigationController
所以我创建了一个名为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()
}
}
但是即使我觉得这没用也没用。我们需要在点击通知时执行操作。我们应该像这样检查identifier
行动:
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
switch response.actionIdentifier {
case UNNotificationDefaultActionIdentifier:
self.present()
completionHandler()
default:
break;
}
}
}
希望有所帮助
答案 1 :(得分:1)
也许你的rootView控制器是零。尝试在呈现类之前设置一个rootview_Controller,即使你已经在appDelegate的完成发布中指定了一个rootviewController。
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
viewController = storyboard.instantiateViewControllerWithIdentifier("YourRootVC") as UIViewController
self.window?.rootViewController = viewController
window?.rootViewController?.present(yourPusHNotificationVC, animated: true, completion: nil)
completionHandler()
}
}