How to push a new view upon opening push notificat

2019-06-14 01:42发布

问题:

So what I'm trying to do is when you click view on receiving a push notification when the app opens it has the presented view push a new controller with details regarding the notification. I'm using a UITabBarController with UINavigationControllers. Any help would be much appreciated, I've tried searching but I couldn't seem to find anything that pointed me in the right direction. Current code below:

- (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:

If application is launched by a notification then it will be present in the application delegate's application:didFinishLaunchingWithOptions: launchOptions dictionary under the key UIApplicationLaunchOptionsRemoteNotificationKey, which has all the info the notification has (JSON converted to NSDictionary I believe).

EDIT:

Got the question wrong, I think what you're looking for is just the pointer to the currently selected navigation controller. You get that if you query [tabbarcontroller selectedViewController], which returns the visible navigation controller. Then simply push the newly created controller on top of the stack of that navigation controller.