ios - 当应用程序关闭时从推送通知打开特定的 ViewController

标签 ios swift

当应用程序关闭时(不在后台或非事件状态)推送通知时,我不想打开特殊的 ViewController 当应用程序在后台运行时,但当应用程序关闭时,Xcode 推送到 Main ViewController

我尝试使用所有应用程序状态,但不起作用

    if(application.applicationState == .background) {
        let dict = userInfo["aps"] as? NSDictionary
        if let cont = dict {
            let cate = cont["category"] as? String
            if let category = cate {
                switch category {
                case "NEWS":
                    let storyboard = UIStoryboard(name: "Shops", bundle: nil)
                    let NewBoard = storyboard.instantiateViewController(withIdentifier: "MainShopsViewController")
                    let tabbar = self.window?.rootViewController as? UITabBarController

                    let navcontrol = tabbar?.selectedViewController as? UINavigationController
                    tabbar?.selectedIndex = 1
                    navcontrol?.pushViewController(NewBoard, animated: true)
                    self.window?.makeKeyAndVisible()
                default:
                    break
                }
            }
        }
    }

最佳答案

你可以使用这个函数来做到这一点..

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

    // tell the app that we have finished processing the user’s action / response
    completionHandler()
}

关于ios - 当应用程序关闭时从推送通知打开特定的 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56869498/

相关文章:

ios - MutableCollectionType 的 Swift 扩展不适用于泛型类型数组

ios - UIViewController 没有正确加载它的 xib

iphone - 中文环境默认字体是什么?

iphone - ios 6 : Center UILabel vertically using Auto Layout constraints

Swift performSegueWithIdentifier 显示黑屏

swift for windows平台和linux平台开发

swift - swift 中的转换运算符

ios - 调用 imageWithData :UIImageJPEGRepresentation() multiple times only compresses image the first time

ios - 在 iOS 应用程序中使用位置服务时如何确定应用程序重新启动的确切原因?

应用程序终止时的 iOS 10 更新位置