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

 更新时间:2016年09月26日 14:14:26   作者:番薯大佬  
这篇文章主要介绍了解析iOS10中的极光推送消息的适配的相关资料,我们需要先安装Xcode8.0版本,接下来本文分步骤详细给大家介绍,需要的朋友可以参考下

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]; 
}

以上所述是小编给大家介绍的解析iOS10中的极光推送消息的适配,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • IOS代码笔记UIView的placeholder的效果

    IOS代码笔记UIView的placeholder的效果

    这篇文章主要为大家详细介绍了IOS实现placeholder效果的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • IOS 开发之操作图库自定义控制器

    IOS 开发之操作图库自定义控制器

    这篇文章主要介绍了IOS 开发之操作图库自定义控制器的相关资料,需要的朋友可以参考下
    2017-02-02
  • iOS实现富文本编辑器的方法详解

    iOS实现富文本编辑器的方法详解

    大家在开发的时候经常会用到富文本编辑器,所以这篇文章就给大家整理了如何使用iOS实现富文本编辑器的方法,相信本文对大家具有一定的参考借鉴价值,有需要的朋友们可以一起来看看。
    2016-10-10
  • UITableView中Cell重用机制导致内容重复的解决方法

    UITableView中Cell重用机制导致内容重复的解决方法

    这篇文章主要为大家详细介绍了UITableView中Cell重用机制导致内容重复的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • iOS App开发中的UIStackView堆叠视图使用教程

    iOS App开发中的UIStackView堆叠视图使用教程

    UIStackView是iOS9以来新增加的组件,使我们能够对UIView子类对象进行灵活排版,这里我们就来看一下iOS App开发中的UIStackView堆叠视图使用教程
    2016-07-07
  • iOS 懒加载的使用实例代码

    iOS 懒加载的使用实例代码

    本篇文章主要介绍了iOS 懒加载的使用实例代码,详细的介绍了什么是懒加载和优点,及其实例。有兴趣的可以了解一下
    2017-05-05
  • iOS如何获取当前View所在控制器的方法

    iOS如何获取当前View所在控制器的方法

    在开发iOS的时候经常需要获取当前View所在的控制器,下面小编给大家分享个方法,文章给出了示例代码,对大家的学习和理解很有帮助,下面来一起看看吧。
    2016-09-09
  • iOS中利用UIBezierPath + CAAnimation实现心跳动画效果

    iOS中利用UIBezierPath + CAAnimation实现心跳动画效果

    这篇文章主要给大家介绍了关于iOS中利用UIBezierPath + CAAnimation实现心跳动画效果的相关资料,文中通过示例代码介绍的非常详细,对大家的日常开发具有一定的参考学习,需要的朋友们下面随着小编来一起学习学习吧。
    2017-10-10
  • iOS开发之MRC(手动内存管理)详解

    iOS开发之MRC(手动内存管理)详解

    这篇文章主要介绍了 iOS开发之MRC(手动内存管理)详解的相关资料,需要的朋友可以参考下
    2022-08-08
  • iOS UIWebView实现禁止用户复制剪切功能

    iOS UIWebView实现禁止用户复制剪切功能

    这篇文章主要给大家介绍了iOS中的UIWebView如何实现禁止用户复制剪切的功能,文中给出了详细的示例代码,有需要的朋友们可以参考借鉴,下面来一起学习学习吧。
    2016-11-11

最新评论