iOS初步集成極光推送后你還要做這些事

當(dāng)我們把推送證書配置好再把極光SDK拖入項目配置馋记,然后注冊極光推送,完成代理昵宇,這樣沒有太多意外你就能收到消息了,但是我們都知道還需要做一些處理儿子,都是哪些呢瓦哎?

當(dāng)收到消息時,app在前臺如何處理

在后臺如何處理柔逼?

未啟動如何處理蒋譬?

當(dāng)app在前臺收到消息如何跳轉(zhuǎn)到指定頁面?

在后臺收到系統(tǒng)通知卒落,點擊通知欄又如何跳轉(zhuǎn)指定頁面羡铲?

未啟動時點擊通知欄又如何跳轉(zhuǎn)指定頁面?

收到自定義通知如何顯示儡毕?

怎么給指定用戶發(fā)送消息也切?

當(dāng)有多個類型通知的時候,怎么拿到服務(wù)器發(fā)送過來的extras的內(nèi)容腰湾,然后根據(jù)類型做出正確的跳轉(zhuǎn)雷恃?

怎么在app內(nèi)部開啟、關(guān)閉通知费坊?

你測試包(真機調(diào)試)收到通知后倒槐,怎么確定上架包也能收到消息?

今天小伙伴問我Badge怎么弄附井,我這邊徽章個數(shù)是從服務(wù)器請求的讨越,使用WZLBadge這個三方繪制的两残。另外,我個人遇到的一個崩潰問題 覺得有必要讓大家看下把跨。

在上面我拋出了一些問題人弓,基本都是我們集成極光后前端需要處理的事情,下面我一個個解決這些問題着逐。我個人沒有總結(jié)太多崔赌,肯定有缺失,這里只是給不知道的伙伴列舉一些常見的耸别。若有誤健芭,請指出。

當(dāng)收到消息時秀姐,app在前臺如何處理慈迈?如何跳轉(zhuǎn)?

當(dāng)應(yīng)用在前臺時囊扳,接收到通知消息首先會調(diào)用極光的這個代理

#pragma mark- JPUSHRegisterDelegate
// iOS 10 Support
- (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];
        
    }
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
        // 需要執(zhí) 這個 法吩翻,選擇 是否提醒 戶兜看,有Badge锥咸、Sound、Alert三種類型可以選擇設(shè)置
       completionHandler(UNNotificationPresentationOptionSound); 
    }else if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {//后臺
        // 需要執(zhí) 這個 法细移,選擇 是否提醒 戶搏予,有Badge、Sound弧轧、Alert三種類型可以選擇設(shè)置
        completionHandler(UNNotificationPresentationOptionAlert); 
    }else {//未啟動
        // 需要執(zhí) 這個 法雪侥,選擇 是否提醒 戶,有Badge精绎、Sound速缨、Alert三種類型可以選擇設(shè)置
        completionHandler(UNNotificationPresentationOptionAlert); 
    }
}

在上面代理中有幾個判斷,當(dāng)app在前臺代乃、后臺旬牲、未運行。三種狀態(tài)搁吓,解釋下
Badge:應(yīng)用角標(biāo) Sound:通知聲音 Alert:通知欄 如果你同時需要通知欄展示原茅,聲音,角標(biāo)堕仔,只需要以|符號連接即可擂橘,不需要的直接刪除。

