title: How to handle multiple remote-notification click in IOS
body: <p>I am trying to open different view controllers based on <code>notification</code> click but when <code>push notification</code> is received it automatically runs the controller code in background so when I open the app through app icon/notification from <code>notification centre</code> it loads the <code>view controller</code> immediately but when multiple notifications are received it loads first notification controller irrespective of whichever notification was tapped.</p>
  
  <blockquote>
    <p>Let's say I've got notification with titles "Evening","Morning" and
    "Night", it should open "Evening View Controller" when "Evening"
    notification is tapped but it loads "Night View Controller" when I go
    back it loads "Morning View Controller" and at last it loads "Evening
    View Controller". Before notification came, I was on Main Controller
    and then I moved the app into background.</p>
  </blockquote>
  
  <p>When I tap on "Morning" <code>notification</code> it doesn't do anything now.</p>
  
  <p>Earlier I tried using addObserver but result is the same so I move to app delegate. </p>
  
  <p>Here is the app delegate code</p>
  
  <pre><code>@interface AppDelegate ()
  
  @end
  
  
  @implementation AppDelegate
  
  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  
      [[UIApplication sharedApplication]setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
  
      // Override point for customization after application launch.
      // Register for Push Notitications, if running iOS 8 or More
  
      if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
  
          UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                          UIUserNotificationTypeBadge |
                                                          UIUserNotificationTypeSound);
          UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
          [application registerUserNotificationSettings:settings];
          [application registerForRemoteNotifications];
  
      } else {
          // Register for Push Notifications before iOS 8
  
          [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert |  UIRemoteNotificationTypeSound)];
      }
  
      [application setStatusBarHidden:YES];
  
      return YES;
  }
  
  // Handle remote notification registration.
  - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  
      NSLog(@"Device Token %@",[self stringWithDeviceToken:deviceToken]);
  
      [[NSUserDefaults standardUserDefaults]setObject:[self stringWithDeviceToken:deviceToken] forKey:@"registration_id"];
      [[NSUserDefaults standardUserDefaults] synchronize];
  }
  
  - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
  
      NSLog(@"Error in registration. Error: %@", err);
  }
  
  
  - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  
      NSDictionary *alertInfo = (NSDictionary *) userInfo[@"aps"][@"alert"];
      NSLog(@"%@",alertInfo);
  
      NSString *alertID = [userInfo valueForKey:@"alertid"];
  
  
      if (application.applicationState == UIApplicationStateBackground) {
  
          [UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
  
          NSLog(@"Background Mode");
          NSString *title = alertInfo[@"title"];
  
          [self openViewController:title];
      }
  
      if(application.applicationState == UIApplicationStateActive){
  
          NSLog(@"Active Mode");
             }
  
      completionHandler(UIBackgroundFetchResultNewData);
      [self performSelector:@selector(sendAckRequest:) withObject:alertID];
  }
  
  
  - (void)openViewController:(NSString *)notificationTitle{
  
      NSDictionary * userDict = [[NSUserDefaults standardUserDefaults] objectForKey:@"loginUser"];
  
      if([notificationTitle isEqualToString:@"Exercise"]){
  
      UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
  
      UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
  
      Excercise *excerciseController = (Excercise*)[mainStoryboard instantiateViewControllerWithIdentifier: @"Excercise"];       
      [navigationController pushViewController:excerciseController animated:YES];
      //[navigationController presentViewController:excerciseController animated:YES completion:nil];
  
      }else if([notificationTitle isEqualToString:@"Weight"]){
  
          UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
  
          UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
  
          Weight *weightController = (Weight*)[mainStoryboard instantiateViewControllerWithIdentifier: @"Weight"];           
         [navigationController pushViewController:weightController animated:YES];
  
          //[navigationController presentViewController:weightController animated:YES completion:nil];
  
      }else if([notificationTitle isEqualToString:@"MCQ"]){
  
          UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
  
          UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
  
          QuestionOfTheDay *questionController = (QuestionOfTheDay*)[mainStoryboard instantiateViewControllerWithIdentifier: @"QuestionOfTheDay"];
          questionController.self.dictUser = userDict;
          [navigationController pushViewController:questionController animated:YES]
      }
  }
  
  -(void)sendAckRequest:(NSString *)alertID {
  
      AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
      manager.requestSerializer = [AFHTTPRequestSerializer serializer];
      manager.responseSerializer = [AFHTTPResponseSerializer serializer];
  
      NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  
      NSString *userID =[[defaults objectForKey:@"loginUser"]objectForKey:@"UserId"];
      NSString *serverRegistrationID = [defaults objectForKey:@"server_registration_id"];
  
      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
  
      [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
      NSString *currentDateString = [dateFormatter stringFromDate:[NSDate date]];
  
      //NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
  
      //NSDictionary *parameter = @{@"ReminderDateTime":currentDateString};
  
      NSString *url = [NSString stringWithFormat:@"%@%@/%@/%@/?ReminderDateTime=%@",sendNotificationAck,userID,serverRegistrationID,alertID,currentDateString];
  
  
      NSLog(@"url: %@",url);
  
      [manager GET:url parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
  
          if(operation.response.statusCode == 200)
              NSLog(@"Notification Acknowledged");
          else
              NSLog(@"Notification failed to acknowledge");
  
      } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
  
          NSLog(@"error: %@",[error localizedDescription]);
      }];
  }
  
  - (NSString *)stringWithDeviceToken:(NSData *)deviceToken {
  
      const char *data = [deviceToken bytes];
  
      NSMutableString *token = [NSMutableString string];
  
      for (int i = 0; i &lt; [deviceToken length]; i++) {
  
          [token appendFormat:@"%02.2hhX", data[i]];
      }
  
      return token;
  }
  
  - (void)applicationDidBecomeActive:(UIApplication *)application {
      // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  
      if([UIApplication sharedApplication].applicationIconBadgeNumber!=0)
      [UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber - 1;
  }
  
  @end
  </code></pre>