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

来源:互联网 发布:淘宝查号网 编辑:程序博客网 时间:2024/04/29 21:42

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的适配代码

j

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





补充:完整的使用极光

1、导入相应头文件

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #import "JPUSHService.h"  
  2. #import <AdSupport/AdSupport.h>  
  3. #ifdef NSFoundationVersionNumber_iOS_9_x_Max  
  4. // 这里是iOS10需要用到的框架  
  5. #import <UserNotifications/UserNotifications.h>  
  6. #endif  

2、启动极光推送功能

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. static NSString *JPushAppKey  = @"6abc87b33b23d35b9c3b86e0";  
  2. static NSString *JPushChannel = @"Publish channel";  
  3. // static BOOL JPushIsProduction = NO;  
  4. #ifdef DEBUG  
  5. // 开发 极光FALSE为开发环境  
  6. static BOOL const JPushIsProduction = FALSE;  
  7. #else  
  8. // 生产 极光TRUE为生产环境  
  9. static BOOL const JPushIsProduction = TRUE;  
  10. #endif  

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  2.     // Override point for customization after application launch.  
  3.       
  4.     // 启动极光推送  
  5.     // Required  
  6.     // - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { }  
  7.     if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0// iOS10  
  8.     {  
  9. #ifdef NSFoundationVersionNumber_iOS_9_x_Max  
  10.         JPUSHRegisterEntity *entity = [[JPUSHRegisterEntity alloc] init];  
  11.         entity.types = (UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound);  
  12.         [JPUSHService registerForRemoteNotificationConfig:entity delegate:target];  
  13. #endif  
  14.     }  
  15.     else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)  
  16.     {  
  17.         // categories  
  18.         [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)  
  19.                                               categories:nil];  
  20.     }  
  21.     else  
  22.     {  
  23.         // categories nil  
  24.         [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)  
  25.                                               categories:nil];  
  26.     }  
  27.       
  28.     // Required  
  29.     // [JPUSHService setupWithOption:launchOptions]  
  30.     // pushConfig.plist appKey  
  31.     // 有广告符标识IDFA(尽量不用,避免上架审核被拒)  
  32.     /* 
  33.     NSString *JPushAdvertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; 
  34.     [JPUSHService setupWithOption:JPushOptions 
  35.                            appKey:JPushAppKey 
  36.                           channel:JPushChannel 
  37.                  apsForProduction:JPushIsProduction 
  38.             advertisingIdentifier:JPushAdvertisingId]; 
  39.     */  
  40.     // 或无广告符标识IDFA(尽量不用,避免上架审核被拒)  
  41.     [JPUSHService setupWithOption:options  
  42.                            appKey:JPushAppKey  
  43.                           channel:JPushChannel  
  44.                  apsForProduction:JPushIsProduction];  
  45.       
  46.       
  47.     // 2.1.9版本新增获取registration id block接口。  
  48.     [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {  
  49.         if(resCode == 0)  
  50.         {  
  51.             // iOS10获取registrationID放到这里了, 可以存到缓存里, 用来标识用户单独发送推送  
  52.             NSLog(@"registrationID获取成功:%@",registrationID);  
  53.             [[NSUserDefaults standardUserDefaults] setObject:registrationID forKey:@"registrationID"];  
  54.             [[NSUserDefaults standardUserDefaults] synchronize];  
  55.         }  
  56.         else  
  57.         {  
  58.             NSLog(@"registrationID获取失败,code:%d",resCode);  
  59.         }  
  60.     }];  
  61.   
  62.       
  63.     return YES;  
  64. }  

3、注册

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken  
  2. {  
  3.     [JPUSHService registerDeviceToken:data];  
  4. }  
4、注册失败

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificwationsWithError:(NSError *)error  
  2. {  
  3.     NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);  
  4. }  

5、接收

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo  
  2. {  
  3.     // apn 内容获取:  
  4.     // 取得 APNs 标准信息内容  
  5.     [JPUSHService handleRemoteNotification:dict];  
  6. }  
6、处理通知

6-1、iOS10以下版本时

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler  
  2. {  
  3.     DLog(@"2-1 didReceiveRemoteNotification remoteNotification = %@", userInfo);  
  4.       
  5.     // apn 内容获取:  
  6.     [JPUSHService handleRemoteNotification:dict];  
  7.     completionHandler(UIBackgroundFetchResultNewData);  
  8.       
  9.     DLog(@"2-2 didReceiveRemoteNotification remoteNotification = %@", userInfo);  
  10.     if ([userInfo isKindOfClass:[NSDictionary class]])  
  11.     {  
  12.         NSDictionary *dict = userInfo[@"aps"];  
  13.         NSString *content = dict[@"alert"];  
  14.         DLog(@"content = %@", content);  
  15.     }  
  16.       
  17.     if (application.applicationState == UIApplicationStateActive)  
  18.     {  
  19.         // 程序当前正处于前台  
  20.     }  
  21.     else if (application.applicationState == UIApplicationStateInactive)  
  22.     {  
  23.         // 程序处于后台  
  24.     }  
  25. }  
6-2、iOS10及以上版本时

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #pragma mark - iOS10: 收到推送消息调用(iOS10是通过Delegate实现的回调)  
  2. #pragma mark- JPUSHRegisterDelegate  
  3.   
  4. #ifdef NSFoundationVersionNumber_iOS_9_x_Max  
  5. // 当程序在前台时, 收到推送弹出的通知  
  6. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler  
  7. {  
  8.     NSDictionary *userInfo = notification.request.content.userInfo;  
  9.       
  10.     if ([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])  
  11.     {  
  12.         [JPUSHService handleRemoteNotification:userInfo];  
  13.     }  
  14.       
  15.     // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置  
  16.     // completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);  
  17. }  
  18.   
  19. // 程序关闭后, 通过点击推送弹出的通知  
  20. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler  
  21. {  
  22.     NSDictionary *userInfo = response.notification.request.content.userInfo;  
  23.       
  24.     if ([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])  
  25.     {  
  26.         [JPUSHService handleRemoteNotification:userInfo];  
  27.     }  
  28.       
  29.     completionHandler();  // 系统要求执行这个方法  
  30. }  
  31. #endif  

7、其他注意事项

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

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. /// 绑定别名(注意:1 登录成功或者自动登录后;2 去除绑定-退出登录后)  
  2. + (void)JPushTagsAndAliasInbackgroundTags:(NSSet *)set alias:(NSString *)name  
  3. {  
  4.     // 标签分组(表示没有值)  
  5.     NSSet *tags = set;  
  6.     // 用户别名(自定义值,nil是表示没有值)  
  7.     NSString *alias = name;  
  8.     NSLog(@"tags = %@, alias = %@(registrationID = %@)", tags, alias, [self registrationID]);  
  9.       
  10.     // tags、alias均无值时表示去除绑定  
  11.     [JPUSHService setTags:tags aliasInbackground:alias];  

0 0
原创粉丝点击