iOS开发——消息推送跳转

项目开发用集成是极光推送JPush

    这里主要是消息推送过来处理对应界面跳转

    
    同时看到两篇写的不错的相关博客分享一下:

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

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

   

   推送的是根据用户ID及用户的绑定的JPush注册ID,对应用户推送。

    首先看一下推送过来的消息格式:


推送来的字典:{
    "_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;

这三个是后台服务自定义的键值信息,根据自己开发的需要自定义。主要靠这三个信息跳转到对应的界面。


  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操作。


 

posted @ 2016-08-04 11:22  刘成利的博客  阅读(566)  评论(0编辑  收藏  举报