【问题标题】:Push Notifications - Push a ViewController upon Notification Tap with SceneDelegate推送通知 - 使用 SceneDelegate 在通知点击时推送 ViewController
【发布时间】:2020-01-10 04:40:35
【问题描述】:

在 iOS 13 之前,导航控制器和根视图是在 AppDelegate 中定义的。然而,在 iOS 13 中,Apple 引入了 SceneDelegate,它接管了这些视图功能的处理。但是,AppDelegate 仍然处理诸如本地通知处理之类的事情。 See this answer for some code that outlines these changes for root views.

如果我想在用户点击本地通知时推送视图,我会在 AppDelegate 中执行以下操作:

extension AppDelegate: UNUserNotificationCenterDelegate {

    var navigationController: UINavigationController?

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        if response.actionIdentifier == UNNotificationDefaultActionIdentifier { //User taps notification
             let vc = MyViewController()
             self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}

但是,由于我的项目的根导航控制器现在可以在 iOS 13 中的 SceneDelegate 中定义,我似乎无法弄清楚如何在由 SceneDelegate 而不是 AppDelegate 管理的导航控制器中推送视图。

【问题讨论】:

  • 我在同一条船上。在 AppDelegate 中,window 变量为零。我是否在SceneDelegate 中设置窗口并在AppDelegate 中使用它。

标签: ios swift


【解决方案1】:

SceneDelegate可以这样处理通知响应:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  func scene(
    _ scene: UIScene,
    willConnectTo session: UISceneSession,
    options connectionOptions: UIScene.ConnectionOptions
  ) {
    if let windowScene = scene as? UIWindowScene {
      let window = UIWindow(windowScene: windowScene)
      // rootViewController set up code
      // say,
      // let mainController = ViewController()
      // let navigationController = UINavigationController(rootViewController: mainController)
      // window.rootViewController = navigationController

      // This is UNNotificationResponse
      if let notificationResponse = connectionOptions.notificationResponse {
        window.makeKeyAndVisible()
        // do the pushing on your navigation controller
        // navigationController.push()
        return
      }

      window.makeKeyAndVisible()
    }
  }
}

【讨论】:

    【解决方案2】:

    如果您的应用支持多窗口或单窗口,您可以使用UNNotificationResponse 上的targetScene 属性来确定通知将启动哪个场景(窗口)。

    例子:

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        guard let windowSceneDelegate = response.targetScene?.delegate as? UIWindowSceneDelegate,
              let window = windowSceneDelegate.window,
              let rootViewController = window.rootViewController as? UINavigationController else {
            completionHandler()
            return
        }
        let notificationViewController = UIViewController()
        rootViewController.pushViewController(notificationViewController, animated: true)
        completionHandler()
    }
    

    【讨论】:

    • 亚马逊。在做需要的事情之前,我应该把控制器作为导航控制器。谢谢。我的标签和导航有点复杂。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    相关资源
    最近更新 更多