如何在应用程序关闭时在本地通知上打开 VC

How to open VC on local notification when app is closed

Hy 我是 iOS 的新手,但不知何故我设法完成了一些任务。我正在开发为用户设置提醒的应用程序。因此,为此我使用了本地通知 (UNUserNotificationCenterDelegate)。

一切正常。我写了一些代码,我在预定的时间收到通知,我已经处理了以下情况。

我的应用程序可以完美地处理这两种情况,或者您可以按我的需要说。但我在下面的案例中束手无策

when the App is removed from recent, or not even running in background at all,and a that time if the scheduled notification pops up, and user taps the notification, It opens the splash view controller then opens my app main view controller, where as I need to go to same view controller from where user set the scheduled time for reminder.

我想我很清楚自己想要什么,正在发生什么。是否有任何更改可以做到这一点。我知道这是可能的,因为 Whats App 和其他应用程序也在这样做。请帮我做这件事。 ...

注: 我正在使用 UserNotifications(本地通知)并且部署目标是 10.3

更新: 我看到 link 和我有同样的需求,但我不知道选择的答案是什么,因为我是 iOS 的新手,所以我不确定该做什么和怎么做:

当您的应用程序通过 LocalNotification 启动时,您的 UNUserNotificationCenterDelegate 方法 userNotificationCenter didReceive response 将被调用。

所以我建议您按照下面的方法在当前视图控制器的顶部显示您的通知。

//Add this extension in any of your class
extension UIApplication {

@objc class func topViewController(_ base: UIViewController?) -> UIViewController? {

    var baseController = base

    if baseController == nil{

        baseController = UIApplication.shared.keyWindow?.rootViewController
    }

    if let nav = baseController as? UINavigationController {
        return topViewController(nav.visibleViewController)
    }

    if let tab = baseController as? UITabBarController {
        if let selected = tab.selectedViewController {
            return topViewController(selected)
        }
    }

    return baseController
}

}

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


    if response.actionIdentifier == "YourIdentifier"{

        let controllerToPresent = MyNotificationViewController(nibName: "MyNotificationViewController", bundle: nil)
        controllerToPresent.notificationInfo = response.notification.request.content.userInfo

        //If navigation flow needed, add controllerToPresent to nav controller
        let navConroller = UINavigationController(rootViewController: controllerToPresent)
        UIApplication.topViewController(nil)?.present(navConroller, animated: true, completion: nil)

        //If navigation flow not needed, present directly
        UIApplication.topViewController(nil)?.present(controllerToPresent, animated: true, completion: nil)
    }

    completionHandler()
}

那么,您的问题是当应用程序被终止或处于非活动状态时,然后当用户点击通知时,提醒屏幕会出现,对吗?

情况如下: 通知显示 (inactive/killed) -> 点击通知 -> 启动画面 -> 提醒屏幕。

您应该保存要在通知中显示的数据。 iOS 将在 remoteNotification 中保存任何通知数据。

因此,当用户从非活动状态打开应用程序时,首先调用的是 AppDelegate 中的 launchOption

示例如下:

    if launchOptions != nil {
    // get data from notificaioton when app is killed or incative
           if let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary  {
                   // Do what you want, you can set set the trigger to move it the screen as you want, for your case is to reminder screen
                }
            }