新版iOS10更新后, 通知的處理發(fā)生里很大變化, 本人也剛剛處理完成這一方面的問(wèn)題, 寫出來(lái)希望幫助更多的人, 項(xiàng)目中用的是極光推送的最新版.
極光的配置
#pragma mark - 極光推送
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
#endif
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//可以添加自定義categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
} else {
//categories 必須為nil
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
}
[JPUSHService setupWithOption:launchOptions appKey:JPushSpKey
channel:@"Publish channel"
apsForProduction:YES
advertisingIdentifier:nil];
在極光2.1.9版本以后可實(shí)現(xiàn)SDK封裝的JPUSHRegisterDelegate協(xié)議方法拇囊,適配iOS10新增的delegate協(xié)議方法磷支。
即以下兩個(gè)方法:
#pragma mark - 極光推送 to iOS7-9
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// IOS 7 Support Required
[JPUSHService handleRemoteNotification:userInfo];
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
SPLog(@"iOS7-9 前臺(tái)收到通知");
}else if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive) {
SPLog(@"iOS7-9 離線收到通知");
}else if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
SPLog(@"iOS7-9 后臺(tái)收到通知");
}
completionHandler(UIBackgroundFetchResultNewData);
}
#pragma mark- JPUSHRegisterDelegate
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
//應(yīng)用處于前臺(tái)
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
NSDictionary * userInfo = notification.request.content.userInfo;
UNNotificationRequest *request = notification.request; // 收到推送的請(qǐng)求
UNNotificationContent *content = request.content; // 收到推送的消息內(nèi)容
NSNumber *badge = content.badge; // 推送消息的角標(biāo)
NSString *body = content.body; // 推送消息體
UNNotificationSound *sound = content.sound; // 推送消息的聲音
NSString *subtitle = content.subtitle; // 推送消息的副標(biāo)題
NSString *title = content.title; // 推送消息的標(biāo)題
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
[self analysisUserInfoWithDict:userInfo];
SPLog(@"iOS10 前臺(tái)收到遠(yuǎn)程通知");
}else{
SPLog(@"iOS10 前臺(tái)收到本地通知:{\nbody:%@青抛,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);
SPLog(@"iOS10 前臺(tái)收到本地通知");
}
//需要執(zhí)行這個(gè)方法弱判,選擇是否提醒用戶.有Badge绑青、Sound、Alert三種類型可以設(shè)置
completionHandler(UNNotificationPresentationOptionBadge|
UNNotificationPresentationOptionSound|
UNNotificationPresentationOptionAlert);
}
//應(yīng)用未啟動(dòng)
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
NSDictionary * userInfo = response.notification.request.content.userInfo;
UNNotificationRequest *request = response.notification.request; // 收到推送的請(qǐng)求
UNNotificationContent *content = request.content; // 收到推送的消息內(nèi)容
NSNumber *badge = content.badge; // 推送消息的角標(biāo)
NSString *body = content.body; // 推送消息體
UNNotificationSound *sound = content.sound; // 推送消息的聲音
NSString *subtitle = content.subtitle; // 推送消息的副標(biāo)題
NSString *title = content.title; // 推送消息的標(biāo)題
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
[self analysisUserInfoWithDict:userInfo];
SPLog(@"iOS10 離線收到遠(yuǎn)程通知");
}else{
SPLog(@"iOS10 收到本地通知:{\nbody:%@怠褐,\ntitle:%@,\nsubtitle:%@,\nbadge:%@畏梆,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);
SPLog(@"iOS10 離線收到本地通知");
}
completionHandler(); // 系統(tǒng)要求執(zhí)行這個(gè)方法
}
#endif