极光推送(别名)

记录下极光推送的集成点滴

初始化工作

// Optional
    // 获取IDFA
    // 如需使用IDFA功能请添加此代码并在初始化方法的advertisingIdentifier参数中填写对应值
    NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];

    BOOL production = NO;
#if DEBUG
    production = NO;
#else
    production = YES;
#endif
    
    // Required
    // init Push
    // notice: 2.1.5版本的SDK新增的注册方法,改成可上报IDFA,如果没有使用IDFA直接传nil
    // 如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。
    [JPUSHService setupWithOption:launchOptions appKey:APPKEY
                          channel:@"test.app"
                 apsForProduction:production
            advertisingIdentifier:advertisingId];

监听极光是否登录成功,只有登录成功才能注册别名

-(void)didLoginNotification{
NSString *alias = @"别名";
[JPUSHService setTags:[NSSet set] alias:alias fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
        NSLog(@"rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, iTags, iAlias);
        if (iResCode == 0) {
            NSLog(@"设置成功!");
        }
    }];
}
#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];
    }
    // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
    completionHandler(UNNotificationPresentationOptionAlert|UNNotificationPresentationOptionSound);
}

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    // Required
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    NSLog(@"userInfo:%@",userInfo);
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler();  // 系统要求执行这个方法
}
// AppDelegate里的方法,可以另外弄一个扩展类
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    NSLog(@"推送内容aps:%@",userInfo[@"aps"]);
    // 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];
}
//关闭推送
[[UIApplication sharedApplication] unregisterForRemoteNotifications];

//用上面的方法关闭推送后,开启推送方法
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeSound |
                                                      UIUserNotificationTypeAlert) categories:nil];
     
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didLoginNotification) name:kJPFNetworkDidLoginNotification object:nil];
-(void)didLoginNotification{
    
    NSString * alias = @"别名";
    
    [JPUSHService setTags:[NSSet set] alias: alias fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
        NSLog(@"rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, iTags, iAlias);
        if (iResCode == 0) {
            NSLog(@"设置成功!");
        }
    }];
}

需要注意的地方

// 需要在applicationWillResignActive方法里设置 badge,这个方法是从前台回到后台时调用,如果不调用,桌面上icon的数字会一直加,因为服务器推送是默认badge+1的,如果不设置,会一直加下去,除非服务器设置badge

- (void)applicationWillResignActive:(UIApplication *)application {
    NSInteger badge = 100;
    [JPUSHService setBadge:badge];
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 初始化 这里就不在说明如何初始化极光推送了,附上极光官方文档极光官方文档。 开发环境和生产环境区别 这里的解释下,...
    RocketsChen阅读 9,080评论 0 0
  • 1.使用别名,光网中进行别名推送——(下面会有字段设置),这跟后台一样的处理,所以完成不需要管后台如何推,使用这就...
    武汉刘德华阅读 14,699评论 0 6
  • 推送技术产生场景: --服务器端主动性: 客户端与服务器交互都是客户端主动的, 服务器一般不能主动与客户端进行数据...
    原军锋阅读 34,817评论 4 60
  • 版本记录 前言   现在很多APP都有推送功能,其中极光推送就是很多APP的首选。我们最近的几个APP也是用的极光...
    刀客传奇阅读 8,530评论 0 8
  • 都是些旧文了,冬日里拿出来看看,聊以慰藉。 一个有些寒冷的冬日,一个温暖的冬日,感谢你,来过,感谢你,一直在。 P...
    夏玖寒阅读 614评论 0 1