views:

55

answers:

1

I need to show a notification modal whenever a push notification is received (while the app is running). My app has a tab bar, and I've gotten this to partially work by pushing the notification modal onto the tab bar controller.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {        
    NotificationViewController *vc = [[NotificationViewController alloc] init];
    [tabBarController presentModalViewController:vc animated:YES];
    [vc release];
}

This seems to fail, however, when there is already a different modal open that hides the tab bar controller. What is the best way to make sure that the NotificationViewController always displays when a push notification is received, even if there is already a modal open that is hiding the tab bar controller?

A: 

There are two things you can do. First is to dismiss current modal controller, but it could confuse the user. The second thing would be something like that:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {      
    UIViewController* currentController = tabBarController;
    if ( [currentController modalViewController] != nil )
          currentController = [currentController modalViewController];

    NotificationViewController *vc = [[NotificationViewController alloc] init];
    [currentController presentModalViewController:vc animated:YES];
    [vc release];
}

Probably not the prettiest thing to do, as it opens another modal controller in a modal controller, but it works.

Estarriol
That will work fine for my purposes. Thank you.
Steve N