gpt4 book ai didi

ios - 当点击通知时,在 rootViewController 上显示 viewController

转载 作者:行者123 更新时间:2023-11-28 10:49:18 25 4
gpt4 key购买 nike

当应用程序处于终止状态并且我收到推送通知时。如果我查看我希望应用程序正常加载,但它应该在 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 一样。

更新
这是我的 Storyboard的截图: enter image description here

最佳答案

所以你需要先满足标签栏 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]

哪个导航 Controller 应该出现在这个 View 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()

注意 这只是推荐,因此我使用了一个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()


}
}

但它没有用,即使我认为这是无用的。每当点击通知时,我们都需要一个 Action 。我们应该像这样检查一个 Action 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;
}
}
}

希望对你有帮助

关于ios - 当点击通知时,在 rootViewController 上显示 viewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46924878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com