Taking the time to order a bit push relevant, iOS push online pile principle, in which not repeat them here sorted out, including registration, registration success callback, the callback method impressions and clicks Taiwan before and after the push. (Swift version)
First, push registration
AppDelegate method of application (_ application:. UIApplication, didFinishLaunchingWithOptions register in
the sample code:
//注册推送通知
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!)
}
Second, push registration success, failure callback
After successful registration token is reported to server failure alert (on demand) is displayed.
Sample code:
//推送注册成功
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)
}
Third, we received notice of impressions and clicks
First, when the APP is not started (to be killed), I notified APP is unable callback, then under specific notification callback with reference to FIG Aurora push callback.
Sample code:
//接受通知
@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)
}
}
First this, there is a problem please correct me.
Reproduced in: https: //www.jianshu.com/p/b4f3e1d6e8cf