iOS10修改了部分推送的显示方式以及接受等方式,第三方推送还是比较好用,极光集成:
1,配置环境,首先Xcode开启推送,开始xcode8会自动检测APPID以及证书配置文件:
2,添加库文件,iOS10增加了通知框架:
libresolv.tbd
UserNotifications.framework ,其他:
添加Framework CFNetwork.framework CoreFoundation.framework CoreTelephony.framework SystemConfiguration.framework CoreGraphics.framework Foundation.framework UIKit.framework Security.framework Xcode7需要的是libz.tbd;Xcode7以下版本是libz.dylib Adsupport.framework (获取IDFA需要;如果 使 IDFA,请 要添加) UserNotifications.framework(Xcode8及以上) libresolv.tbd (JPush 2.2.0及以上版本需要)
3,下载JpushSDK,集成到项目中,下载地址:https://pan.baidu.com/s/1jIntAoY
4,集成进去之后,调整好header search和library search路径。
5,Appdelegate.h文件添加头文件,并且遵循协议。
//极光 #import "JPUSHService.h" #import <UserNotifications/UserNotifications.h> @interface AppDelegate : UIResponder <UIApplicationDelegate,JPUSHRegisterDelegate>
6,didFinishLaunchingWithOptions方法注册通知并且启动Jpush做第三方代理:
//极光开始 //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]; } [JPUSHService setupWithOption:launchOptions appKey:JPUSH_KEY channel:@"nuoqiaokeji-station.com" apsForProduction:YES advertisingIdentifier:nil]; //设置tag值,为不同的用户进行不同的推送 if([UserDefaultOperate getNotiTagValue] != nil){ [JPUSHService setTags:[NSSet setWithObject:[UserDefaultOperate getNotiTagValue]] alias:nil fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) { NSLog(@"current noti tag %@",iTags); }]; } //设置小红点为0 [application setApplicationIconBadgeNumber:0]; //极光结束
- (void)applicationWillEnterForeground:(UIApplication *)application { //清空红点 [JPUSHService resetBadge]; [application setApplicationIconBadgeNumber:0]; // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSLog(@"deviceToken - - %@",deviceToken); //极光上传设备ID // Required [JPUSHService registerDeviceToken:deviceToken]; } -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@" 如果app没有运行,会调用这个方法,接收到推送的东西 = == = = =user info %@",userInfo); // 取得 APNs 标准信息内容 NSDictionary *aps = [userInfo valueForKey:@"aps"]; NSString *content = [aps valueForKey:@"alert"]; //推送显示的内容 NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge数量 NSString *sound = [aps valueForKey:@"sound"]; //播放的声音 NSLog(@"content =[%@], badge=[%ld], sound=[%@]",content,(long)badge,sound); NSLog(@"userInfos - - -- - %@",userInfo); // Required [JPUSHService handleRemoteNotification:userInfo]; } //通知注册失败回调 -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); } //iOS10之前接受到通知的处理,10以后是下边两个支持方法 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { NSDictionary *aps = [userInfo objectForKey:@"aps"]; if(application.applicationState == UIApplicationStateActive){ //NSLog(@"我在前台"); NSString* pusInfo=[aps objectForKey:@"alert"]; NSLog(@"user info - - - %@",pusInfo); [self alertWithMessage:pusInfo isSucceed:@"温馨提示"]; //app进入时 弹出提示框 弹出是依照以下原则 //当前页面为 正在拍卖的详情是 除了有关代理出价失效显示(alert) 自己出价不显示 其他人出价显示(黑框) //其他页面(alert) 消息内容一样不显示 //这里自己定义view给用户弹出推送 userinfo下边的aps是内容 }else{ //NSLog(@"我在后台点击推送消息进入的应用程序"); //删除服务器红点个数,然后设置清空本地红点个数 [JPUSHService resetBadge]; [application setApplicationIconBadgeNumber:0]; } // IOS 7 Support Required [JPUSHService handleRemoteNotification:userInfo]; completionHandler(UIBackgroundFetchResultNewData); } // iOS 10 Support - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger options))completionHandler{ // Required NSDictionary * userInfo = notification.request.content.userInfo; if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class] ]) { [JPUSHService handleRemoteNotification:userInfo]; //前台提示 NSDictionary *aps = [userInfo objectForKey:@"aps"]; NSString* pusInfo=[aps objectForKey:@"alert"]; [self alertWithMessage:pusInfo isSucceed:@"温馨提示"]; } completionHandler(UNNotificationPresentationOptionSound); // 需要执 这个 法,选择 是否提醒 户,有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(); // 系统要求执 这个 法 } //极光结束
7,设置开发环境或者发布环境:
[JPUSHService setupWithOption:launchOptions appKey:JPUSH_KEY channel:@"nuoqiaokeji-station.com" apsForProduction:YES advertisingIdentifier:nil]; //production YES为发布,NO为开发