抽空整理了一下推送相关,iOS推送的原理网上一大摞,在这就不再赘述了,这里整理了一下包括注册、注册成功与否的回调,推送前后台的展示与点击回调方法。(swift版本)
一、推送注册
在AppDelegate中的方法 application(_ application: UIApplication, didFinishLaunchingWithOptions中进行注册。
示例代码:
//注册推送通知
if #available(iOS 10.0, *) {
let notifCenter = UNUserNotificationCenter.current()
notifCenter.delegate = self
notifCenter.requestAuthorization(options: [.alert, .badge, .sound]) { (registered, error) in
#if DEBUG
if registered {
print("notification authorization granted")
} else {
print("notification authorization not granted,\nerror: \(String(describing: error))")
}
#endif
}
// 实现代理的方法, 接收到通知会调用.
} else {
let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
}
UIApplication.shared.registerForRemoteNotifications()
// 获取远程推送消息 iOS 10 已取消
let notification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [AnyHashable: Any]
// 如果remote不为空,就代表应用在未打开的时候收到了推送消息
if notification != nil {
// 收到推送消息实现的方法
UIApplication.shared.applicationIconBadgeNumber = 0
self.gotoViewControllerWithType(userInfo: notification!)
}
二、推送注册成功、失败回调
注册成功后将token上报给服务器,失败则显示alert(根据需求)。
示例代码:
//推送注册成功
func application(_ application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenParts = deviceToken.map { data -> String in
return String(format: "%02.2hhx", data)
}
let token = tokenParts.joined()
#if DEBUG
print("Device Token: \(token)")
#endif
//上报服务器token
self.reportToken(token: token)
}
//推送注册失败
func application(_ application: UIApplication,didFailToRegisterForRemoteNotificationsWithError error: Error) {
// print("registerError:\(error.localizedDescription)")
//展示alert
let alertVc = UIAlertController(title: nil, message: error.localizedDescription, preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertVc.addAction(action)
self.window?.rootViewController?.present(alertVc, animated: true, completion: nil)
}
三、收到通知的展示和点击
首先在APP未启动(被杀死)时,收到通知APP是无法回调的,然后具体的通知回调参考下图极光推送回调。
示例代码:
//接受通知
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
DDLogDebug("userInfo10:\(userInfo)")
if UIApplication.shared.applicationState == .active {
//应用在前台
}else{
//推送后的动作
self.gotoViewControllerWithType(userInfo: userInfo)
}
}
//10.0之前展示、点击代理,iOS10.0之后展示代理
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler handler: @escaping (UIBackgroundFetchResult) -> Void) {
// mxPrint("Notification received: \(userInfo)")
let extra = userInfo[AnyHashable("extra")] as! [AnyHashable: Any]
let type = extra[AnyHashable("type")] as! String
if UIApplication.shared.applicationState == .active {
}else{
self.gotoViewControllerWithType(userInfo: userInfo)
}
}
先到这,有问题欢迎指正。