在 Swift 3 中单击处于终止/关闭状态的推送通知时,应用程序无法打开

2023-12-28

如何打开一个Application当应用程序处于killed/closed状态,我收到通知,但当我单击通知应用程序时未打开,但当应用程序位于前台或后台时工作正常。我尝试了下面的代码仍然无法解决问题所在。请检查我尝试过的内容

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    if #available(iOS 10.0, *) {
        let center = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(grant, error)  in
            if error == nil {
                if grant {
                    DispatchQueue.main.async(execute: {
                        application.registerForRemoteNotifications()
                    })
                } else {
                    //User didn't grant permission
                }
            } else {
                print("error: ",error as Any)
            }
        })
    } else {
        // Fallback on earlier versions
        let notificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(notificationSettings)
    }
    self.updateAppPNBadge()
    if let notification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String : AnyObject] {
        _ = notification["aps"] as! [String : AnyObject]
        (window?.rootViewController as! TabBar).selectedIndex = 4
    }
    return true
}

func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
    if notificationSettings.types != .none {
        application.registerForRemoteNotifications()
    }
}   

// Token registered
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let tokenString = deviceToken.reduce("") { string, byte in
            string + String(format: "%02X", byte)
        }
        print("token: ", tokenString)

        UserDefaults.standard.set("\(tokenString)", forKey: "tokenID")
}

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
        completionHandler([UNNotificationPresentationOptions.alert,UNNotificationPresentationOptions.sound,UNNotificationPresentationOptions.badge])
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {

    if #available(iOS 10.0, *) {
        if let info = userInfo["aps"] as? Dictionary<String, AnyObject>
        {
        print("\(info)")
        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TabBar") as! UITabBarController
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = vc
        vc.selectedIndex = 4
        self.window?.makeKeyAndVisible()
        }
    }
    else {
        if let info = userInfo["aps"] as? Dictionary<String, AnyObject>
        {
        let alertMsg = info["alert"] as? String

        let alert = UIAlertView(title: "", message: alertMsg, delegate: nil, cancelButtonTitle: "OK")
        alert.show()
        print("\(info)")
        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TabBar") as! UITabBarController
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = vc
        vc.selectedIndex = 4
        self.window?.makeKeyAndVisible()
        }
    }
        UIApplication.shared.applicationIconBadgeNumber = UIApplication.shared.applicationIconBadgeNumber + 1
}

func updateAppPNBadge() {
        UIApplication.shared.applicationIconBadgeNumber = 0
        UIApplication.shared.cancelAllLocalNotifications()
}

func applicationWillResignActive(_ application: UIApplication) {

    self.updateAppPNBadge()
}

func applicationDidEnterBackground(_ application: UIApplication) {

    self.updateAppPNBadge()
}
func applicationWillTerminate(_ application: UIApplication) {
    self.updateAppPNBadge()
}

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Swift 3 中单击处于终止/关闭状态的推送通知时,应用程序无法打开 的相关文章

随机推荐