推送跳转到指定的controllers

来源:互联网 发布:第三方网络平台有哪些 编辑:程序博客网 时间:2024/05/29 18:01

以前做过推送, 但只是那种最基本的广播推送(向所有安装appde设备通知), 列播组播这种对指定用户推送消息还没做过, 最近刚好有个项目,向指定用户推送物流信息、物品状态等等。于是前几天就和也没做过推送的后台干起来了,详情如下:

我用的是友盟推送, 配置证书这一环节直接跳过了,这个网上有讲的。给大家讲一点常识,友盟推送分生产环境和开发环境。用手机刷上去的就是开发环境, 发布到苹果商店就是生产环境,没发布前怎么模拟呢, 用普通账号打的ad hoc 包, 用企业账号打的ad hoc 包或者enterprise包都可以测试生产环境。


开发环境下, 你把APP删掉,重新调试上来,就会生成一个新的device_token了!下面的几个步骤是自己的理解结合网上的资料写出的想法,大牛们轻的吐槽……

1、传device_token的时机

后台向指定用户做同送,那么必须知道某个用户的device_token,那么怎么获取token呢,APP启动后会在appdelegate的didRegisterForRemoteNotificationsWithDeviceToken方法里返回device_token信息:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{  [UMessage registerDeviceToken:deviceToken];   NSString * token = [[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]                     stringByReplacingOccurrencesOfString: @">" withString: @""]                    stringByReplacingOccurrencesOfString: @" " withString: @""];  }

但是后台向指定用户推送, 就必须把uid(用户ID)和token关联起来,我这里的做法时, 用户登录的时候就上传token信息, 退出登录的时候, 就清除用户绑定的token信息, 这样确保后台会向用户最后一次登录的设备号做推送(只是自己的理解,当然了,iOS设备收到推送后,还要判断用户是否登录而且登录的是不是你要推送的用户)。

2、 收到通知时app的状态

收到通知的时候APP的状态可能是未启动、前台活跃(任何界面)、后台等三种。

  • 未启动时,点击通知栏启动App, 会在didFinishLaunchingWithOptions方法里收到通知内容。
  • 剩下两种会在didReceiveRemoteNotification方法里收到通内容。

    - (void)application:(UIApplication *)application   didReceiveRemoteNotification:(NSDictionary *)userInfo{ _userInfo = userInfo;   //关闭友盟对话框 [UMessage setAutoAlert:NO]; [UMessage didReceiveRemoteNotification:userInfo]; NSLog(@"_______________友盟系统方法 userInfo %@",userInfo);if(userInfo)// 调用appdelegate的分类处理业务逻辑  [self dealWithMyMessagePush:userInfo];}

    代码里面删减了一些和推送不相关的代码,我的项目架构是tab+nav

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window.rootViewController = [[FDIMGBarController alloc] init]; //  分类  [self UMengShareMethodAndCount:launchOptions]; // 类别[self FD_updateAppVersion];NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];if(userInfo){//推送信息  self.userInfo = userInfo;//[userInfo copy]} return YES;}
  • 未启动时收到通知,就判断字典userInfo是否为空, 不为空是说明有通知消息。我这里的做法是把她复制给AppDelegate的某个属性, 在首页控制器取到AppDelegate的这个值,判断是否为空,不为空就进行下一步操作。
  • 用户在前台或后台收到消息的时候, 我会弹出一个弹出框提醒用户,是否前往我的消息界面。

5、 未启动时首页控制器逻辑处理

在viewDidLoad方法里:AppDelegate * app = (AppDelegate *)[UIApplication sharedApplication].delegate;//pushName 是我给后天约定的通知必传值,所以我可以根据他是否为空来判断是否有通知NSString *  pushName = [[app.userInfo objectForKey:@"aps"] objectForKey:@"alert"];if(![SYFCustomCLASS SYFIsEmptyOrNull:pushName])    [self getPushInfo:app.userInfo];

如果有通知的话:

-(void)getPushInfo:(NSDictionary *)dict{ if(!IsLogin){// 判断用户是否登录    LoginViewController * loginVC = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];//通知必返回要通知用户的uid, 判断登录的用户是不是你要通知的用户    loginVC.push_uid = dict[@"uid"];    FDNavigationController * loginNav = [[FDNavigationController alloc] initWithRootViewController:loginVC];    [self presentViewController:loginNav animated:YES completion:^{}];}else{// 这就文章标题说的某一界面   MyUserMessageVC * messageVC = [[MyUserMessageVC alloc] initWithNibName:@"MyUserMessageVC" bundle:nil];    [self.navigationController pushViewController:messageVC animated:YES]; }}

当用户的登录成功的时候,在上传device_token的接口方法里, 需要判断登录用户的uid是不是你要通知的用户。如果不是你要通知的用户,dismiss就好了,结束;如果是就要跳转到我的消息界面。

   if(![self.push_uid isEqualToString:currentuid])        [self dismissViewControllerAnimated:YES completion:NULL];       else{        [self dismissViewControllerAnimated:YES completion:^{         AppDelegate * app = (AppDelegate *)[UIApplication sharedApplication].delegate;   //AppDelegate的分类         [app  testLoginerUidCorret];         }];

方法如下:

- (void)testLoginerUidCorret{// 取到tabbarcontrollerFDIMGBarController *tabBarController = ( FDIMGBarController*)self.window.rootViewController;// 取到navigationcontrollerFDNavigationController * nav = (FDNavigationController *)tabBarController.selectedViewController;//取到nav控制器当前显示的控制器  UIViewController * baseVC = (UIViewController *)nav.visibleViewController;//如果是当前控制器是我的消息控制器的话,刷新数据即可if([baseVC isKindOfClass:[MyUserMessageVC class]]){    MyUserMessageVC * vc = (MyUserMessageVC *)baseVC;    [vc reloadMessageData];    return;}// 否则,跳转到我的消息MyUserMessageVC * messageVC = [[MyUserMessageVC alloc] initWithNibName:@"MyUserMessageVC" bundle:nil];[nav pushViewController:messageVC animated:YES];}

6、应用在前台或后台的逻辑处理

逻辑和未启动时很多情况都是类似,弹出提醒框, 点击立即前往时,判断用户是否登录,如果用户登录,直接跳转到我的消息界面;没有登录请参考上面逻辑。


原创粉丝点击