iOS開發——消息推送跳轉

項目開發用集成是極光推送JPush
app

    這裏主要是消息推送過來處理對應界面跳轉
fetch

    
    同時看到兩篇寫的不錯的相關博客分享一下:
ui

     http://www.jianshu.com/p/eaf07c4372a8spa

     http://www.jianshu.com/p/d4460fed39c1.net

   

   推送的是根據用戶ID及用戶的綁定的JPush註冊ID,對應用戶推送。
code

    首先看一下推送過來的消息格式:blog


推送來的字典:{
    "_j_msgid" = 976126105;
    aps =     {
        alert = "\U865a\U62df\U7b56\U7565I160704711100001\U5356\U51fa\U80a1\U7968:\U5b9c\U534e\U5065\U5eb7100\U80a1\U3001\U4ef7\U683c\U4e3a32.15\U3001\U5171\U8ba13215\U2026\U2026";
        badge = 50;
        sound = "";
    };
    "pro_id" = 160704711100001;
    "pro_type" = 1;
    "type_id" = 1;
}
事件



其中    "pro_id" = 160704711100001;
    "pro_type" = 1;
    "type_id" = 1;

開發

這三個是後臺服務自定義的鍵值信息,根據本身開發的須要自定義。主要靠這三個信息跳轉到對應的界面。get


  alert = "\U865a\U62df\U7b56\U7565I160704711100001\U5356\U51fa\U80a1\U7968:\U5b9c\U534e\U5065\U5eb7100\U80a1\U3001\U4ef7\U683c\U4e3a32.15\U3001\U5171\U8ba13215\U2026\U2026";
        badge = 50;
        sound = "";

這三個是推送的固定格式,無論誰的推送都有,alert是通知欄提示的消息信息,badge是桌面app數字角標提醒

sound是收到消息提醒的聲音。

接收到推送的通知有三種狀況,第一種,App未啓動  第二種,App在前臺   第三種,App在後臺

第一種,App未啓動的 時候,點擊通知欄消息時,app從- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 這個方法裏接受處理消息

處理邏輯爲:將消息放到專門的方法dealRemoteNotification裏處理分析後跳轉

 // 未啓動 推送消息
    NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if(userInfo){
       
        [self dealRemoteNotification:userInfo];
    }


其餘的兩種狀況在下面方法處理

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    // iOS 7 Support Required
    [self dealRemoteNotification:userInfo];


    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

處理跳轉的方法以下

 

    // 推送消息處理跳轉  
    -(void)dealRemoteNotification:(NSDictionary*)userInfo{  
          
        NSLog(@"推送來的字典:%@",userInfo);  
        // 取得 APNs 標準信息內容  
    //    NSDictionary *aps = [userInfo valueForKey:@"aps"];  
    //    NSString *content = [aps valueForKey:@"alert"]; //推送顯示的內容  
    //    NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge數量  
    //    NSString *sound = [aps valueForKey:@"sound"]; //播放的聲音  
    //  
          
        // 前臺的時候不處理消息跳轉  
        if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) { // 前臺不處理推送  
            return;  
        }  
        //服務端自定義鍵值  
        NSString *typeID = [userInfo valueForKey:@"type_id"];     // 真實虛擬  
        NSString *proType = [userInfo valueForKey:@"pro_type"];   // IMP仍是ISM  
        NSString *proID = [userInfo valueForKey:@"pro_id"];       // id或code  
      
        if (proType.integerValue == 4){  // 消息助手詳情  
          
            MsgAssistant_PushViewController *MsgVC = [[MsgAssistant_PushViewController alloc]init];  
              
            [self.window.rootViewController presentViewController:MsgVC animated:YES completion:^{  
                  
            }];  
          
        }  
          
        if (typeID.integerValue == 1) { // 虛擬  
            if (proType.integerValue == 1) { // ISM  
                  
             
                ISM_PushTradeCommandViewController *ISMPush = [[ISM_PushTradeCommandViewController alloc]init];  
                ISMPush.ismCodeString                       = proID;  
                ISMPush.isVirtualExperience                 = YES;  
                  
                  
                [self.window.rootViewController presentViewController:ISMPush animated:YES completion:^{  
                      
                }];  
                  
            }else if(proType.integerValue == 2){ // IMP  
                  
                // IMP交易記錄  
                IMP_PushTradeRecordViewController *IMPPushVC = [[IMP_PushTradeRecordViewController alloc] init];  
               
                IMPPushVC.impId               = proID;  
                IMPPushVC.isVirtualExperience = YES;  
                  
      
                [self.window.rootViewController presentViewController:IMPPushVC animated:YES completion:^{  
                      
                }];  
              
            }else{  
                return ;  
            }  
              
        }else if (typeID.integerValue == 2){  // 真實  
          
            if (proType.integerValue == 1) { // ISM  
                  
                  
                ISM_PushTradeCommandViewController *ISMPush = [[ISM_PushTradeCommandViewController alloc]init];  
                ISMPush.ismCodeString                       = proID;  
                ISMPush.isVirtualExperience                 = NO;  
                 
                [self.window.rootViewController presentViewController:ISMPush animated:YES completion:^{  
                      
                }];  
                  
            }else if(proType.integerValue == 2){ // IMP  
                  
                // IMP交易記錄  
                IMP_PushTradeRecordViewController *IMPPushVC = [[IMP_PushTradeRecordViewController alloc] init];  
        
                IMPPushVC.impId               = proID;  
                IMPPushVC.isVirtualExperience = NO;  
                 
                  
                [self.window.rootViewController presentViewController:IMPPushVC animated:YES completion:^{  
                      
                }];  
          
        }else{  
          
            return ;  
        }  
          
      }  
          
    }  

 

本跳轉是單個界面,沒有下一級,因此沒用導航控制器

根據我的狀況處理,這裏前臺的時候沒有處理,有的前臺處理的時候是彈窗提示,引導跳轉


同時注意 跳轉的界面返回的處理,這裏重寫跳轉界面返回事件

- (void)backButtonEvent{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}

通常狀況下,看完一個消息詳情界面用戶會點擊返回退出此界面,

若是用戶未退出消息詳情界面,而下拉通知欄點擊了消息,可能會因爲未dimiss的界面,

新的消息詳情界面沒法present出來,建議處理消息的方法裏跳轉前發送通知到控制器執行dimiss操做。

相關文章
相關標籤/搜索