在Swift 3中收到推送通知时如何打开特定的视图控制器

PPS*_*ein 3 ios swift3

当应用程序未完全打开时,当我点击推送通知警报时,我遇到了特定的视图控制器无法移动的问题。

这是我的代码:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    /*

    fetch and add push notification data

     */
    goAnotherVC()
}

func goAnotherVC() {
    if (application.applicationState == UIApplicationState.active) {
        /* active stage is working */ 
    } else if (application.applicationState == UIApplicationState.inactive || application.applicationState == UIApplicationState.background) {
        if (type == "1" || type == "2") {
            let storyboard: UIStoryboard = UIStoryboard(name: "MyAppointments", bundle: nil)
            let apptVC = storyboard.instantiateViewController(withIdentifier: "NotificationDetailViewController") as! NotificationDetailViewController
            let navigationController = UINavigationController.init(rootViewController: apptVC)
            self.window?.rootViewController = navigationController
            self.window?.makeKeyAndVisible()
        } else if (type == "3") {
            let storyboard: UIStoryboard = UIStoryboard(name: "MyAppointments", bundle: nil)
            let apptVC = storyboard.instantiateViewController(withIdentifier: "NotificationDetailViewController") as! NotificationDetailViewController
            let navigationController = UINavigationController.init(rootViewController: apptVC)
            self.window?.rootViewController = navigationController
            self.window?.makeKeyAndVisible()
        } else if (type == "4") {
            let storyboard: UIStoryboard = UIStoryboard(name: "Enquiry", bundle: nil)
            let enqVC = storyboard.instantiateViewController(withIdentifier: "EnquiryDetailViewController") as! EnquiryDetailViewController
            let navigationController = UINavigationController.init(rootViewController: enqVC)
            self.window?.rootViewController = navigationController
            self.window?.makeKeyAndVisible()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当应用程序处于活动状态时,我会收到通知并点击以移动特定的VC。请帮助我我所缺少的。

Ess*_*med 15

斯威夫特 5

只需实现以下函数,当用户单击通知时将调用该函数。

在 AppDelegate 中:

// This method is called when user clicked on the notification
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
    // Do whatever you want when the user tapped on a notification
    // If you are waiting for specific data from the notification 
    // (e.g., key: "target" and associated with "value"), 
    // you can capture it as follows then do the navigation:

    // You may print `userInfo` dictionary, to see all data received with the notification.
    let userInfo = response.notification.request.content.userInfo
    if let targetValue = userInfo["target"] as? String, targetValue == "value"
    {
        coordinateToSomeVC()
    }
    
    completionHandler()
}

private func coordinateToSomeVC()
{
    guard let window = UIApplication.shared.keyWindow else { return }

    let storyboard = UIStoryboard(name: "YourStoryboard", bundle: nil) 
    let yourVC = storyboard.instantiateViewController(identifier: "yourVCIdentifier")
    
    let navController = UINavigationController(rootViewController: yourVC)
    navController.modalPresentationStyle = .fullScreen

    // you can assign your vc directly or push it in navigation stack as follows:
    window.rootViewController = navController
    window.makeKeyAndVisible()
}
Run Code Online (Sandbox Code Playgroud)

笔记:

如果你根据通知导航到一个特定的控制器,你应该关心你将如何从这个控制器导航回来,因为你的堆栈中现在没有控制器。您必须实例化您将返回的控制器。在我的情况下,当用户点击返回时,我实例化家庭控制器并再次将其设为应用根目录,因为应用程序通常会启动。


Joe*_*Joe 6

当应用处于关闭状态时,您应在“ func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool{”中检查启动选项, 然后调用您的API。

例:

if let option = launchOptions {
        let info = option[UIApplicationLaunchOptionsKey.remoteNotification]
        if (info != nil) {
             goAnotherVC()
    }}
Run Code Online (Sandbox Code Playgroud)