Search code examples
iosios7push-notificationapple-push-notificationsuilocalnotification

Display an Alert popup when application is receiving an Apple Push Notification in iOS


I am creating a Chat kind of iPhone application using Apple push notification services. APN's is working fine and i am getting notification when user is receiving new message. So, i have set one Toast pop up in didReceiveRemoteNotification of my App Delegate class. The problem is, i am getting Toast pop up in every View Controller screen because i have added Toast on my main window itself. but can you please help me that how can i hide this Toast pop up from one of my Chat List View Controller screen. How can i check which View Controller is loaded currently on my window view, when application is in foreground?

here is my code :

- (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];
}

Thanks!


Solution

  • Declare below methods in 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];
    

    }