兼容iOS 10 极光推送

一、前言

      如果使用的app是调用了极光推送sdk,升级iOS 10系统后,将收不到消息推送,因为iOS 推出了新的通知特性。为了兼容iOS 10系统,需要下载最新的极光推送sdk,下载链接如下:极光推送链接

二、配置工程

导入SDK

将SDK包解压,在Xcode中选择“Add files to 'Your project name'...”,将解压后的lib子文件夹(包含JPUSHService.h、jpush-ios-x.x.x.a)添加到你的工程目录中。

UserNotifications.framework(Xcode8及以上)

Capabilities

如使用Xcode8及以上环境开发,请开启Application Target的Capabilities->Push Notifications选项,如图:


初始化代码

请将以下代码添加到

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

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

//Required

if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {

JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];

entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;

[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];

}

//Required

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

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

[JPUSHService setupWithOption:launchOptions appKey:appKey

channel:channel

apsForProduction:isProduction

advertisingIdentifier:advertisingId];

注册APNs成功并上报DeviceToken

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

- (void)application:(UIApplication *)application

didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

/// Required - 注册 DeviceToken

[JPUSHService registerDeviceToken:deviceToken];

}

实现注册APNs失败接口(可选)

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

//Optional

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

}

添加处理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];

}

添加处理JPush自定义消息回调方法

如需使用JPush的自定义消息功能,请参考文档来实现自定义消息的处理回调方法。

三、成功运行


真机调试该项目,如果控制台输出以下日志则代表您已经集成成功。

2016-09-19 17:12:12.745823 219b28[1443:286814]  | JPUSH | I - [JPUSHLogin]

----- login result -----

uid:5460310207

registrationID:171976fa8a8620a14a4

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容