記錄下極光推送的集成點(diǎn)滴
初始化工作
// Optional
// 獲取IDFA
// 如需使用IDFA功能請?zhí)砑哟舜a并在初始化方法的advertisingIdentifier參數(shù)中填寫對應(yīng)值
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新增的注冊方法,改成可上報(bào)IDFA,如果沒有使用IDFA直接傳nil
// 如需繼續(xù)使用pushConfig.plist文件聲明appKey等配置內(nèi)容,請依舊使用[JPUSHService setupWithOption:launchOptions]方式初始化。
[JPUSHService setupWithOption:launchOptions appKey:APPKEY
channel:@"test.app"
apsForProduction:production
advertisingIdentifier:advertisingId];
監(jiān)聽極光是否登錄成功,只有登錄成功才能注冊別名
-(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(@"設(shè)置成功!");
}
}];
}
#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];
}
// 需要執(zhí)行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以選擇設(shè)置
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(); // 系統(tǒng)要求執(zhí)行這個方法
}
// AppDelegate里的方法,可以另外弄一個擴(kuò)展類
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"推送內(nèi)容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];
}
//關(guān)閉推送
[[UIApplication sharedApplication] unregisterForRemoteNotifications];
//用上面的方法關(guān)閉推送后,開啟推送方法
[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(@"設(shè)置成功!");
}
}];
}
需要注意的地方
// 需要在applicationWillResignActive方法里設(shè)置 badge,這個方法是從前臺回到后臺時調(diào)用,如果不調(diào)用,桌面上icon的數(shù)字會一直加,因?yàn)榉?wù)器推送是默認(rèn)badge+1的,如果不設(shè)置,會一直加下去,除非服務(wù)器設(shè)置badge
- (void)applicationWillResignActive:(UIApplication *)application {
NSInteger badge = 100;
[JPUSHService setBadge:badge];
}