ios - Swift - 如何在收到推送通知时打开特定的 View Controller ?

标签 ios swift push-notification

当我在应用程序未完全打开阶段时点击推送通知警报时,我遇到了特定 View Controller 未移动的问题。

这是我的代码:

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()
        }
    }
}

当应用程序处于事件状态时,我可以获得通知并点击以移动特定的 VC。请帮助我我所缺少的。

最佳答案

swift 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()
}

注意事项:

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

关于ios - Swift - 如何在收到推送通知时打开特定的 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42940629/

相关文章:

ios - UIWebView 快速更改链接颜色

ios - UIView 表面自定义转换/动画(如 'a water drop effect' )

swift - 不能从 myCoustomClass downCast

ios - 如何最好地删除所有 UIViewcontroller 并转到单个 UIViewController?

ios - 在 iOS 应用程序中显示通知,而应用程序在前台/在 swift 3 中处于事件状态

ios - 如何在 iOS 应用程序中获取 DNS TXT 记录

json - 使用 JSONDecoder 和可编码协议(protocol)进行联网

android - 小米note3自动启动列表中的安卓应用如何移动,即使应用关闭也会收到推送

windows-phone-7 - 如何在适用于 Windows Phone 的 Microsoft 推送通知服务中进行身份验证?

ios - Xcode 6 中不同设备的不同 Storyboard