如果你在應(yīng)用內(nèi)收到通知(非自定義消息摩骨,后面會詳細(xì)說這個)通贞,應(yīng)該在這個 if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {}判斷中進行提示朗若、跳轉(zhuǎn)。你可以在這里寫一個UIAlertController提示昌罩,也可以像我一樣提示在頂部捡偏,取決你們UI設(shè)計。
我的代碼

 if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
        
        /*
         *      當(dāng)應(yīng)用在前臺  接到通知
         */
        NSString *messageAlert = [[userInfo objectForKey:@"aps"]objectForKey:@"alert"];

        //獲取頂層控制器
        UIViewController *currentVC = [self currentViewController];
    
        [TSMessage showNotificationInViewController:currentVC.navigationController
                                              title:@"系統(tǒng)通知"
                                           subtitle:messageAlert
                                              image:[UIImage imageNamed:@"notifi"]
                                               type:TSMessageNotificationTypeMessage
                                           duration:TSMessageNotificationDurationAutomatic
                                           callback:^{
                                               if (currentVC.navigationController) {
                                                   if ([Person currentLoginUser].userId.length == 11) {
                                                       //先跳轉(zhuǎn)指定tab然后push到指定頁面
                                                       BaseTabBarController *myTbabar = (BaseTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                                                       myTbabar.selectedIndex = 2;
                                                       //改變bar后再次獲取當(dāng)前控制器
                                                       UIViewController *VC = [self currentViewController];
                                                       MyServiceViewController *vc = [[MyServiceViewController alloc] init];
                                                       [vc setHidesBottomBarWhenPushed:YES];
                                                       [VC.navigationController pushViewController:vc animated:YES];
                                                   }else {
                                                       [SVProgressHUD showInfoWithStatus:@"請登錄后峡迷,前往我的服務(wù)查看银伟。"];
                                                   }
                                               }else {
                                                   [SVProgressHUD showErrorWithStatus:@"跳轉(zhuǎn)失敗,請自行前往個人中心查看绘搞。"];
                                               }

                                           }
                                        buttonTitle:nil
                                     buttonCallback:^{
                                     }
                                         atPosition:TSMessageNotificationPositionNavBarOverlay
                               canBeDismissedByUser:YES];
        completionHandler(UNNotificationPresentationOptionSound); // 需要執(zhí) 這個 法彤避,選擇 是否提醒 戶,有Badge夯辖、Sound琉预、Alert三種類型可以選擇設(shè)置

    }

上面代碼我選擇了一個提示的第三方TSMessage在點擊消息的時候做了一個跳轉(zhuǎn)。在調(diào)用這個三方的時候你首先要獲取當(dāng)前頂層控制器賦值給這個三方蒿褂。 UIViewController *currentVC = [self currentViewController];


//獲取Window當(dāng)前顯示的ViewController
- (UIViewController*)currentViewController{
    //獲得當(dāng)前活動窗口的根視圖
    UIViewController* vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (1) {
        //根據(jù)不同的頁面切換方式圆米,逐步取得最上層的viewController
        if ([vc isKindOfClass:[UITabBarController class]]) {
            vc = ((UITabBarController*)vc).selectedViewController;
        }
        if ([vc isKindOfClass:[UINavigationController class]]) {
            vc = ((UINavigationController*)vc).visibleViewController;
        }
        if (vc.presentedViewController) {
            vc = vc.presentedViewController;
        }else{
            break;
        }
    }
    return vc;
}

有個細(xì)節(jié)是,當(dāng)你跳轉(zhuǎn)指定控制器的時候啄栓,你有必要判斷當(dāng)前控制器是否可以push過去娄帖,即有沒有導(dǎo)航欄,如果沒有則不能push昙楚,當(dāng)然也可以像我這樣先跳轉(zhuǎn)到個人中心近速,保證有導(dǎo)航欄,但是這樣的處理也許不適用你的app.

在后臺收到消息如何處理堪旧?如何跳轉(zhuǎn)削葱?

當(dāng)你在后臺,收到消息淳梦,通知欄會彈出一個系統(tǒng)alert析砸,一旦你點擊了這個alert,目標(biāo)app會被喚起爆袍,同時調(diào)用下面代理函數(shù)首繁。

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler;

在這個函數(shù)中我們做這樣的提示螃宙、跳轉(zhuǎn)處理蛮瞄。

 // Required
    NSDictionary *userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
                
        /*
         *     應(yīng)用在后臺  點擊了通知欄
         */
        UIViewController *currentVC = [self currentViewController];
        if (currentVC.navigationController) {
            if ([Person currentLoginUser].userId.length == 11) {
                //先跳轉(zhuǎn)指定tab然后push到指定頁面
                BaseTabBarController *myTbabar = (BaseTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                myTbabar.selectedIndex = 2;
                MyServiceViewController *vc = [[MyServiceViewController alloc] init];
                [vc setHidesBottomBarWhenPushed:YES];
                //改變bar后再次獲取當(dāng)前控制器
                UIViewController *VC = [self currentViewController];
                [VC.navigationController pushViewController:vc animated:YES];
            }else {
                [SVProgressHUD showInfoWithStatus:@"請登錄后,前往我的服務(wù)查看谆扎。"];
            }
        }else {
            //避免沒有導(dǎo)航欄跳轉(zhuǎn)崩潰的問題
            [SVProgressHUD showErrorWithStatus:@"跳轉(zhuǎn)失敗挂捅,請自行前往個人中心查看。"];
        }
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler(); // 系統(tǒng)要求執(zhí) 這個 法

以上跳轉(zhuǎn)代碼沙郭、獲取當(dāng)前頂層控制器都是相同的路克,不再解釋。

未啟動時受到消息如何處理紊服?如何跳轉(zhuǎn)

這種情況是最不容易找到的伺糠,因為當(dāng)你的應(yīng)用未啟動蒙谓,點擊了通知欄,它沒有調(diào)用任何極光的代理训桶,這時候需要我們?nèi)雍瘮?shù)進行判斷累驮。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self appStateInactive:launchOptions];
}

