Swift ios 10 收到推送处理
2017-03-24 本文已影响369人
71150ce14a00
import UserNotifications
import UserNotificationsUI
extension AppDelegate:UNUserNotificationCenterDelegate{
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void){
let userInfo = notification.request.content.userInfo
print("userInfo10:\(userInfo)")
completionHandler([.sound,.alert])
}
//iOS10新增:处理后台点击通知的代理方法
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void){
let userInfo = response.notification.request.content.userInfo
print("userInfo10:\(userInfo)")
completionHandler()
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
print("收到新消息Active\(userInfo)")
if application.applicationState == UIApplicationState.active {
// 代表从前台接受消息app
}else{
// 代表从后台接受消息后进入app
UIApplication.shared.applicationIconBadgeNumber = 0
}
completionHandler(.newData)
}
func registerAppNotificationSettings(launchOptions: [NSObject: AnyObject]?) {
if #available(iOS 10.0, *) {
let notifiCenter = UNUserNotificationCenter.current()
notifiCenter.delegate = self
let types = UNAuthorizationOptions(arrayLiteral: [.alert, .badge, .sound])
notifiCenter.requestAuthorization(options: types) { (flag, error) in
if flag {
print("iOS request notification success")
}else{
print(" iOS 10 request notification fail")
}
}
} else { //iOS8,iOS9注册通知
let setting = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(setting)
}
UIApplication.shared.registerForRemoteNotifications()
}
}