Приложение не открывается при нажатии на Push-уведомления в состоянии «убито / закрыто» в Swift 3 - PullRequest
0 голосов
/ 06 октября 2018

Как открыть 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()
}
...