在上面啟動函數(shù)中調(diào)用一個方法,來判斷是否是收到通知啟動的舵揭。最后谤专,進行目標(biāo)控制器跳轉(zhuǎn)。

- (void)appStateInactive:(NSDictionary *)launchOptions {
      /*
 *     當(dāng)應(yīng)用不在后臺    點擊通知欄
 */
NSDictionary* pushInfo = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
if (pushInfo) {
    NSDictionary *apsInfo = [pushInfo objectForKey:@"aps"];
    if(apsInfo) {
        [self.mainTab.tabBar showBadgeOnItemIndex:2];
        UIViewController *currentVC = [self currentViewController];
        if (currentVC.navigationController) {
            if ([Person currentLoginUser].userId.length == 11) {
                //先跳轉(zhuǎn)指定tab然后push到指定頁面
                BaseTabBarController *myTbabar = (BaseTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                myTbabar.selectedIndex = 2;
                MyServiceViewController *vc = [[MyServiceViewController alloc] init];
                [vc setHidesBottomBarWhenPushed:YES];
                //更改后bar后再次獲取當(dāng)前控制器
                UIViewController *VC = [self currentViewController];
                [VC.navigationController pushViewController:vc animated:YES];
            }else {
                [SVProgressHUD showInfoWithStatus:@"請登錄后午绳,前往我的服務(wù)查看置侍。"];
            }
        }
    }
    
}
                
}

收到自定義通知如何顯示?

自定義消息拦焚,這個比較特殊蜡坊,它必須是在app正在前臺的時候才能收到消息,收到消息的位置而且不在代理中赎败,而是在一個通知中秕衙。


當(dāng)注冊極光后可以加入下面代碼。注冊一個通知螟够。

// 注冊通知   當(dāng)收到自定義消息的時候
    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(networkDidReceiveMessage:)
                                                     name:kJPFNetworkDidReceiveMessageNotification
                                                   object:nil];

這個通知類型極光文檔是這樣解釋的

kJPFNetworkDidReceiveMessageNotification // 收到消息(非APNS)

實現(xiàn)這個通知方法

/*
 *      當(dāng)在前臺  接收到  自定義消息 通知
 */
