所以由于某种原因,从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
and是否比尝试实例化正确的 VC 更好?或者有解决办法吗?UIWindowScene
UIWindow