请尝试下面的代码
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) { if application.applicationState == .inactive || application.applicationState == .background { //opened from a push notification when the app was on background } else { if application.applicationState == .active { let myuserinfo = userInfo as NSDictionary print(myuserinfo) let tempDict:NSDictionary = ((myuserinfo.value(forKey: "aps") as? NSDictionary))! let alert = UIAlertController(title: "alertView", message: (tempDict.value(forKey: "alert") as? String)!, preferredStyle: .alert) let ok = UIAlertAction(title: "OK", style: .default, handler: nil) alert.addAction(ok) //alert.show() self.window?.rootViewController?.present(alert, animated: true, completion: nil) let _:NSDictionary = (tempDict.value(forKey: "custom") as? NSDictionary)! } } }
改变这一行:
let person:NSDictionary = (tempDict.value(forKey: "custom") as? NSDictionary!)!
至
let person:NSDictionary = (tempDict.value(forKey: "custom") as? NSDictionary)!
同样在appdelegate还有一个方法,你也可以这样做。用上面一行替换该行。可能是这个帮助。如果你有任何问题,请告诉我或发布任何其他问题。