在前面已经做完了极光消息的推送,那么有消息了,如何跳转到需要的控制器呢?其实,主要还是在userInfo这个消息里面做判断来处理,具体如下:
下面这两个是远程推送时接收消息的方法,这是应用程序提供的方法,只要成功注册了极光推送,推送消息时,就会调用这两个方法,在这两个方法收到的userInfo消息做判断即可。
// Required,For systems with less than or equal to iOS6
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
// IOS 7 Support Required
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
具体代码如下:我这里是区别环信推送消息控制器跳转和环信推送消息控制器跳转
通过在极光推送的服务器上设置自定义字段,用来判断跳转的是极光推送的消息控制器
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ // Required,For systems with less than or equal to iOS6 [JPUSHService handleRemoteNotification:userInfo]; } -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ // IOS 7 Support Required [JPUSHService handleRemoteNotification:userInfo]; if (completionHandler) { completionHandler(UIBackgroundFetchResultNewData); } //消息提示数字 self.badge = userInfo[@"aps"][@"badge"]; //取得Extras字段内容 NSString *customizeValue = [userInfo valueForKey:@"customizeExtras"]; //服务端中Extras字段,key是自己定义的,用来判断跳转的是极光推送的消息控制器 // 启动程序,跳转到极光推送消息的控制器 if ([customizeValue isEqualToString:@"Jpush"]) { KJTabViewController *rootVC = (KJTabViewController *)[UIApplication sharedApplication].keyWindow.rootViewController; rootVC.selectedIndex = 4; KJNavgationController *navc = rootVC.viewControllers[4]; self.mineVC = navc.viewControllers[0]; KJNewFriendController *newVC = [[KJNewFriendController alloc]init]; [self.mineVC.navigationController pushViewController:newVC animated:YES]; [self CancelBadgeValue]; application.applicationIconBadgeNumber -= [self.badge integerValue]; } else{ // 启动程序,跳转到环信推送消息的控制器 KJTabViewController *rootVC = (KJTabViewController *)[UIApplication sharedApplication].keyWindow.rootViewController; rootVC.selectedIndex = 1; } // 应用正处理前台状态下,不会收到极光推送消息,因此在此处需要额外处理一下 if (application.applicationState == UIApplicationStateActive) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"您有一条推送消息" message:userInfo[@"aps"][@"alert"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil]; [alert show]; //注册监听,取消badgeValue的数字 [NotyCenter addObserver:self selector:@selector(CancelBadgeValue) name:@"CancelBadgeValueNotification" object:nil]; } } #pragma mark - UIAlertViewDelegate -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ UIViewController *currentVC = [self getCurrentVC]; if ([currentVC isKindOfClass:[KJTabViewController class]]) { //根控制器 KJTabViewController *rootVC = (KJTabViewController *)currentVC; KJNavgationController *navc = rootVC.viewControllers[4]; self.mineVC = navc.viewControllers[0]; if (buttonIndex == 0) { self.mineVC.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@",self.badge]; }else{ KJNewFriendController *newVC = [[KJNewFriendController alloc]init]; rootVC.selectedIndex = 4; [self.mineVC.navigationController pushViewController:newVC animated:YES]; [self CancelBadgeValue]; } }else{ //非根控制器 KJTabViewController *rootVC = (KJTabViewController *)[UIApplication sharedApplication].keyWindow.rootViewController; KJNavgationController *navc = rootVC.viewControllers[4]; self.mineVC = navc.viewControllers[0]; if (buttonIndex == 0) { self.mineVC.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@",self.badge]; } if (buttonIndex == 1) { [currentVC.navigationController popToRootViewControllerAnimated:NO]; rootVC.selectedIndex = 4; KJNewFriendController *newVC = [[KJNewFriendController alloc]init]; [self.mineVC.navigationController pushViewController:newVC animated:YES]; [self CancelBadgeValue]; } } } #pragma mark - 取消极光消息数目 -(void)CancelBadgeValue{ self.mineVC.tabBarItem.badgeValue = nil; } /** * 获取当前屏幕显示的viewcontroller */ - (UIViewController *)getCurrentVC { UIViewController *result = nil; UIWindow * window = [[UIApplication sharedApplication] keyWindow]; if (window.windowLevel != UIWindowLevelNormal) { NSArray *windows = [[UIApplication sharedApplication] windows]; for(UIWindow * tmpWin in windows) { if(tmpWin.windowLevel == UIWindowLevelNormal) { window = tmpWin; break; } } } UIView *frontView = [[window subviews] objectAtIndex:0]; id nextResponder = [frontView nextResponder]; if ([nextResponder isKindOfClass:[UIViewController class]]){ result = nextResponder; }else{ result = window.rootViewController; } return result; }
时间: 2024-10-01 10:28:17