- (void)networkDidReceiveMessage:(NSNotification *)notification {
    
    //有沒有未讀消息
    [self.mainTab.tabBar showBadgeOnItemIndex:2];
    
    
    NSDictionary *userInfo = [notification userInfo];
    NSString *content = [userInfo valueForKey:@"content"];
      
    //獲取頂層
    UIViewController *currentVC = [self currentViewController];
    [TSMessage showNotificationInViewController:currentVC.navigationController
                                          title:@"系統(tǒng)通知"
                                       subtitle:content
                                          image:[UIImage imageNamed:@"notifi"]
                                           type:TSMessageNotificationTypeMessage
                                       duration:TSMessageNotificationDurationAutomatic
                                       callback:^{
                                           //判斷是否有導(dǎo)航欄
                                           if (currentVC.navigationController) {
                                               if ([Person currentLoginUser].userId.length == 11) {
                                                   //先跳轉(zhuǎn)指定tab然后push到指定頁面
                                                   BaseTabBarController *myTbabar = (BaseTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                                                   myTbabar.selectedIndex = 2;
                                                   //改變bar后再次獲取當(dāng)前控制器
                                                   UIViewController *VC = [self currentViewController];
                                                   MyServiceViewController *vc = [[MyServiceViewController alloc] init];
                                                   [vc setHidesBottomBarWhenPushed:YES];
                                                   [VC.navigationController pushViewController:vc animated:YES];
                                               }else {
                                                   [SVProgressHUD showInfoWithStatus:@"請登錄后灾梦,前往我的服務(wù)查看峡钓。"];
                                               }
                                           }else {//沒有導(dǎo)航欄
                                               [SVProgressHUD showErrorWithStatus:@"跳轉(zhuǎn)失敗妓笙,請自行前往個人中心查看。"];
                                           }
                                       }
                                    buttonTitle:nil
                                 buttonCallback:^{
                                 }
                                     atPosition:TSMessageNotificationPositionNavBarOverlay
                           canBeDismissedByUser:YES];

}

上面代碼幾乎和在前臺收通知的代碼是一樣的能岩。有點需要說明的是這里我直接取的content這個key寞宫。我這邊讓后臺傳的是這個,當(dāng)然也有后臺會放extras這個字段的json數(shù)據(jù)拉鹃,我后面會有代碼詳細(xì)說這個解析辈赋。

怎么給指定用戶發(fā)送消息?

通知一般不是廣播式的膏燕,有時候需要針對不同用戶群體或者個體發(fā)送通知钥屈,例如優(yōu)惠券等。極光提供了幾種區(qū)分用戶的方法坝辫,在Web中我們可以看到

image.png

設(shè)備標(biāo)簽篷就、別名、ID近忙、群推

標(biāo)簽和別名差不多竭业。只說別名≈侨螅現(xiàn)在有這個場景:我想給我所有的注冊用戶推送消息,沒注冊的不想推未辆。
這時候窟绷,你需要在極光登錄成功(非常重要,否則你可能出現(xiàn)注冊別名無效的情況)的通知方法中向極光服務(wù)器注冊Alias。如下代碼咐柜,建議以userId或者服務(wù)器登錄返回tag值注冊兼蜈,這樣后臺方便發(fā)送消息。登錄成功的通知名稱:kJPFNetworkDidLoginNotification

[JPUSHService setAlias:userId callbackSelector:@selector(alias:) object:self];

這樣你在web選擇對應(yīng)別名發(fā)送通知拙友,就可以推送到希望推送到的手機上了饭尝。

當(dāng)有多個類型通知的時候,怎么拿到服務(wù)器發(fā)送過來的extras的內(nèi)容献宫,然后根據(jù)類型做出正確的跳轉(zhuǎn)钥平?

在上面我們一直說的都是在控制臺直接發(fā)送消息,然而實際上我們推送都是后臺開發(fā)人員以API的形式對接極光的服務(wù)器姊途。通知往往攜帶一些所需的參數(shù)涉瘾,例如紅包里面的金額cost...不多說廢話,直接看我的解析代碼吧捷兰!

//json解析
- (NSDictionary *)jsonWithString:(NSString *)dataString {
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    return dic;
}
//內(nèi)部收到消息立叛,提示
- (void)showMessageWithUserInfo:(NSDictionary *)userInfo {
    
    //獲取頂層控制器
    [self.mainTab.tabBar showBadgeOnItemIndex:2];
    UIViewController *currentVC = [self currentViewController];
    NSString *messageAlert = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
    NSString *extras = [userInfo objectForKey:@"extras"];
    NSDictionary *dic = [self jsonWithString:extras];
    
    NSString *notifiType = [NSString stringWithFormat:@"%@",dic[@"messag_type"]];
    
    [TSMessage showNotificationInViewController:currentVC.navigationController title:@"系統(tǒng)通知" subtitle:messageAlert image:[UIImage imageNamed:@"notifi"] type:TSMessageNotificationTypeMessage duration:TSMessageNotificationDurationAutomatic callback:^{
        if (currentVC.navigationController) {
            if ([Person currentLoginUser].userId.length == 11) {
                if ([notifiType isEqualToString:@"1"]) {//消息類型
                    CommentViewController *vc = [[CommentViewController alloc] initWithNibName:@"CommentViewController" bundle:nil];
                    vc.orderTitle = dic[@"item_title"];
                    vc.remark = dic[@"comments"];
                    vc.time = dic[@"item_time"];
                    vc.orderId = [NSString stringWithFormat:@"%@",dic[@"item_id"]];
                    [vc setHidesBottomBarWhenPushed:YES];
                    [currentVC.navigationController pushViewController:vc animated:YES];
                }else {
                    //其他消息類型
                }
                
            }else {
                [SVProgressHUD showInfoWithStatus:@"請登錄后,前往我的服務(wù)查看贡茅。"];
            }
        }else {
            [SVProgressHUD showErrorWithStatus:@"跳轉(zhuǎn)失敗秘蛇,請自行前往個人中心查看。"];
        }
    } buttonTitle:nil buttonCallback:^{
    } atPosition:TSMessageNotificationPositionNavBarOverlay
                           canBeDismissedByUser:YES];
    
}
//外部收到消息顶考。點擊了消息
- (void)touchAlertWithUserInfo:(NSDictionary *)userInfo {
    
    [self.mainTab.tabBar showBadgeOnItemIndex:2];
    UIViewController *currentVC = [self currentViewController];
    NSString *extras = userInfo[@"extras"];
    NSDictionary *dic = [self jsonWithString:extras];
    NSString *notifiType = [NSString stringWithFormat:@"%@",dic[@"messag_type"]];
    
    if (currentVC.navigationController) {
        if ([Person currentLoginUser].userId.length == 11) {
            if ([notifiType isEqualToString:@"1"]) {//消息類型
                //先跳轉(zhuǎn)指定tab然后push到指定頁面
                BaseTabBarController *myTbabar = (BaseTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                myTbabar.selectedIndex = 1;
                //改變bar后再次獲取當(dāng)前控制器
                UIViewController *VC = [self currentViewController];
                CommentViewController *vc = [[CommentViewController alloc] initWithNibName:@"CommentViewController" bundle:nil];
                vc.orderTitle = dic[@"item_title"];
                vc.remark = dic[@"comments"];
                vc.time = dic[@"item_time"];
                vc.orderId = [NSString stringWithFormat:@"%@",dic[@"item_id"]];
                [vc setHidesBottomBarWhenPushed:YES];
                [VC.navigationController pushViewController:vc animated:YES];
            }
            
        }else {
            [SVProgressHUD showInfoWithStatus:@"請登錄后赁还,前往我的服務(wù)查看。"];
        }
    }else {
        [SVProgressHUD showErrorWithStatus:@"跳轉(zhuǎn)失敗驹沿,請自行前往個人中心查看艘策。"];
    }
}

我把以上方法獨立出來。方法都有注釋渊季,不再講解朋蔫。那么,這時候我僅僅需要在合適的位置調(diào)用這些方法即可却汉!
例如驯妄。在后臺的時候

// iOS 10 Support
- (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]]) {
        /*
         *     應(yīng)用在后臺  點擊了通知欄
         */
        [self touchAlertWithUserInfo:userInfo];
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler(); // 系統(tǒng)要求執(zhí) 這個 法
}

