0

我正在使用Apple push notification services創建一個Chat類型的iPhone應用程序。 APN的工作正常,當用戶收到新消息時,我得到notification。所以,我已經在我的App Delegate類的didReceiveRemoteNotification中彈出一個Toast彈出。問題是,我得到的烤麪包彈出在每個View Controller屏幕,因爲我已經在我的main window本身添加了吐司。但你能幫我,我怎麼能隱藏這Toast彈出我的Chat List View Controller屏幕之一。我如何檢查當前應用程序在​​的窗口視圖中當前是否加載了哪個View Controller當應用程序在iOS中收到Apple推送通知時顯示警報彈出框

這裏是我的代碼:

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo 
{ 
    if (application.applicationState == UIApplicationStateActive){ 

     NSString *personName = [[userInfo valueForKey:@"aps"] valueForKey:@"user_name"]; 
     NSString *meassge = [NSString stringWithFormat:@"New message from %@.", personName]; 

     [[self window] makeToast:meassge duration:1.0 position:@"center"]; 


     [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTheTable" object:nil]; 
    } 

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; 
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 
} 

謝謝!

回答

0

聲明如下方法AppDelegate.m

- (UIViewController*)topViewController { 
    return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController]; 
} 

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController { 
    if ([rootViewController isKindOfClass:[UITabBarController class]]) { 
     UITabBarController* tabBarController = (UITabBarController*)rootViewController; 
     return [self topViewControllerWithRootViewController:tabBarController.selectedViewController]; 
    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) { 
    UINavigationController* navigationController = (UINavigationController*)rootViewController; 
    return [self topViewControllerWithRootViewController:navigationController.visibleViewController]; 
    } else if (rootViewController.presentedViewController) { 
    UIViewController* presentedViewController = rootViewController.presentedViewController; 
    return [self topViewControllerWithRootViewController:presentedViewController]; 
    } else { 
    return rootViewController; 
    } 
    } 

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo 
{ 
    if (application.applicationState == UIApplicationStateActive){ 

    NSString *personName = [[userInfo valueForKey:@"aps"] valueForKey:@"user_name"]; 
    NSString *meassge = [NSString stringWithFormat:@"New message from %@.", personName]; 
    if(![[self topViewController] isKindOfClass:[ChatListViewController class]])//ChatListViewController is your viewcontroller 
     [[self window] makeToast:meassge duration:1.0 position:@"center"]; 


    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTheTable" object:nil]; 
    } 

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; 
[[UIApplication sharedApplication] cancelAllLocalNotifications]; 

}

+0

@ RamyaIt不工作,我已經試過您的代碼。儘管如此,我仍然在我的ChatListViewController類中烤麪包。 – user2786 2014-09-25 06:34:12

+0

請檢查一次topViewController是否通過放置NSLog(@「top view controller is%@」,[self topViewController])返回ChatListViewController;之前如果條件 – 2014-09-25 06:58:24

+0

我得到[自我topViewController] SWRevealViewController在我的控制檯。其實我通過使用SWRevealViewController呈現我的ChatListViewController。 – user2786 2014-09-25 07:13:22

相關問題