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圖標啟動應用)
}
}
}