极光推送

1、注册极光账号,创建应用,再根据文档,到苹果开发者中心,创建推送证书,导出成p12,上传到极光平台。(这些极光推送文档有步骤)
2、使用cocoapods导入JPush
pod 'JPush', '~> 3.0.8'
3、在AppDelegate中先引入头文件

#import "JPUSHService.h"
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import 
#endif
// 如果需要使用idfa功能所需要引入的头文件(可选)
#import 
#pragma mark 极光推送
    //notice: 3.0.0及以后版本注册可以这样写,也可以继续用之前的注册方式
    NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
    }
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
#pragma mark 这个位置在发布的时候要改成生产环境
    [JPUSHService setupWithOption:launchOptions appKey:@"a7e9c964f098c6f858d81025" channel:@"AppStore" apsForProduction:NO advertisingIdentifier:advertisingId];
    [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        if(resCode == 0){
            NSLog(@"registrationID获取成功:%@",registrationID);
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            [user setObject:registrationID forKey:@"registrationID"];
            [user synchronize];
        }
        else{
            NSLog(@"registrationID获取失败,code:%d",resCode);
        }
    }];
    if (launchOptions) {
        NSDictionary * remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (remoteNotification) {
            [self goToMssageViewControllerWith:remoteNotification];
        }
    }
#pragma mark 推送
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [JPUSHService registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    //Optional
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
#pragma mark- JPUSHRegisterDelegate

- (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 {
    NSLog(@"推送消息===%@",userInfo);
    // Required, iOS 7 Support
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSLog(@"推送消息===%@",userInfo);
    application.applicationIconBadgeNumber = 0;
    [self goToMssageViewControllerWith:userInfo];
    [JPUSHService handleRemoteNotification:userInfo];
    
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
    //即将进入前台,图标上的未读消息清零
    [application setApplicationIconBadgeNumber:0];
    [application cancelAllLocalNotifications];
    
}
- (void)goToMssageViewControllerWith:(NSDictionary *)dic{
    NSLog(@"%@",dic);
}

4、在登录成功之后,取用户的一个标识作为alias,一般用userId或者userNumber。

[JPUSHService setAlias:model.userNumber completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                NSLog(@"%@",iAlias);
                if (iResCode == 0) {
                    NSLog(@"添加别名成功");
                }
            } seq:1];

5、在用户退出登录的时候,删除别名

[JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                if (iResCode == 0) {
                    NSLog(@"删除别名成功");
                }
            } seq:1];

你可能感兴趣的:(极光推送)