iOS 10 及以上系统,采用以下方法:
@available(iOS 10.0, *)
func jpushNotificationCenter(_ center: UNUserNotificationCenter!, willPresent notification: UNNotification!, withCompletionHandler completionHandler: ((Int) -> Void)!) {
// 前台通知,JPUSH socket直连
let userInfo = notification.request.content.userInfo
// 在这里处理推送消息
if let trigger = notification.request.trigger, trigger.isKind(of: UNPushNotificationTrigger.self){
#if arch(arm) || arch(arm64)
JPUSHService.handleRemoteNotification(userInfo)
#endif
}
completionHandler(Int(UNNotificationPresentationOptions.alert.rawValue))
}
@available(iOS 10.0, *)
func jpushNotificationCenter(_ center: UNUserNotificationCenter!, didReceive response: UNNotificationResponse!, withCompletionHandler completionHandler: (() -> Void)!) {
// 点击通知,无论App进程是否为杀死状态
let userInfo = response.notification.request.content.userInfo
// 在这里处理推送消息(点击推送消息进入应用)
if let trigger = response.notification.request.trigger, trigger.isKind(of: UNPushNotificationTrigger.self){
#if arch(arm) || arch(arm64)
JPUSHService.handleRemoteNotification(userInfo)
#endif
}
completionHandler()
}
iOS 10以下系统,采用以下方法:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
if application.applicationState == .active{ // iOS 10以下,前台
// 在这里处理前台推送消息
}else{ // iOS 10以下,后台,且进程未终结
// 在这里处理后台推送消息(点击推送消息进入应用)
}
#if arch(arm) || arch(arm64)
JPUSHService.handleRemoteNotification(userInfo)
#endif
}
// App进程已终结,启动App进程
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool{
if #available(iOS 10, *) {
// iOS 10及以上,不处理此消息
}else{
// 如果系统是iOS 10以下时,解析消息内容
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [AnyHashable : Any]{
// 在这里处理推送消息(点击App图标启动应用)
}
}
}