【问题标题】:Swift - How to open specific view controller when push notification received?Swift - 收到推送通知时如何打开特定的视图控制器?
【发布时间】:2017-08-13 22:00:16
【问题描述】:

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

这是我的代码:

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。请帮助我我所缺少的。

【问题讨论】:

  • 你把上面的代码放在哪里了?在什么功能上?
  • func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 内,我已经改变了我的问题。

标签: ios swift push-notification


【解决方案1】:

Swift 5、iOS 13 -

从 iOS 13 开始,“窗口”在 SceneDelegate 中可用。但是 didReceiveNotification 方法仍然存在于 AppDelegate 中。

所以你必须先从 SceneDelegate

访问 window
let window = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window

现在您可以设置窗口的 rootViewController 属性

    window.rootViewController = viewControllerObject
    window.makeKeyAndVisible()

【讨论】:

【解决方案2】:

斯威夫特 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()
}

注意:

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

【讨论】:

  • 在哪里可以找到Helper.instance.goToControlllerWith!?
  • 这是一个自定义方法,使用实例化。
  • 主要问题是关于如何显示您在帮助类中处理的 vc
  • 你的意思是我应该清除其他功能以专注于用户问题?
  • @NegarMoshtaghi 我已将答案编辑为更加相关,此时请再次检查。
【解决方案3】:

当您的应用处于关闭状态时,您应该检查启动选项

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { }

并调用您的 API。

例子:

if let option = launchOptions {
  let info = option[UIApplicationLaunchOptionsKey.remoteNotification]
  if (info != nil) {
    goAnotherVC()
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 2016-04-23
    相关资源
    最近更新 更多