一般的APP都会有推送功能,来增加用户粘性,而极光的JPush是个不错的三方。
我用cocoaPods安装 pod 'JPush'
然后在AppDelegate.m文件里
#import "AppDelegate.h" // 引入JPush功能所需头文件 #import "JPUSHService.h" // iOS10注册APNs所需头文件 #ifdef NSFoundationVersionNumber_iOS_9_x_Max #import <UserNotifications/UserNotifications.h> #endif @interface AppDelegate ()<JPUSHRegisterDelegate> @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;//电池栏 /* * 添加极光 */ [self addJPUSHRegister:launchOptions]; return YES; } -(void)addJPUSHRegister:(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<UNNotificationCategory *> *categories for iOS10 or later // NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9 } [JPUSHService registerForRemoteNotificationConfig:entity delegate:self]; // Required // init Push // notice: 2.1.5版本的SDK新增的注册方法,改成可上报IDFA,如果没有使用IDFA直接传nil // 如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。 [JPUSHService setupWithOption:launchOptions appKey:@"7937fbbb35fc010bad2f11a7" channel:nil apsForProduction:NO advertisingIdentifier:nil]; } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { //注册APNs成功并上报DeviceToken /// Required - 注册 DeviceToken [JPUSHService registerDeviceToken:deviceToken]; } - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { //Optional实现注册APNs失败接口(可选) NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); } #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(JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound); // 需要执行这个方法,选择是否提醒用户,有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]; } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.(App即将进入前台)中将小红点清除 NSLog(@"进入前台"); [UIApplication sharedApplication].applicationIconBadgeNumber = 0; } 还有就是::: //注册极光别名和标签 NSSet *tags =[NSSet setWithObjects:@"old",@"all",@"new", nil]; [JPUSHService setTags:tags alias:@"userID" fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) { }];