当前位置:主页 > 移动开发 > iOS代码 >

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

时间:2021-05-08 09:06:05 | 栏目:iOS代码 | 点击:

iOS10发布后,发现项目中的极光推送接收消息异常了。

查了相关资料后才发现,iOS10中对于通知做了不少改变。同时也发现极光也很快更新了对应的SDK。

现在就把适配修改的做法分享一下,希望对有需要的童鞋有所帮助。

具体做法如下:

注意:必须先安装Xcode8.0版本。

一、添加相关的SKD,或framework文件

1、添加UserNotification.framework

2、更新jpush的SDK(最新版本:jpush-ios-2.1.9.a)https://www.jiguang.cn

二、进行路径和消息推送的配置

1、设置jpush的SDK的路径

2、开启消息推送功能

三、代码修改

1、添加userNotification的头文件

2、添加userNotification的启用代码

3、添加jpush的适配代码

4、添加jpush的代理和代理方法(注意:在appDelegate.m文件中使用)


补充:完整的使用极光

1、导入相应头文件

#import "JPUSHService.h" 
#import <AdSupport/AdSupport.h> 
#ifdef NSFoundationVersionNumber_iOS_9_x_Max 
// 这里是iOS10需要用到的框架 
#import <UserNotifications/UserNotifications.h> 
#endif

2、启动极光推送功能

static NSString *JPushAppKey = @"6abc87b33b23d35b9c3b86e0"; 
static NSString *JPushChannel = @"Publish channel"; 
// static BOOL JPushIsProduction = NO; 
#ifdef DEBUG 
// 开发 极光FALSE为开发环境 
static BOOL const JPushIsProduction = FALSE; 
#else 
// 生产 极光TRUE为生产环境 
static BOOL const JPushIsProduction = TRUE; 
#endif 
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
// Override point for customization after application launch. 
// 启动极光推送 
// Required 
// - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { } 
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) // iOS10 
{ 
#ifdef NSFoundationVersionNumber_iOS_9_x_Max 
JPUSHRegisterEntity *entity = [[JPUSHRegisterEntity alloc] init]; 
entity.types = (UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound); 
[JPUSHService registerForRemoteNotificationConfig:entity delegate:target]; 
#endif 
} 
else 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]; 
} 
// Required 
// [JPUSHService setupWithOption:launchOptions] 
// pushConfig.plist appKey 
// 有广告符标识IDFA(尽量不用,避免上架审核被拒) 
/* 
NSString *JPushAdvertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; 
[JPUSHService setupWithOption:JPushOptions 
appKey:JPushAppKey 
channel:JPushChannel 
apsForProduction:JPushIsProduction 
advertisingIdentifier:JPushAdvertisingId]; 
*/ 
// 或无广告符标识IDFA(尽量不用,避免上架审核被拒) 
[JPUSHService setupWithOption:options 
appKey:JPushAppKey 
channel:JPushChannel 
apsForProduction:JPushIsProduction]; 
// 2.1.9版本新增获取registration id block接口。 
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) { 
if(resCode == 0) 
{ 
// iOS10获取registrationID放到这里了, 可以存到缓存里, 用来标识用户单独发送推送 
NSLog(@"registrationID获取成功:%@",registrationID); 
[[NSUserDefaults standardUserDefaults] setObject:registrationID forKey:@"registrationID"]; 
[[NSUserDefaults standardUserDefaults] synchronize]; 
} 
else 
{ 
NSLog(@"registrationID获取失败,code:%d",resCode); 
} 
}]; 
return YES; 
}

3、注册

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{ 
[JPUSHService registerDeviceToken:data]; 
}

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、其他注意事项

为了保证用户能正常接收,或有针对性的接收通知,登录成功后(或退出后)需要设置别名、标记。通常都是该逻辑都是写在用户登录APP成功之后,或者是用户退出当前登录状态后。

/// 绑定别名(注意:1 登录成功或者自动登录后;2 去除绑定-退出登录后) 
+ (void)JPushTagsAndAliasInbackgroundTags:(NSSet *)set alias:(NSString *)name 
{ 
// 标签分组(表示没有值) 
NSSet *tags = set; 
// 用户别名(自定义值,nil是表示没有值) 
NSString *alias = name; 
NSLog(@"tags = %@, alias = %@(registrationID = %@)", tags, alias, [self registrationID]); 
// tags、alias均无值时表示去除绑定 
[JPUSHService setTags:tags aliasInbackground:alias]; 
}

您可能感兴趣的文章:

相关文章