2

当应用程序处于终止状态并且我收到推送通知时。如果我查看我希望应用程序正常加载,但它应该显示一个我已经添加了关闭按钮的viewController上方,rootViewController并且每当我点击此按钮时,我只想关闭它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. 我希望应用程序像它一样正常运行,就像当用户来自通知时在启动时在根视图控制器上显示一个视图控制器一样。

更新
这里是我的故事板的截图: 在此处输入图像描述

4

2 回答 2

3

因此,您需要先使标签栏控制器无法满足:

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;
        }
    }
}

希望能帮助到你

于 2017-10-25T08:50:04.660 回答
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()
        }
    }
于 2017-10-25T06:46:17.877 回答