在iOS中收到APNS时打开视图控制器

嗨,我是新来的iPhone,我一直在尝试使用苹果推送通知。 基本上,我想要做的是,当用户点击收到的推送通知消息,那么我需要打开一个特定的视图控制器。 我已经添加了关键参数“types”的自定义数据到我的有效载荷JSON,所以代表通知types值我需要打开特定的视图控制器,而不是主视图控制器。

这里是我的有效载荷JSON:

{"aps":{"alert":"This is testing message","type":"Notify","badge":1,"sound":"default"}} 

我的代码是:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; splashViewController = [[SplashViewController alloc] initWithNibName:@"SplashViewController" bundle:nil]; self.window.rootViewController = splashViewController; [self.window makeKeyAndVisible]; return YES; } - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo { //this will call when your app will get any notification from server. [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; [[UIApplication sharedApplication] cancelAllLocalNotifications]; NSDictionary *aps = (NSDictionary *)[userInfo objectForKey:@"aps"]; NSMutableString *notificationType = [aps objectForKey:@"type"]; NSLog(@"notification type is = %@", notificationType); //For redirect to the view UIViewController *viewController; if([notificationType isEqualToString:@"Notify"]){ //Notify updates UpdatesViewController *uvc1 = [[UpdatesViewController alloc] initWithNibName:@"UpdatesViewController" bundle:nil]; viewController = uvc1; } else { //Voting & QA VotingViewController *votingViewController = [[VotingViewController alloc] initWithNibName:@"VotingViewController" bundle:nil]; viewController = votingViewController; } [self.window.rootViewController presentViewController:viewController animated:YES completion:NULL]; } 

我的代码是不呈现其他视图控制器。 正如我的代码所提到的,我得到两种Web服务(通知types)1.通知和2.从苹果投票。 我不知道如何代表通知types呈现特定的视图控制器,如果types是“通知”,那么我需要打开UpdatesViewController否则它将打开其他viewcontroller。 如果有人知道如何做到这一点,请帮助我。

谢谢。

 //For redirect to the view UIViewController *uvc; if([notificationType isEqualToString:@"Notify"]){ UIViewController *updates = [[UpdatesViewController alloc] initWithNibName:@"UpdatesViewController" bundle:nil]; // add updates specific data updates.data = [aps objectForKey:@"data"]; uvc = updates; } else if([notificationType isEqualToString:@"Voting "]) { UIViewController *voting = [[VotingViewController alloc] initWithNibName:@"VotingViewController" bundle:nil]; // add voting specific data voting.data = [aps objectForKey:@"data"]; uvc = voting; } [self.window.rootViewController presentViewController:uvc animated:YES completion:NULL]; 

你应该检查didFinishLaunchingWithOptions的aps。 didReceiveRemoteNotification只适用于应用程序正在运行或暂停。