iOS10正式版发布以后,网上各类适配XCode8以及iOS10的文章满天飞。但对于iOS10适配远程推送的文章却很少。iOS10对于推送的修改仍是很是大的,新增了UserNotifications Framework,今天就结合本身的项目,说一说实际适配的状况。html
1、Capabilities中打开Push Notifications 开关app
在XCode7中这里的开关不打卡,推送也是能够正常使用的,可是在XCode8中,这里的开关必需要打开,否则会报错:ui
Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的受权字符串" UserInfo={NSLocalizedDescription=未找到应用程序的“aps-environment”的受权字符串}代理
打开后会生成entitlements文件,在这里能够设置APS Environment,asp-environment的值为development或者production,表明开发环境或者生产环境。参考官方文档:https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/EnablingLocalAndPushNotifications.html#//apple_ref/doc/uid/TP40011195-CH3-SW2htm
2、推送的注册ip
首先引入UserNotifications Framework,开发
- import <UserNotifications/UserNotifications.h>
iOS10修改了注册推送的方法,这里咱们又要对不一样版本分别进行设置了。在application didFinishLaunchingWithOptions方法中修改之前的推送设置(我只实现了iOS8以上的设置)文档
- if (IOS_VERSION >= 10.0) {
- UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
- center.delegate = self;
- [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
- if (!error) {
- DLog(@"request authorization succeeded!");
- }
- }];
- } else {
- if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
- //IOS8,建立UIUserNotificationSettings,并设置消息的显示类类型
- UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil];
-
- [application registerUserNotificationSettings:notiSettings];
- }
- }
3、UNUserNotificationCenterDelegate代理实现字符串
在iOS10中处理推送消息须要实现UNUserNotificationCenterDelegate的两个方法:get
- - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
-
-
- - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
其中第一个方法为App在前台的时候收到推送执行的回调方法,第二个为App在后台的时候,点击推送信息,进入App后执行的 回调方法。
之前处理推送,信息是在userInfo参数中,而新方法中代表上看并无这个参数,其实咱们同样能够获取到userInfo,以下:
- /// App在前台时候回调
- - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
- NSDictionary *userInfo = notification.request.content.userInfo;
- [self handleRemoteNotificationForcegroundWithUserInfo:userInfo];
- }
-
- /// App在后台时候点击推送调用
- - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
- NSDictionary *userInfo = response.notification.request.content.userInfo;
- [self handleRemoteNotificationBackgroundWithUserInfo:userInfo];
- }
完成上面三个步骤的设置,对于iOS10的推送设置基本就适配了。要想自定义Notification Content或者实现其余NotificationAction请参考其余文章。这里只是作了对iOS10的适配。