推送通知-使用SceneDelegate在通知点击时推送ViewController [英] Push Notifications - Push a ViewController upon Notification Tap with SceneDelegate

查看:324
本文介绍了推送通知-使用SceneDelegate在通知点击时推送ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 13之前,AppDelegate中定义了导航控制器和根视图.但是,在iOS 13中,Apple引入了SceneDelegate,它接管了这些视图功能的处理.但是,AppDelegate仍然可以处理诸如本地通知处理之类的事情. 请参见此答案,以获取一些概述了根视图的更改的代码.

Prior to iOS 13, navigation controllers and root views were defined in AppDelegate. With iOS 13 however, Apple introduced SceneDelegate, which takes over the handling of these view functions. However, AppDelegate still handles things such as Local Notification Handling. See this answer for some code that outlines these changes for root views.

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

If I wanted a view to be pushed when a user taps a local notification, I would do the something like the following in 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管理的导航控制器中推送视图.

However, since the root navigation controller for my project can now be defined in SceneDelegate as of iOS 13, I can't seem to figure out how to push a view within a navigation controller managed by SceneDelegate instead of AppDelegate.

推荐答案

SceneDelegate可以通过以下方式处理通知响应:

SceneDelegate can handle notification response this way:

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

这篇关于推送通知-使用SceneDelegate在通知点击时推送ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