在前臺的時候

// iOS 10 Support
- (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];
    }
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
        
        /*
         *      當(dāng)應(yīng)用在前臺  接到通知
         */
        NSLog(@"userInfo : %@",userInfo);
        //提示
        [self showMessageWithUserInfo:userInfo];
        completionHandler(UNNotificationPresentationOptionSound); // 需要執(zhí) 這個 法,選擇 是否提醒 戶合砂,有Badge青扔、Sound、Alert三種類型可以選擇設(shè)置

    }else if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {//后臺
        completionHandler(UNNotificationPresentationOptionAlert); // 需要執(zhí) 這個 法,選擇 是否提醒 戶赎懦,有Badge雀鹃、Sound、Alert三種類型可以選擇設(shè)置
    }else {//未啟動
        completionHandler(UNNotificationPresentationOptionAlert); // 需要執(zhí) 這個 法励两,選擇 是否提醒 戶黎茎,有Badge、Sound当悔、Alert三種類型可以選擇設(shè)置
    }
}

我們看到通過方法獨立出來傅瞻,我們的代碼更簡潔,邏輯也更清晰了盲憎。

怎么在app內(nèi)部開啟嗅骄、關(guān)閉通知?

一般都在app設(shè)置中有一個switch的開關(guān)饼疙。來開啟關(guān)閉通知溺森,代碼很簡單,直接貼上來窑眯。有一點需要注意屏积,就是這個開關(guān)的狀態(tài)需要存到本地,有高要求的存服務(wù)器磅甩。

cell.textLabel.text = @"消息提醒";
        UISwitch *swi = [[UISwitch alloc] initWithFrame:CGRectMake(CGRectGetWidth(cell.frame)-15,7.5, 45, 30)];
        //存儲到本地
        NSUserDefaults *defaults = [[NSUserDefaults alloc] init];
        NSString *isNotification = [defaults objectForKey:@"isNotification"];
        NSLog(@"noti %@",isNotification);
        if ([isNotification isEqualToString:@"0"]) {
            swi.on = NO;
        }else {
            swi.on = YES;
        }
        [swi addTarget:self action:@selector(onOrOff:) forControlEvents:UIControlEventValueChanged];
        [cell.contentView addSubview:swi];
