记录下极光推送的集成点滴
初始化工作
// 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];
}