如何在应用关闭时打开推送通知时推送新视图?

时间:2011-05-06 20:38:16

标签: iphone objective-c apple-push-notifications

所以我要做的是当你在应用程序打开时点击查看接收推送通知时,它会显示所呈现的视图,推送一个新的控制器,其中包含有关通知的详细信息。我正在使用UITabBarController和UINavigationControllers。任何帮助将不胜感激,我已经尝试过搜索,但我似乎找不到任何指向正确方向的东西。目前的代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];

[application setStatusBarStyle:UIStatusBarStyleBlackOpaque];

tabBarController = [[UITabBarController alloc] init];

controller = [[controller alloc] init];
UINavigationController *controller1 = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease];
controller1.tabBarItem.image = [UIImage imageNamed:@"icon_news.png"];
[controller setTitle:@"View"];
[controller release];

controller = [[controller alloc] init];
UINavigationController *controller2 = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease];
controller2.tabBarItem.image = [UIImage imageNamed:@"icon_news.png"];
[controller setTitle:@"View"];
[controller release];

controller = [[controller alloc] init];
UINavigationController *controller3 = [[[UINavigationController alloc] initWithRootViewController:controller3] autorelease];
controller3.tabBarItem.image = [UIImage imageNamed:@"icon_news.png"];
[controller setTitle:@"View"];
[controller release];

controller = [[controller alloc] init];
UINavigationController *controller4 = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease];
controller4.tabBarItem.image = [UIImage imageNamed:@"icon_news.png"];
[controller setTitle:@"View"];
[controller release];

controller = [[controller alloc] init];
UINavigationController *controller5 = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease];
controller5.tabBarItem.image = [UIImage imageNamed:@"icon_news.png"];
[controller setTitle:@"View"];
[controller release];

tabBarController.viewControllers = [NSArray arrayWithObjects:controller1, controller2, controller3, controller4, controller5, nil];

[window addSubview:tabBarController.view];

[window makeKeyAndVisible];

launchDefault = YES;
//[self performSelector:@selector(handlePostLaunch) withObject:nil afterDelay:0];

// Push Notification info

NSDictionary *apns = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

NSString *result =  [[[apns valueForKey:@"aps"] valueForKey:@"alert"] valueForKey:@"loc-args"];

NSString *playerID = [NSString stringWithFormat:@"%@", result];

playerID = [[playerID componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@""];

playerID = [playerID stringByReplacingOccurrencesOfString:@"  " withString:@""];

playerID = [playerID stringByReplacingOccurrencesOfString:@"(" withString:@""];

playerID = [playerID stringByReplacingOccurrencesOfString:@")" withString:@""];

NSLog(@"Player ID: %@", playerID);

if (![playerID isEqualToString:@"null"]) {
    if (!detailViewController) {
        detailViewController = [[PlayerDetailViewController alloc] init];
    }

    NSManagedObjectContext *moc = [[AppController sharedAppController] managedObjectContext];

    NSFetchRequest *req = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Players"
                                              inManagedObjectContext:moc];
    [req setEntity:entity];

    NSPredicate *pre = [NSPredicate predicateWithFormat:@"playerID=%@", playerID];
    [req setPredicate:pre];

    NSError *error;
    NSArray *list = [moc executeFetchRequest:req error:&error];

    [req release];

    Players *player = [list lastObject];

    [detailViewController setPlayer:player];

    //Want to Push view here

    [detailViewController release];

    detailViewController = nil;
}

return YES;

}

1 个答案:

答案 0 :(得分:0)

如果应用程序是通过通知启动的,那么它将出现在关键字UIApplicationLaunchOptionsRemoteNotificationKey下的应用程序委托的application:didFinishLaunchingWithOptions: launchOptions字典中,该字典具有通知所具有的所有信息(JSON转换为我相信的NSDictionary)。

编辑:

问题有误,我认为你所寻找的只是指向当前所选导航控制器的指针。如果查询返回可见导航控制器的[tabbarcontroller selectedViewController],就可以得到它。然后只需将新创建的控制器推到该导航控制器的堆栈顶部即可。