iOS 10 JPush

苹果在iOS10上对apns推送做了修改, 极光也是很给力的, 在第一时间就对sdk进行了更新, 下面对iOS10注册极光推送进行一下记录.

首先, 在极光的开发者服务里注册应用获取appKey, 在apple Developer配置推送证书...等等等这些废话就不说了.

兼容iOS10的是极光2.1.9版本的sdk.

1. 导入SDK

2. 导入SDK依赖的系统框架

CFNetwork.framework

CoreFoundation.frameworkCoreTelephony.frameworkSystemConfiguration.frameworkCoreGraphics.frameworkFoundation.frameworkUIKit.frameworkSecurity.framework

Xcode7需要的是libz.tbd;Xcode7以下版本是libz.dylib

Adsupport.framework (获取IDFA需要;如果不使用IDFA,请不要添加)

UserNotifications.framework(Xcode8及以上)

3. 设置Build Setting中, Search Paths的User Header Search Paths

4.如果用的是Xcode8及以上环境开发需要开启Application Target的Capabilities->Push Notifications选项

这两个一定要都是对号 ,  这个选项不开启在iOS10后不会注册成功

添加这个选项会在项目中多这样一个文件

5. 不要忘记Xcode7以上需要支持http传输方式

下面是需要写的代码部分:

6. 在AppDelegate.m中, 引入头文件

[objc]view plaincopy

// 极光推送

#import "JPUSHService.h"

#import 

#ifdef NSFoundationVersionNumber_iOS_9_x_Max

#import  // 这里是iOS10需要用到的框架

#endif

7. 设置注册极光推送需要的一些参数

[objc]view plaincopy

staticNSString*constJPUSHAPPKEY =@"xxxxxxxxxxxxxxxxx";// 极光appKey

staticNSString*constchannel =@"Publish channel";// 固定的

#ifdef DEBUG // 开发

staticBOOLconstisProduction = FALSE;// 极光FALSE为开发环境

#else // 生产

staticBOOLconstisProduction = TRUE;// 极光TRUE为生产环境

#endif

8. 这里是AppDelegate.m中的代码, 分了几大块, 全部粘到下面, 直接复制可用(只需要下面这些代码就可以实现通知)

[objc]view plaincopy

//

//  AppDelegate.m

//  iOS10_JPUSH

//

//  Created by 周昊 on 16/9/18.

//  Copyright © 2016年 周昊. All rights reserved.

//

#import "AppDelegate.h"

// 极光推送

#import "JPUSHService.h"

#import 

#ifdef NSFoundationVersionNumber_iOS_9_x_Max

#import  // 这里是iOS10需要用到的框架

#endif

staticNSString*constJPUSHAPPKEY =@"xxxxxxxxxxxxxxxxx";// 极光appKey

staticNSString*constchannel =@"Publish channel";// 固定的

#ifdef DEBUG // 开发

staticBOOLconstisProduction = FALSE;// 极光FALSE为开发环境

#else // 生产

staticBOOLconstisProduction = TRUE;// 极光TRUE为生产环境

#endif

@interfaceAppDelegate ()// 最新版的sdk需要实现这个代理方法

@end

@implementationAppDelegate

- (BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

// 注册apns通知

if([[UIDevicecurrentDevice].systemVersionfloatValue] >=10.0)// iOS10

{

#ifdef NSFoundationVersionNumber_iOS_9_x_Max

JPUSHRegisterEntity* entity = [[JPUSHRegisterEntityalloc]init];

entity.types= UNAuthorizationOptionAlert|UNAuthorizationOptionBadge | UNAuthorizationOptionSound;

[JPUSHServiceregisterForRemoteNotificationConfig:entitydelegate:self];

#endif

}

elseif([[UIDevicecurrentDevice].systemVersionfloatValue] >=8.0)// iOS8, iOS9

{

//可以添加自定义categories

[JPUSHServiceregisterForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)categories:nil];

}

else// iOS7

{

//categories 必须为nil

[JPUSHServiceregisterForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)categories:nil];

}

/*

*  launchingOption 启动参数.

*  appKey 一个JPush 应用必须的,唯一的标识.

*  channel 发布渠道. 可选.

*  isProduction 是否生产环境. 如果为开发状态,设置为 NO; 如果为生产状态,应改为 YES.

*  advertisingIdentifier 广告标识符(IDFA) 如果不需要使用IDFA,传nil.

* 此接口必须在 App 启动时调用, 否则 JPush SDK 将无法正常工作.

*/

// 广告标识符

NSString*advertisingId = [[[ASIdentifierManagersharedManager]advertisingIdentifier]UUIDString];

// 如不需要使用IDFA,advertisingIdentifier 可为nil

// 注册极光推送

