我更新了xcode之后xcode8 极光推送送就爆红 是什么情况

1279人阅读
iOS10发布后,发现项目中的极光推送接收消息异常了。
查了相关资料后才发现,iOS10中对于通知做了不少改变。同时也发现极光也很快更新了对应的SDK。
现在就把适配修改的做法分享一下,希望对有需要的童鞋有所帮助。
具体做法如下:
注意:必须先***Xcode8.0版本。
一、添加相关的SKD,或framework文件
1、添加UserNotification.framework
2、更新jpush的SDK(最新版本:jpush-ios-2.1.9.a)
二、进行路径和消息推送的配置
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&
2、启动极光推送功能
static NSString *JPushAppKey
= @&6abc87b33b23d35b9c3b86e0&;
static NSString *JPushChannel = @&Publish channel&;
// static BOOL JPushIsProduction = NO;
#ifdef DEBUG
// 开发 极光FALSE为开发环境
static BOOL const JPushIsProduction = FALSE;
// 生产 极光TRUE为生产环境
static BOOL const JPushIsProduction = TRUE;
- (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];
else if ([[UIDevice currentDevice].systemVersion floatValue] &= 8.0)
// categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)
categories:nil];
// 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(@®istrationID获取成功:%@&,registrationID);
[[NSUserDefaults standardUserDefaults] setObject:registrationID forKey:@®istrationID&];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@®istrationID获取失败,code:%d&,resCode);
return YES;
- (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);
- (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.userI
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.userI
if ([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])
[JPUSHService handleRemoteNotification:userInfo];
completionHandler();
// 系统要求执行这个方法
7、其他注意事项
为了保证用户能正常接收,或有针对性的接收通知,登录成功后(或退出后)需要设置别名、标记。通常都是该逻辑都是写在用户登录APP成功之后,或者是用户退出当前登录状态后。
/// 绑定别名(注意:1 登录成功或者自动登录后;2 去除绑定-退出登录后)
+ (void)JPushTagsAndAliasInbackgroundTags:(NSSet *)set alias:(NSString *)name
// 标签分组(表示没有值)
NSSet *tags =
// 用户别名(自定义值,nil是表示没有值)
NSString *alias =
NSLog(@&tags = %@, alias = %@(registrationID = %@)&, tags, alias, [self registrationID]);
// tags、alias均无值时表示去除绑定
[JPUSHService setTags:tags aliasInbackground:alias];
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:128643次
积分:5264
积分:5264
排名:第3719名
原创:393篇
转载:170篇
评论:17条
(13)(62)(25)(15)(28)(39)(63)(3)(4)(3)(1)(3)(1)(1)(4)(6)(39)(1)(28)(37)(3)(4)(3)(6)(11)(13)(39)(31)(78)&&国之画&&&& &&&&&&
&& &&&&&&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!Xcode8之极光推送 - 简书
下载简书移动应用
写了1586字,被0人关注,获得了0个喜欢
Xcode8之极光推送
好久没做推送了,最近越到了一些坑,但顺利搞定,简单说下步骤一. 首先简单说下***(注意所以推送相关***尽量用项目名来命名,bundleid与工程保持一致,命名注意区分测试和发布***)1. 首先要有一个99$的苹果开发者账号,新建开发者***(此处就省略了)2. 创建appID,要勾选Push Notifications,bundleid要与工程保持一致(Xcode8在工程中选择对应的team后会自动在该账号上创建一个appid,如图),
3. 添加调试设备
iTunes上可获取设备UUID
4. 分别新建测试和发布的推送***,下载后双击导入
5. 新建描述文件,也是测试和发布一对
6. 打开钥匙串会发现有一对你刚才导入的推送***,右键到处为p12文件,注意区分测试和发布***,注意自己设置的密码
7. 打开极光开发者服务(有个人账号),创建应用,应用名就是自己开发项目的名字,上传应用图标,在***的地方上传刚刚导出的两个p12文件,bundleid与工程一致,然后点击创建应用,创建成功后如图
8. 记录,appkey是写在工程里,Master 是要给服务器的
二、配置工程1. 首先下载极光的SDK并把需要的导入到工程
2.导入依赖库
3. Xcode8 要在这里打开开关,都为对号则表示正常,***选自动
4.在appdelegate中倒入头文件
注意打开允许http请求的开关然后上代码static NSString *appKey = @"这里填极光上面的appkey";static NSString *channel = @"Publish channel";static BOOL isProduction = FALSE;接受协议&JPUSHRegisterDelegate&- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {/*......*/#pragma mark -- 远程推送NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];//Requiredif ([[UIDevice currentDevice].systemVersion floatValue] &= 10.0) {JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionS[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];}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];}//如不需要使用IDFA,advertisingIdentifier 可为nil[JPUSHService setupWithOption:launchOptions appKey:appKeychannel:channelapsForProduction:isProductionadvertisingIdentifier:nil];//2.1.9版本新增获取registration id block接口。[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {if(resCode == 0){NSLog(@"registrationID获取成功:%@",registrationID);}else{NSLog(@"registrationID获取失败,code:%d",resCode);}}];// Override point for customization after application launch.return YES;}#pragma mark - 获取 device Token- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {// 告诉服务器 服务器保存[JPUSHService registerDeviceToken:deviceToken];NSLog(@"%@", deviceToken);}#pragma mark - 获取信息/***
当收到远程推送通知时调用**
@param application 应用程序*
@param userInfo
具体内容*///注册失败- (void)application:(UIApplication *)applicationdidFailToRegisterForRemoteNotificationsWithError:(NSError *)error {NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);}#if __IPHONE_OS_VERSION_MAX_ALLOWED & __IPHONE_7_1- (void)application:(UIApplication *)applicationdidRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {}// Called when your app has been activated by the user selecting an action from// a local notification.// A nil action identifier indicates the default action.// You should call the completion handler as soon as you've finished handling// the action.- (void)application:(UIApplication *)applicationhandleActionWithIdentifier:(NSString *)identifierforLocalNotification:(UILocalNotification *)notificationcompletionHandler:(void (^)())completionHandler {}// Called when your app has been activated by the user selecting an action from// a remote notification.// A nil action identifier indicates the default action.// You should call the completion handler as soon as you've finished handling// the action.- (void)application:(UIApplication *)applicationhandleActionWithIdentifier:(NSString *)identifierforRemoteNotification:(NSDictionary *)userInfocompletionHandler:(void (^)())completionHandler {}#endif// 收到通知- (void)application:(UIApplication *)applicationdidReceiveRemoteNotification:(NSDictionary *)userInfofetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {[JPUSHService handleRemoteNotification:userInfo];//
NSLog(@"iOS7及以上系统,收到通知:%@", [self logDic:userInfo]);if ([[UIDevice currentDevice].systemVersion floatValue]&10.0 || application.applicationState&0) {//
[rootViewController addNotificationCount];}completionHandler(UIBackgroundFetchResultNewData);}- (void)application:(UIApplication *)applicationdidReceiveLocalNotification:(UILocalNotification *)notification {[JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];}#ifdef NSFoundationVersionNumber_iOS_9_x_Max#pragma mark- JPUSHRegisterDelegate- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {NSDictionary * userInfo = notification.request.content.userIUNNotificationRequest *request = notification. // 收到推送的请求UNNotificationContent *content = request. // 收到推送的消息内容NSNumber *badge = content.
// 推送消息的角标NSString *body = content.
// 推送消息体UNNotificationSound *sound = content.
// 推送消息的声音NSString *subtitle = content.
// 推送消息的副标题NSString *title = content.
// 推送消息的标题if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {[JPUSHService handleRemoteNotification:userInfo];NSLog(@"iOS10 前台收到远程通知:%@", [self logDic:userInfo]);//
[rootViewController addNotificationCount];}else {// 判断为本地通知NSLog(@"iOS10 前台收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);}completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置}- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {NSDictionary * userInfo = response.notification.request.content.userIUNNotificationRequest *request = response.notification. // 收到推送的请求UNNotificationContent *content = request. // 收到推送的消息内容NSNumber *badge = content.
// 推送消息的角标NSString *body = content.
// 推送消息体UNNotificationSound *sound = content.
// 推送消息的声音NSString *subtitle = content.
// 推送消息的副标题NSString *title = content.
// 推送消息的标题if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {[JPUSHService handleRemoteNotification:userInfo];//
NSLog(@"iOS10 收到远程通知:%@", [self logDic:userInfo]);//
[rootViewController addNotificationCount];}else {// 判断为本地通知NSLog(@"iOS10 收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);}completionHandler();
// 系统要求执行这个方法}#endif// if not ,log will be \Uxxx- (NSString *)logDic:(NSDictionary *)dic {if (![dic count]) {}NSString *tempStr1 =[[dic description] stringByReplacingOccurrencesOfString:@"\\u"withString:@"\\U"];NSString *tempStr2 =[tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];NSString *tempStr3 =[[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];NSString *str =[NSPropertyListSerialization propertyListFromData:tempDatamutabilityOption:NSPropertyListImmutableformat:NULLerrorDescription:NULL];}这些代码极光的开发文档里都有然后连接设备真机调试***,在设备提示上选择允许发送通知5. ***完退出应用,打开极光的应用管理页面,选择要推送消息的应用,选择推送
点击立即发送,打开记录,设备会有提示音,弹窗,推送成功
6. 本人只说出自己的成功经验,如有问题请提出指导意见。
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
选择支付方式:iOS开发之推送—极光推送_图文_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
iOS开发之推送—极光推送
|文档简介|
中国移动互联网研发培训专家|
总评分0.0|
i​O​S​开​发​之​推​送​—​极​光​推​送
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
你可能喜欢

参考资料

 

随机推荐