I try to open different view controllers based on notification click, but when I receive push notification it automatically runs the controller code in the background, so when I open the application through the application icon / notification from the notification centre , it loads the view controller immediately, but when several of notifications, it loads the first notification controller, regardless of which of the notifications was listened to.
Say I have a notification with the headings “Evening”, “Morning” and “Night”, it should open “Evening View Controller” when “Evening”, the notification is listened, but it loads the “Night View Controller” when I go back loads the “Morning View Controller” and finally loads the “Evening” View Controller. Before the notification, I was on the Main Controller and then I moved the application to the background.
When I click the "Morning" notification , it does nothing now.
I used to try using addObserver, but the result is the same, so I move on to the application delegate.
Here is the application delegation 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 < [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