[JPUSHServicesetupWithOption:launchOptionsappKey:JPUSHAPPKEYchannel:channelapsForProduction:isProductionadvertisingIdentifier:advertisingId];

//2.1.9版本新增获取registration id block接口。

[JPUSHServiceregistrationIDCompletionHandler:^(intresCode,NSString*registrationID) {

if(resCode ==0)

{

// iOS10获取registrationID放到这里了, 可以存到缓存里, 用来标识用户单独发送推送

NSLog(@"registrationID获取成功:%@",registrationID);

[[NSUserDefaultsstandardUserDefaults]setObject:registrationIDforKey:@"registrationID"];

[[NSUserDefaultsstandardUserDefaults]synchronize];

}

else

{

NSLog(@"registrationID获取失败,code:%d",resCode);

}

}];

returnYES;

}

// ---------------------------------------------------------------------------------

- (void)applicationWillResignActive:(UIApplication*)application {

}

- (void)applicationDidEnterBackground:(UIApplication*)application {

[[UIApplicationsharedApplication]setApplicationIconBadgeNumber:0];

}

- (void)applicationWillEnterForeground:(UIApplication*)application {

[applicationsetApplicationIconBadgeNumber:0];

[applicationcancelAllLocalNotifications];

}

- (void)applicationDidBecomeActive:(UIApplication*)application {

}

- (void)applicationWillTerminate:(UIApplication*)application {

}

// ---------------------------------------------------------------------------------

#pragma mark - 注册推送回调获取 DeviceToken

#pragma mark -- 成功

- (void)application:(UIApplication*)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken

{

// 注册成功

// 极光: Required - 注册 DeviceToken

[JPUSHServiceregisterDeviceToken:deviceToken];

}

#pragma mark -- 失败

- (void)application:(UIApplication*)applicationdidFailToRegisterForRemoteNotificationsWithError:(NSError*)error

{

// 注册失败

NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);

}

// ---------------------------------------------------------------------------------

// 这部分是官方demo里面给的, 也没实现什么功能, 放着以备不时之需

#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*)applicationdidReceiveLocalNotification:(UILocalNotification*)notification

{

[JPUSHServiceshowLocalNotificationAtFront:notificationidentifierKey:nil];

}

// ---------------------------------------------------------------------------------

#pragma mark - iOS7: 收到推送消息调用

- (void)application:(UIApplication*)applicationdidReceiveRemoteNotification:(NSDictionary*)userInfofetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler {

// iOS7之后调用这个

[JPUSHServicehandleRemoteNotification:userInfo];

NSLog(@"iOS7及以上系统,收到通知");

if([[UIDevicecurrentDevice].systemVersionfloatValue] <10.0|| application.applicationState>0)

{

// 程序在前台或通过点击推送进来的会弹这个alert

NSString*message = [NSStringstringWithFormat:@"iOS7-8-9收到的推送%@", [userInfo[@"aps"]objectForKey:@"alert"]];

UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:messagedelegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil,nilnil];

[alertshow];

}

completionHandler(UIBackgroundFetchResultNewData);

}

// ---------------------------------------------------------------------------------

#pragma mark - iOS10: 收到推送消息调用(iOS10是通过Delegate实现的回调)

#pragma mark- JPUSHRegisterDelegate

#ifdef NSFoundationVersionNumber_iOS_9_x_Max

// 当程序在前台时, 收到推送弹出的通知

- (void)jpushNotificationCenter:(UNUserNotificationCenter*)centerwillPresentNotification:(UNNotification*)notificationwithCompletionHandler:(void(^)(NSInteger))completionHandler {

NSDictionary* userInfo = notification.request.content.userInfo;

if([notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]])

{

[JPUSHServicehandleRemoteNotification:userInfo];

NSString*message = [NSStringstringWithFormat:@"will%@", [userInfo[@"aps"]objectForKey:@"alert"]];

NSLog(@"iOS10程序在前台时收到的推送: %@", message);

UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:messagedelegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil,nilnil];

[alertshow];

}

completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);// 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置

}

// 程序关闭后, 通过点击推送弹出的通知

- (void)jpushNotificationCenter:(UNUserNotificationCenter*)centerdidReceiveNotificationResponse:(UNNotificationResponse*)responsewithCompletionHandler:(void(^)())completionHandler {

NSDictionary* userInfo = response.notification.request.content.userInfo;

if([response.notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]])

{

[JPUSHServicehandleRemoteNotification:userInfo];

NSString*message = [NSStringstringWithFormat:@"did%@", [userInfo[@"aps"]objectForKey:@"alert"]];

NSLog(@"iOS10程序关闭后通过点击推送进入程序弹出的通知: %@", message);

UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:messagedelegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil,nilnil];

[alertshow];

}

completionHandler();// 系统要求执行这个方法

}

#endif

@end

注: 极光的AppKey要自己到极光的官网申请哦

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容