推送通知-极光推送

1:在极光开发者服务创建应用应用名称应用图标APNS开发证书(p12文件)(开发证书密码:-安装开发者证书到钥匙串-钥匙串导出p12文件-自定义密码):制作APNS开发证书的流程:a/创建AppID :确定哪一个app支持远程推送功能Development -- Apple Push NostificationApp ID Description :name(不能有中文)Explicit App ID (准确的AppID--BundleID )--Xcode中的Bundle IdentifierEnable Serives中设置push Notification--Enablecontinue -- 显示该AppID 中push Notifications -- configure状态点击edit--选中Push notification 创建开发证书配置开发证书需要CSR文件上传打开钥匙串--生成CSR文件continue--下载生成开发证书双击安装开发证书APNS生产证书(p12文件)(开发证书密码:-安装开发者证书到钥匙串-钥匙串导出p12文件-自定义密码)制作APNS生产证书的流程:a/创建AppID :确定哪一个app支持远程推送功能Prodution -- Apple Push Nostificationcontinue -- 选择AppID(确定哪个App生生产证书)continue--上传CSR文件continue-- 下载生成生产证书双击安装生产证书点击保存:生成一个App Key还需配置真机调试(另见详情)2.配置工程(1)导入SDKa/将SDK包解压,在Xcode中选择“Add files to 'Your project name'...”,将解压后的lib子文件夹(包含JPUSHService.h、jpush-ios-x.x.x.a,jcore-ios-x.x.x.a)添加到你的工程目录中。b/添加FrameworkCFNetwork.frameworkCoreFoundation.frameworkCoreTelephony.frameworkSystemConfiguration.frameworkCoreGraphics.frameworkFoundation.frameworkUIKit.frameworkSecurity.frameworklibz.tbd (Xcode7以下版本是libz.dylib)AdSupport.framework (获取IDFA需要;如果不使用IDFA,请不要添加)UserNotifications.framework (Xcode8及以上)libresolv.tbd (JPush 2.2.0及以上版本需要, Xcode7以下版本是libresolv.dylib)b(2)Build Settings如果你的工程需要支持小于7.0的iOS系统,请到Build Settings 关闭 bitCode 选项,否则将无法正常编译通过。 设置 Search Paths 下的 User Header Search Paths 和 Library Search Paths,比如SDK文件夹(默认为lib)与工程文件在同一级目录下,则都设置为"$(SRCROOT)/{静态库所在文件夹名称}"即可。(3)Capabilities如使用Xcode8及以上环境开发,请开启Application Target的Capabilities->Push Notifications选项(4)允许Xcode7支持Http传输方法  如果您使用的是2.1.9及以上的版本则不需要配置此步骤如果用的是Xcode7或更新版本,需要在App项目的plist手动配置下key和值以支持http传输:选择1:根据域名配置在项目的info.plist中添加一个Key:NSAppTransportSecurity,类型为字典类型。然后给它添加一个NSExceptionDomains,类型为字典类型;把需要的支持的域添加給NSExceptionDomains。其中jpush.cn作为Key,类型为字典类型。每个域下面需要设置2个属性:NSIncludesSubdomains、NSExceptionAllowsInsecureHTTPLoads。 两个属性均为Boolean类型,值分别为YES、YES。选择2:全局配置NSAppTransportSecurityNSAllowsArbitraryLoads3.添加头文件以下代码添加到 AppDelegate.m 引用头文件的位置// 引入JPush功能所需头文件#import "JPUSHService.h"// iOS10注册APNs所需头文件#ifdef NSFoundationVersionNumber_iOS_9_x_Max#import#endif// 如果需要使用idfa功能所需要引入的头文件(可选)#import4.添加Delegate为AppDelegate添加Delegate。参考代码:@interface AppDelegate ()@end5.添加初始化代码1.0版本开始,API类名为JPUSHService,不再使用原先的APService。添加初始化APNs代码请将以下代码添加到-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  //Required  //notice: 3.0.0及以后版本注册可以这样写,也可以继续用之前的注册方式  JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];  entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;  if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {    // 可以添加自定义categories    // NSSet*categories for iOS10 or later    // NSSet*categories for iOS8 and iOS9

}

[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];

添加初始化JPush代码

请将以下代码添加到

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

// Optional

// 获取IDFA

// 如需使用IDFA功能请添加此代码并在初始化方法的advertisingIdentifier参数中填写对应值

NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];

// Required

// init Push

// notice: 2.1.5版本的SDK新增的注册方法,改成可上报IDFA,如果没有使用IDFA直接传nil

// 如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。

[JPUSHService setupWithOption:launchOptions appKey:@"" channel:@"App Store"];

注册APNs成功并上报DeviceToken

请在AppDelegate.m实现该回调方法并添加回调方法中的代码

- (void)application:(UIApplication *)application

didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

/// Required - 注册 DeviceToken

[JPUSHService registerDeviceToken:deviceToken];

}

添加处理APNs通知回调方法

请在AppDelegate.m实现该回调方法并添加回调方法中的代码

#pragma mark- JPUSHRegisterDelegate

// iOS 10 Support

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {

// Required

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

if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {

[JPUSHService handleRemoteNotification:userInfo];

}

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

}

// iOS 10 Support

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {

// Required

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

if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {

[JPUSHService handleRemoteNotification:userInfo];

}

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

}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

// Required, iOS 7 Support

[JPUSHService handleRemoteNotification:userInfo];

completionHandler(UIBackgroundFetchResultNewData);

}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

// Required,For systems with less than or equal to iOS6

[JPUSHService handleRemoteNotification:userInfo];

}

你可能感兴趣的:(推送通知-极光推送)