[iOS开发]极光推送相关

最近公司产品需求,在项目里面集成极光推送,配合后台,实现特定用户,分组用户和全部用户的推送。之前一直都是接手别人的项目,这些基础服务都已经集成好了,这次算是第一次正式接触推送,全当是给自己做笔记了。

  • 首先当然是去极光官网创建app获取到AppKey,然后上传推送证书到极光,证书不会的同学可以去极光这边查看配置证书的详细方法点我查看配置证书
  • 配置好完之后就是下载极光的SDK,使用cocoapods的同学可以通过cocoapods导入,然后添加依赖库。Xcode8以上需要开启Application Target的Capabilities->Push Notifications选项,Xcode7以上需要在plist中配置允许http传输,这个一般都有配置就不多说。
  • 现在就是在项目里面集成,首先是导入头文件
// 引入JPush功能所需头文件
#import "JPUSHService.h"
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import 
#endif

然后在app的启动方法里面添加初始化代码

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

//你的其他代码



    BOOL isPushProduction = YES;
#ifdef DEBUG
    isPushProduction = NO;
#endif
    
    //Required 极光推送
    //notice: 3.0.0及以后版本注册可以这样写,也可以继续用之前的注册方式
        JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
        entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
            // 可以添加自定义categories
            // NSSet *categories for iOS10 or later
            // NSSet *categories for iOS8 and iOS9
        }
        [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
        [JPUSHService setupWithOption:launchOptions appKey:@"808b05619bc76f8b083c1f8a"
                              channel:@"appStore"
                     apsForProduction:isPushProduction
                advertisingIdentifier:nil];

注册成功之后,会在这个方法里面回调DeviceToken

- (void)application:(UIApplication *)app
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{

//一开始我没看APi,就网上找的方法把deviceToken转化成字符串,把这个当registrationID传给后台,后来发现是错误的,正确方法看下面
//    NSString *deviceTokenString = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"withString:@""]
//                                     stringByReplacingOccurrencesOfString:@">" withString:@""]
//                                    stringByReplacingOccurrencesOfString:@" " withString:@""];
    /// Required - 注册 DeviceToken
    [JPUSHService registerDeviceToken:deviceToken];
    //取得registrationID保存下来,之后和用户标示一起发给后台,极光那边的API还有一个方法+(NSString *)registrationID可以获的,但是建议用下面这个
    [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        [[NSUserDefaults standardUserDefaults] setObject:registrationID forKey:@"deviceTokenString"];
    }];
}

之后就是收到推送的处理

//iOS7及以上接受到通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{       
    // Required, iOS 7 Support
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
    
    if (application.applicationState == UIApplicationStateActive) {
        //iOS7及以上iOS10以下app在前台运行时接受到推送消息的处理回调

    }else{
        //iOS7及以上iOS10以下app在后台运行时或未启动时点击推送消息后的处理回调
        [self handleNotificationActionWithInfo:userInfo];
    }

    //说明:iOS10以下app在前台的时候,接受到消息不会显示在消息栏,也没有向下弹出的通知,iOS10可以自己设置,下面有说明,如果在iOS10以下需要app在前台时有向下的通知需要自定义。
}

// iOS 10 app处于前台时接受到消息
- (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 app在后台或未激活时点击推送处理事件
- (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];
    }
    [self handleNotificationActionWithInfo:userInfo];
    completionHandler();  // 系统要求执行这个方法
}

- (void)handleNotificationActionWithInfo:(NSDictionary *)info{
    //你要进行的处理,跳转页面还是怎样,info里面时后台传过来的字段
}
  • 这样就可以实现极光推送的基本功能,然后就是配合后台实现分组推送和特定用户推送。
//我们这边是用户登陆或者切换账号之后,将用户的ID和之前保存的registrationId一起发给后台,后台就能通过用户id找到极光那边对应的用户进行推送
NSMutableDictionary * param = [NSMutableDictionary dictionary];
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"][@"ID"] != nil) {
        [param setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"][@"ID"] forKey:@"id"];
    }
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"deviceTokenString"] != nil) {
        [param setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"deviceTokenString"] forKey:@"registrationId"];
    }    
    //将数据传给后台,这是我们的请求,可根据不同情况自行处理  
    [XYYHttpApi postRequestWithUrl:[XYYUrl stringByAppendingString:@"bindJpush"] Param:param IsNeedSign:YES SuccessBlock:^(NSDictionary *resultData) {
        block(YES,nil,resultData);
    } FailureBlock:^(NSDictionary *resultData) {
        block(NO,resultData[@"msg"],nil);
    }];

我们这边有一个分区域推送的功能,我们是给用户打tags,不同区域tags不同,后台可以通过tags去进行分区域推送

//dealDataArr为后台传给我的标示数组,这边一定要是字符串的数组,开始后台给我的不是字符串类型,导致一直推送失败,后来找到原因就是这里不是字符串的问题
 NSSet * set = [[NSSet alloc] initWithArray:dealDataArr];
 [JPUSHService setTags:set completion:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
                            
  } seq:0];
//这个在用户登陆之后向后台请求该用户的tags之后传给极光,用户退出登陆之后清空掉
[JPUSHService cleanTags:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
        
    } seq:0];
  • 以上差不多就是集成极光推送的过程,有文档说明很简单,但是细节方面有若干坑,仅此记录一下,提醒自己

你可能感兴趣的:([iOS开发]极光推送相关)