100%工作测试
第一次进口
import UserNotifications
然后在类中添加委托
UNUserNotificationCenterDelegate
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
下面的方法是负责,而应用程序是打开和通知来。
将呈现
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let content = notification.request.content
let alertVC = UIAlertController.init(title: title, message: body, preferredStyle: .alert)
alertVC.addAction(UIAlertAction.init(title: appLan_share.Ok_txt, style: .default, handler: {
_ in
//handle tap here or navigate somewhere…..
}))
vc?.present(alertVC, animated: true, completion: nil)
print("notification Data: \(content.userInfo.values)")
completionHandler([.alert, .sound])
}
您还可以通过检查当前应用程序状态来处理应用程序状态。
此外,如果你的应用程序没有运行,那么下面的方法是负责处理推送通知
didReceive
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
let aps = userInfo["aps"] as? [String: Any]
let alert = aps?["alert"] as? [String: String]
}