iOS APNS Push
func registRemotePush()
{
let center: UNUserNotificationCenter = UNUserNotificationCenter.current()
center.delegate = self
//center.requestAuthorization(options: [.alert, .badge, .sound], completionHandler: )
center.requestAuthorization(options: [.alert, .badge, .sound], completionHandler: {(granted:Bool, error:Error?) -> Void in
if granted {
center.getNotificationSettings(completionHandler: { (settings:UNNotificationSettings)->Void in
//设置通知
})
}
else
{
print("Granted failed!")
}
})
UIApplication.shared.registerForRemoteNotifications()
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
//print("注册远程推送成功 \(deviceToken)")
var token:String = ""
for byte in deviceToken
{
token.append(String(format:"%2x", byte))
}
print("注册远程推送成功 \(token)")
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("注册远程推送失败")
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let request: UNNotificationRequest = notification.request
let content: UNNotificationContent = request.content
let userInfo:Dictionary = content.userInfo
let badge:NSNumber? = content.badge
let title:String = content.title
let subtitle: String = content.subtitle
let body:String = content.body
let sound:UNNotificationSound? = content.sound
let triger:UNNotificationTrigger? = request.trigger
if let theTrigger = triger
{
if theTrigger.isKind(of: UNPushNotificationTrigger.self)
{
//远程通知
}
else
{
//本地通知
}
}
completionHandler([.badge, .sound, .alert])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let request: UNNotificationRequest = response.notification.request
let content: UNNotificationContent = request.content
let userInfo:Dictionary = content.userInfo
let badge:NSNumber? = content.badge
let title:String = content.title
let subtitle: String = content.subtitle
let body:String = content.body
let sound:UNNotificationSound? = content.sound
let triger:UNNotificationTrigger? = request.trigger
if let theTrigger = triger
{
if theTrigger.isKind(of: UNPushNotificationTrigger.self)
{
//远程通知
}
else
{
//本地通知
}
}
completionHandler()
}