1. 程式人生 > >收到遠端推送後的頁面跳轉

收到遠端推送後的頁面跳轉

寶寶最近帶著血光學了點關於推送的東西,

1.獲取deviceToken後要轉換成16進位制上傳給伺服器
以下是轉換方法

//把deviceToken變成16進位制字串
- (NSString *)getHEX:(NSData *)data
{


    if (!data || [data length] == 0) {
        return @"";
    }

    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];

    [data enumerateByteRangesUsingBlock:^(const
void * bytes, NSRange byteRange, BOOL *stop) { unsigned char *dataBytes = (unsigned char*)bytes; for (NSInteger i = 0; i < byteRange.length; i++) { NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff]; if ([hexStr length] == 2) { [string appendString:hexStr]; } else
{ [string appendFormat:@"0%@", hexStr]; } } }]; return string; }

2.接收到通知後 判斷是前臺執行還是後臺執行

//接收遠端推送的訊息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler {

    //接到
//取出訊息體 NSString *messageAlert = [[userInfo objectForKey:@"aps"]objectForKey:@"alert"]; NSString * type = [userInfo objectForKey:@"type"]; if (application.applicationState == UIApplicationStateActive) { //程式處於前臺 NSLog(@"active"); //把icon上的小圖示設定為0 [application setApplicationIconBadgeNumber:0]; //獲取當前的控制器 因為我的控制器是以navigation存在 所以比較好取出 UINavigationController * nav = (UINavigationController *)self.window.rootViewController; UIViewController * currentVC = [nav.viewControllers lastObject]; //彈框通知 UIAlertController * stateAlert = [UIAlertController alertControllerWithTitle:@"遠端通知" message:messageAlert preferredStyle:UIAlertControllerStyleAlert]; [stateAlert addAction:[UIAlertAction actionWithTitle:@"前往" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"前往點選"); //去指定的VC控制器 [self goToDesignatedVC:type withUserInfo:userInfo]; }]]; [stateAlert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]]; [currentVC presentViewController:stateAlert animated:YES completion:nil]; } else if(application.applicationState == UIApplicationStateInactive){ //程式處於後臺 //去指定的VC控制器 [self goToDesignatedVC:type withUserInfo:userInfo]; } NSLog(@"userInfo %@",userInfo); }

3.根據推送訊息的型別不同 實現不同的處理方法 (只寫了一個)

-(void)goToDesignatedVC:(NSString *)type withUserInfo:(NSDictionary *)userInfo{

    NSInteger typeInt = type.intValue;

    switch (typeInt) {
        case 1:
        {
            //收到訂單的通知處理方法

            NSLog(@"appdelegate進入遠端通知,收取訂單選項");
            NSString * order_id = userInfo[@"order_id"];


            //通知訂單列表頁面 跳轉進入訂單詳情頁面
            [[NSNotificationCenter defaultCenter] postNotificationName:@"tabBarTalkToOrderVCNoti" object:order_id];

        }
            break;

        case 2:
        {



        }
            break;

        default:
            break;
    }



}

4.注意 我是用通知去跳轉指定頁面
一定要一層一層去扒開這些VC的衣服去跳轉!
比如我就扒了兩層 先發通知扒了TabBarViewControlle 再從這一頁面繼續發通知進入我需要的頁面!