#pragma mark -- 消息推送的 開/關(guān)
- (void)onOrOff:(UISwitch *)swi {
    NSUserDefaults *defaults = [[NSUserDefaults alloc] init];
    if (!swi.on) {//存入本地
        [[UIApplication sharedApplication] unregisterForRemoteNotifications];//關(guān)閉
        [defaults setObject:@"0" forKey:@"isNotification"];
    }else {
        [[UIApplication sharedApplication] registerForRemoteNotifications];
        [defaults setObject:@"1" forKey:@"isNotification"];
    }
}

另外炊林,不要忘記!你需要在啟動代理中也進行判斷開關(guān)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   //是否關(guān)閉了推送卷要?
    NSUserDefaults *defaults = [[NSUserDefaults alloc] init];
    NSString *isNotification = [defaults objectForKey:@"isNotification"];
    if ([isNotification isEqualToString:@"0"]) {
        [[UIApplication sharedApplication] unregisterForRemoteNotifications];//關(guān)閉
    }else {
        [[UIApplication sharedApplication] registerForRemoteNotifications];//開啟
    }
}

你測試包(真機調(diào)試)收到通知后渣聚,怎么確定上架包也能收到消息?

想要測試上架包僧叉,首先更改極光注冊代碼奕枝。production改為YES


[JPUSHService setupWithOption:launchOptions appKey:kJPushAPPKey channel:@"apsForProduction" apsForProduction:YES
            advertisingIdentifier:nil];

(開發(fā)、生產(chǎn)證書配置不再說彪标,網(wǎng)上很多)這個就涉及到打包的知識了倍权,開發(fā)環(huán)境就是真機調(diào)試的我就不說了。我們這里使用蒲公英平臺安裝生產(chǎn)環(huán)境包捞烟,archive后選擇

image.png

然后選擇Ad Hoc

image.png

這樣打包就是生產(chǎn)包了。如果你沒有對應(yīng)的證書可以去配置当船,同時你還需要配置對應(yīng)描述文件题画。
iOS技術(shù)交流群:511860085 成堆的技術(shù)視頻福利,歡迎加入德频!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末苍息,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌竞思,老刑警劉巖表谊,帶你破解...
    沈念sama閱讀 221,888評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異盖喷,居然都是意外死亡爆办,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,677評論 3 399
  • 文/潘曉璐 我一進店門课梳,熙熙樓的掌柜王于貴愁眉苦臉地迎上來距辆,“玉大人,你說我怎么就攤上這事暮刃】缢悖” “怎么了?”我有些...
    開封第一講書人閱讀 168,386評論 0 360
  • 文/不壞的土叔 我叫張陵椭懊,是天一觀的道長诸蚕。 經(jīng)常有香客問我,道長氧猬,這世上最難降的妖魔是什么挫望? 我笑而不...
    開封第一講書人閱讀 59,726評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮狂窑,結(jié)果婚禮上媳板,老公的妹妹穿的比我還像新娘。我一直安慰自己泉哈,他們只是感情好蛉幸,可當(dāng)我...
    茶點故事閱讀 68,729評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著丛晦,像睡著了一般奕纫。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上烫沙,一...
    開封第一講書人閱讀 52,337評論 1 310
  • 那天匹层,我揣著相機與錄音,去河邊找鬼锌蓄。 笑死升筏,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的瘸爽。 我是一名探鬼主播您访,決...
    沈念sama閱讀 40,902評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼剪决!你這毒婦竟也來了灵汪?” 一聲冷哼從身側(cè)響起檀训,我...
    開封第一講書人閱讀 39,807評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎享言,沒想到半個月后峻凫,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,349評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡览露,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,439評論 3 340
  • 正文 我和宋清朗相戀三年荧琼,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片肛循。...
    茶點故事閱讀 40,567評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡铭腕,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出多糠,到底是詐尸還是另有隱情累舷,我是刑警寧澤,帶...
    沈念sama閱讀 36,242評論 5 350
  • 正文 年R本政府宣布夹孔,位于F島的核電站被盈,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏搭伤。R本人自食惡果不足惜只怎,卻給世界環(huán)境...
    茶點故事閱讀 41,933評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望怜俐。 院中可真熱鬧身堡,春花似錦、人聲如沸拍鲤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,420評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽季稳。三九已至擅这,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間景鼠,已是汗流浹背仲翎。 一陣腳步聲響...
    開封第一講書人閱讀 33,531評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留铛漓,地道東北人溯香。 一個月前我還...
    沈念sama閱讀 48,995評論 3 377
  • 正文 我出身青樓,卻偏偏與公主長得像票渠,于是被迫代替她去往敵國和親逐哈。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,585評論 2 359

推薦閱讀更多精彩內(nèi)容