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

时间:2014-04-02 09:44:40

标签: ios iphone apple-push-notifications

嘿,我是iPhone新手,我一直在尝试使用Apple推送通知。基本上,我想要做的是当用户点击收到的推送通知消息时,我需要打开一个特定的视图控制器。我添加了自定义数据和关键参数" type"到我的有效负载JSON,所以代表通知类型值我需要打开特定的视图控制器而不是主视图控制器。

这是我的有效载荷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];
}

我的代码没有呈现其他视图控制器。正如我的代码所述,我得到两种网络服务(通知类型)1。通知和2.从Apple投票。我不知道如何代表通知类型呈现特定的视图控制器,如果类型是"通知"然后我需要打开UpdatesViewController否则它将打开其他viewcontroller。如果有人知道怎么做,请帮助我。

感谢。

2 个答案:

答案 0 :(得分:3)

//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];

答案 1 :(得分:1)

你应该检查didFinishLaunchingWithOptions中的aps。 didReceiveRemoteNotification仅在应用正在投放或暂停时有效。