【问题标题】:instantiate view controller from fcm push notification从 fcm 推送通知实例化视图控制器
【发布时间】:2021-06-05 05:44:50
【问题描述】:

所以由于某种原因,从AppDelegate 实例化时总是会出现问题。我有一个带有UNNotificationAction 的通知,我在didReceiveResponse() 方法中处理操作选择。

函数如下:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
  
       // Print message ID.
       if let messageID = userInfo[gcmMessageIDKey] {
         print("Message ID: \(messageID)")
       }
    
   
    if response.actionIdentifier == "viewNewEvent" {
        let dashVC: UIViewController = storyboard.instantiateViewController(withIdentifier: Constants.StoryboardIDs.StudentEventDashboardStoryboardID) as! StudentSegmentedTableViewController
        let navDashVC = UINavigationController(rootViewController: dashVC)
        self.window?.rootViewController = navDashVC
        self.window?.makeKeyAndVisible()
        
    }

       print(userInfo)
    print("didReceiveResponse called")

    completionHandler()
}

当我在通知中选择操作时,if 语句中的代码块永远不会运行。起初,我强制展开所有内容,但我收到一条错误消息,提示 viewController 为 nil,现在当我运行并选择通知中的操作时,什么也没有发生。

viewController 怎么可能是 nil?我遇到了类似的情况,并决定在SceneDelegate 中尝试它,它确实有效,但有很多错误需要我最终修复。 在这种情况下,我最好使用UIScene 和UIWindowScene 而不是仅使用UIWindow 来尝试实例化正确的VC?或者有解决办法吗?

【问题讨论】:

    标签: swift storyboard apple-push-notifications appdelegate uiscenedelegate


    【解决方案1】:

    好吧,经过几个小时的研究,我意识到这不能再在 AppDelegate 中完成,我现在必须使用 SceneDelegate 来处理与 UIWindow 和 UIScene. 相关的任何事情

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-18
      • 2020-04-21
      • 2018-05-01
      • 1970-01-01
      • 2015-10-20
      • 2016-10-22
      • 1970-01-01
      相关资源
      最近更新 更多