iOS10下,远程推送发生了变化,新增的UserNotifications.framework
将本地推送和远程推送整合在一块儿。 最近刚刚更新了Xcode8,自动建立证书和配置文件确实方便许多。琰君分享下以开发环境为例(非生产环境)在Xcode8下,快速集成极光推送并兼顾iOS10。ios
注意:极光推送的远程推送是免费的,但相应的用户统计,终端统计等功能是难免费。若是你须要用户统计,终端统计这些功能意味着须要付费才能使用。服务器
开发证书配置
APNs 推送原理及问题
iOS 推送全解析,你不可不知的全部 Tips!app
接下来假设你都了解证书相关的,推送的一些概念和极光推送的知识。那么相信你应该知道须要一台iOS设备和一个苹果开发者账号才能去体验远程推送。若是你清楚远程推送的原理,那能够直接从如下第2步开始。函数
App ID
/证书
/配置文件
1. 远程推送原理测试
当iOS设备联网的状况下,苹果服务器和iOS设备创建了一个长连接,即使应用处于挂起和后台的状态,苹果能够给iOS设备中的应用发送通知。fetch
远程推送原理网站
概念ui
- iOS:iOS设备
- APNS Server:苹果服务器
- Your App: 本身的应用,例如咕咚
- Your Server:本身的服务器,例如咕咚服务器
推送流程加密
UDID
和应用的Bundle ID
到苹果服务器。UDID
和Bundle ID
加密生成一个deviceToken
,并返回给对应iOS应用。deviceToken
发送到本身的服务器,服务器并保存。deviceToken
,将消息和deviceToken
一块儿发送给苹果服务器。deviceToken
找到对应设备下的对应应用,推送消息。说明spa
- 咱们的应用真机测试须要建立对应App ID/证书/配置文件, 这步能够用Xcode8 自动建立,而不须要到苹果开发者平台去建立。若是不是Xcode8,则须要去开发者平台申请,而且在Xcode配置,保证真机测试便可。
- 咱们本身的服务器须要配置 APNs 推送证书,一样的分为开发证书和生产证书。须要到苹果开发者平台去申请。
- 若是不借助第三方推送平台,咱们须要完成推送流程中的1,3,4步。借助极光推送,那么咱们只须要完成推送流程中的1,3步,但一样须要咱们申请APNs 推送证书,而且上传到极光推送平台。
2. Xcode8配置自动建立App ID/证书/配置文件
开启远程推送
2.勾选自动管理App ID
/证书
/配置文件
勾选自动管理
3.检查Xcode生产的App ID
/证书
/配置文件
这步配置好了就能够真机运行了。
检查Xcode生产的App ID/证书/配置文件
3. 建立APNs 推送证书
点击按钮建立证书
2.选择开发环境APNs 推送证书
点击右下角按钮建立
建立开发环境APNs 推送证书
3.选择工程的App ID
选择工程的App ID
4.上传证书请求文件,不知道的童鞋可参照开发证书配置
上传证书请求文件
5.点击下载证书,并双击安装到钥匙串.
安装APNs 推送证书
6.打开钥匙串,并参照下图指示,找到对应的APNs 推送证书,并选择右键导出.
钥匙串导出证书
7.设置证书名字,并选择证书存放位置。
设置证书名字
8.设置证书密码,可不填。极光推送支持证书设置密码,但像leanCloud
推送不支持证书设置密码,各位童鞋可按照实际状况来。最后输入电脑开密码,导出证书。
设置证书密码
输入电脑开机密码导出证书
4. 在极光推送后台建立应用,并上传APNs 推送证书
1.登陆极光推送平台,切换到控制台,并建立应用。接下来在应用信息中上传开发APNs 推送证书
。生产环境APNs 推送证书
配置相似,不作赘述.
上传开发APNs 推送证书
Bundle ID
等信息是否正确.核对Bundle ID 等信息
5. 项目中集成极光推送SDK
jpush-ios-2.1.9.a JPUSHService.h
CFNetwork.framework CoreFoundation.framework CoreTelephony.framework SystemConfiguration.framework CoreGraphics.framework Foundation.framework UIKit.framework Security.framework libz.tbd Adsupport.framework (获取IDFA须要;若是不使用IDFA,请不要添加) UserNotifications.framework(Xcode8及以上)
AppDelegate.m 配置
导入如下头文件
#import "JPUSHService.h" #import <AdSupport/AdSupport.h> #ifdef NSFoundationVersionNumber_iOS_9_x_Max #import <UserNotifications/UserNotifications.h> #endif
而且遵照<JPUSHRegisterDelegate>
协议
@interface AppDelegate ()<JPUSHRegisterDelegate> @end
注册远程推送
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) { //iOS10以上 JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init]; entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound; [JPUSHService registerForRemoteNotificationConfig:entity delegate:self]; }else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { //iOS8以上能够添加自定义categories [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil]; } else { //iOS8如下categories 必须为nil [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil]; } BOOL isProduction = NO;// NO为开发环境,YES为生产环境 //广告标识符 NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; //Required(2.1.5版本的SDK新增的注册方法,改为可上报IDFA,若是没有使用IDFA直接传nil [JPUSHService setupWithOption:launchOptions appKey:@"极光推送AppKey" channel:nil apsForProduction:isProduction advertisingIdentifier:advertisingId]; return YES; }
获得苹果服务器返回的deviceToken
,上传到极光推送服务器。
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ [JPUSHService registerDeviceToken:deviceToken]; }
注册远程通知失败,好比没有联网的状态下。
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{ NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); }
6.获取 APNs(通知) 推送内容
iOS 设备收到一条推送(APNs),用户点击推送通知打开应用时,应用程序根据状态不一样进行处理需在 AppDelegate 中的如下方法中添加代码以获取apn内容
1.若是 App 状态为未运行,此函数将被调用,若是launchOptions包含UIApplicationLaunchOptionsRemoteNotificationKey表示用户点击apn 通知致使app被启动运行;若是不含有对应键值则表示 App 不是因点击apn而被启动,可能为直接点击icon被启动或其余。
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ // apn 内容获取: NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey] }
2.基于iOS 6 及如下的系统版本,若是 App状态为正在前台或者点击通知栏的通知消息,那么此函数将被调用,而且可经过AppDelegate的applicationState是否为UIApplicationStateActive判断程序是否在前台运行。
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ }
3.基于iOS 7 及以上的系统版本,若是是使用 iOS 7 的 Remote Notification 特性那么此函数将被调用
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler: (void (^)(UIBackgroundFetchResult))completionHandler { [JPUSHService handleRemoteNotification:userInfo]; NSLog(@"iOS7及以上系统,收到通知:%@", [self logDic:userInfo]); completionHandler(UIBackgroundFetchResultNewData); } -(NSString *)logDic:(NSDictionary *)dic { if (![dic count]) { return nil; } 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:tempData mutabilityOption:NSPropertyListImmutable format:NULL errorDescription:NULL]; return str; }
4.基于iOS 10及以上的系统版本,
原[application: didReceiveRemoteNotification:]
将会被系统废弃,
由新增UserNotifications.framework
中的如下两个方法替代。
[UNUserNotificationCenterDelegate willPresentNotification:withCompletionHandler:] [UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:]
在极光推送SDK2.1.9版本之后可实现SDK封装的JPUSHRegisterDelegate协议方法,适配iOS10新增的delegate协议方法。
即如下两个方法:
#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.userInfo; UNNotificationRequest *request = notification.request; // 收到推送的请求 UNNotificationContent *content = request.content; // 收到推送的消息内容 NSNumber *badge = content.badge; // 推送消息的角标 NSString *body = content.body; // 推送消息体 UNNotificationSound *sound = content.sound; // 推送消息的声音 NSString *subtitle = content.subtitle; // 推送消息的副标题 NSString *title = content.title; // 推送消息的标题 if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { [JPUSHService handleRemoteNotification:userInfo]; NSLog(@"iOS10 前台收到远程通知:%@", [self logDic: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; UNNotificationRequest *request = response.notification.request; // 收到推送的请求 UNNotificationContent *content = request.content; // 收到推送的消息内容 NSNumber *badge = content.badge; // 推送消息的角标 NSString *body = content.body; // 推送消息体 UNNotificationSound *sound = content.sound; // 推送消息的声音 NSString *subtitle = content.subtitle; // 推送消息的副标题 NSString *title = content.title; // 推送消息的标题 if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { [JPUSHService handleRemoteNotification:userInfo]; NSLog(@"iOS10 收到远程通知:%@", [self logDic:userInfo]); } completionHandler(); // 系统要求执行这个方法 } #endif
7. 极光推送后台发送远程推送测试
1.登陆极光推送
,切换到控制台,并点击对应的应用,点击推送按钮
点击推送按钮
点击发送通知
2.设置推送内容
设置推送内容
选择推送环境iOS开发环境,目标人群,发送时间。再点击可选设置设置消息的具体内容。
屏幕快照
3.设置消息具体内容。远程推送分为普通推送/后台推送/静默推送3种类型,而且类型由推送消息设置来决定。关于如何设置请参考iOS 推送全解析,你不可不知的全部 Tips!
屏幕快照 2016-09-26 上午10.54.59.png
4.iOS设备接收到远程推送
接收到远程推送
5.推送历史能够在这里看获得,但有延迟,可能远程推送已接收到,推送历史数据尚未更新到最新。
屏幕快照 2016-09-28 上午10.23.16.png
关于Xcode8下集成极光远程推送的简单介绍,到这里就结束了。
文/赵先生Try(简书做者) 原文连接:http://www.jianshu.com/p/53e0244e6081 著做权归做者全部,转载请联系做者得到受权,并标注“简书做者”。