解析iOS10中的极光推送消息的适配

2020-01-18 15:55:55刘景俊

4、注册失败


- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificwationsWithError:(NSError *)error 
{ 
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); 
}

5、接收


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 
// apn 内容获取: 
// 取得 APNs 标准信息内容 
[JPUSHService handleRemoteNotification:dict]; 
}

6、处理通知

6-1、iOS10以下版本时


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler 
{ 
DLog(@"2-1 didReceiveRemoteNotification remoteNotification = %@", userInfo); 
// apn 内容获取: 
[JPUSHService handleRemoteNotification:dict]; 
completionHandler(UIBackgroundFetchResultNewData); 
DLog(@"2-2 didReceiveRemoteNotification remoteNotification = %@", userInfo); 
if ([userInfo isKindOfClass:[NSDictionary class]]) 
{ 
NSDictionary *dict = userInfo[@"aps"]; 
NSString *content = dict[@"alert"]; 
DLog(@"content = %@", content); 
} 
if (application.applicationState == UIApplicationStateActive) 
{ 
// 程序当前正处于前台 
} 
else if (application.applicationState == UIApplicationStateInactive) 
{ 
// 程序处于后台 
} 
}

6-2、iOS10及以上版本时


#pragma mark - iOS10: 收到推送消息调用(iOS10是通过Delegate实现的回调) 
#pragma mark- JPUSHRegisterDelegate 
#ifdef NSFoundationVersionNumber_iOS_9_x_Max 
// 当程序在前台时, 收到推送弹出的通知 
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler 
{ 
NSDictionary *userInfo = notification.request.content.userInfo; 
if ([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) 
{ 
[JPUSHService handleRemoteNotification:userInfo]; 
} 
// 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置 
// completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert); 
} 
// 程序关闭后, 通过点击推送弹出的通知 
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler 
{ 
NSDictionary *userInfo = response.notification.request.content.userInfo; 
if ([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) 
{ 
[JPUSHService handleRemoteNotification:userInfo]; 
} 
completionHandler(); // 系统要求执行这个方法 
} 
#endif

7、其他注意事项