極光推送(別名)

記錄下極光推送的集成點(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];
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 初始化 這里就不在說明如何初始化極光推送了,附上極光官方文檔極光官方文檔。 開發(fā)環(huán)境和生產(chǎn)環(huán)境區(qū)別 這里的解釋下,...
    RocketsChen閱讀 9,268評論 0 0
  • 1.使用別名,光網(wǎng)中進(jìn)行別名推送——(下面會有字段設(shè)置),這跟后臺一樣的處理,所以完成不需要管后臺如何推,使用這就...
    武漢劉德華閱讀 14,763評論 0 6
  • 推送技術(shù)產(chǎn)生場景: --服務(wù)器端主動性: 客戶端與服務(wù)器交互都是客戶端主動的, 服務(wù)器一般不能主動與客戶端進(jìn)行數(shù)據(jù)...
    原軍鋒閱讀 35,209評論 4 60
  • 版本記錄 前言 ??現(xiàn)在很多APP都有推送功能,其中極光推送就是很多APP的首選。我們最近的幾個APP也是用的極光...
    刀客傳奇閱讀 8,678評論 0 8
  • 都是些舊文了,冬日里拿出來看看,聊以慰藉。 一個有些寒冷的冬日,一個溫暖的冬日,感謝你,來過,感謝你,一直在。 P...
    夏玖寒閱讀 650評論 0 1

友情鏈接更多精彩內(nèi)容