从本地通知启动应用程序; VC 实例化,但没有出现

Launching app from Local Notification; VC instantiates, but does not appear

我想从本地通知启动我的应用程序,该通知将在主屏幕锁定时或当用户基于类似的讨论在另一个应用程序中时出现 and here 我的 AppDelegate 中有以下代码:

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

        switch response.actionIdentifier {

        case "play":
                var setAlarmVC = self.window?.rootViewController as? SettingAlarmViewController
                if setAlarmVC == nil {
                    let storyboard = UIStoryboard(name: "Main", bundle: nil)
                    setAlarmVC = storyboard.instantiateViewController(withIdentifier: "AlarmViewController") as? SettingAlarmViewController
                    self.window = UIWindow(frame: UIScreen.main.bounds)
                    self.window?.rootViewController = setAlarmVC
                    self.window?.makeKeyAndVisible()
                }

        case "snooze":
            print("I pressed pause")
        default:
            break
        }

        completionHandler()

    }

在我的 SettingAlarmViewController 的 viewDidLoad 中,我设置了一些简单的打印输出

override func viewDidLoad() {
    super.viewDidLoad()
    print("Setting Alarm View Controller was instantiated")
}

当我在应用程序处于后台时从本地通知按下播放时,我按预期打印了控制台:

Setting Alarm View Controller was instantiated

但该应用并未实际启动,也未出现设置警报视图控制器。如果我然后单击应用程序,一个新的设置警报视图控制器是第一个可见的东西。我觉得我一定错过了一些明显的东西,但我无法弄清楚它是什么。

编辑:进行更多测试。当通知出现在锁定屏幕上并且用户在锁定屏幕上按 "play" 时,密码/解锁屏幕不会出现,但应用程序仍会启动并且我得到打印输出“Setting Alarm View Controller was instantiated “

好吧,我一天的时间都浪费在这个上面了,这是为了让其他人不会遇到同样的问题:developer.apple。com/documentation/usernotifications/…你必须广告 [.foreground]到您的通知操作,如 let playAction = UNNotificationAction(identifier: "play", title: "play", options: [.foreground])