当应用程序处于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()
}