开发者

Show modal upon receiving push notification

I need to show a notification modal whenever a push notification is received (while the app is running). My app has a tab bar,开发者_JAVA技巧 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