2

I have an iOS application in which I am using the push notification to notify the user when the correct answer is posted. If the application is open and if I click on the notification it goes to the specified controller. But if the application is closed and if I receive the notification it does not goes to the specified controller.

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSLog(@"User Info %@",userInfo);
    NSString* alertValue = [[userInfo valueForKey:@"aps"] valueForKey:@"badge"];
    NSLog(@"my message-- %@",alertValue);
    int badgeValue= [alertValue intValue];

//    NSNumber *identifierString = [[[userInfo valueForKey:@"aps"]valueForKey:@"details"]valueForKey:@"identifire"];
//    NSLog(@"identifierString %@",identifierString);
    NSString *alertMessage = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
    NSLog(@"ALertMessage %@",alertMessage);
    if ([alertMessage isEqualToString:@"New answer added."]) {
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badgeValue];
//        [[NSUserDefaults standardUserDefaults] setObject:[[[[userInfo valueForKey:@"aps"] valueForKey:@"details"] objectAtIndex:0]valueForKey:@"question"]forKey:@"notificationQuestion"];
//        [[NSUserDefaults standardUserDefaults] setObject:[[[[userInfo valueForKey:@"aps"] valueForKey:@"details"] objectAtIndex:0]valueForKey:@"user_image"]forKey:@"notificationImage"];
//        NSLog(@"Image %@",[[NSUserDefaults standardUserDefaults]valueForKey:@"notificationQuestion"]);


        NSLog(@"User Information %@",userInfo);
        NSLog(@"User Details %@",[[userInfo valueForKey:@"aps"] valueForKey:@"details"]);
       // NSLog(@"User Details 1%@",[[[userInfo valueForKey:@"aps"] valueForKey:@"details"] objectAtIndex:0]);
       // NSLog(@"User Details 1%@",[[[[userInfo valueForKey:@"aps"] valueForKey:@"details"] objectAtIndex:0]valueForKey:@"question"]);
        pushDictonary = [[userInfo valueForKey:@"aps"] valueForKey:@"details"];


    }


    //NSArray *pushDetails = [[userInfo valueForKey:@"aps"] valueForKey:@"details"];
   // NSLog(@"Push Details %@",pushDetails);
    if (application.applicationState == UIApplicationStateActive ) {

//        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
//        localNotification.userInfo = userInfo;
//        localNotification.soundName = UILocalNotificationDefaultSoundName;
//        localNotification.alertBody = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
//        localNotification.fireDate = [NSDate date];
        if ([alertMessage isEqualToString:@"New answer added."])
        {
           [self createNotificationViewwithUserDictionary:userInfo];

        }


    }
    else if (application.applicationState == UIApplicationStateBackground)
    {
        NSLog(@"YES");
    }
    else if (application.applicationState == UIApplicationStateInactive)
    {
        NSLog(@"YES");
        if ([alertMessage isEqualToString:@"New answer added."])
        [self pushdetailsViewController];
    }

For going to the controller :

   -(void)pushdetailsViewController
{

        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
         QPYourQuestionController *controller = (QPYourQuestionController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"yourQuestion"];
      [[QPCommonClass initializeUserDefaults]setObject:[pushDictonary valueForKey:@"question_id"] forKey:@"currentquestionID"];
        NSLog(@"Dictionary %@",pushDictonary);

        UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
        [navigationController pushViewController:controller animated:YES];



}
Mukesh Ram
  • 6,248
  • 4
  • 19
  • 37
Mahin
  • 61
  • 10
  • 1
    Hard to help, as you didn't post the code that handles notifications. – Avi Jul 20 '16 at 06:18
  • you should write down the methos i.e didReceiveNotification:Conpletion handler it will help you to navigate to the view controller even if the app is in killed state – Diksha Jul 20 '16 at 06:22
  • @PranavGupte I will check and update soon – Mahin Jul 20 '16 at 06:26

2 Answers2

2

For the situation when application is closed the remote notification delegate

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

is not called

In this case the call goes to the following method:

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Hence you can do this to take the user to the VC you want

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // ...

    if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
       // Do whatever you want
    }
}
iOS Monster
  • 2,099
  • 1
  • 22
  • 49
  • Correct answer, when application is in kill ( terminated ) state, it will resume will didFinishLaunchingWithOptions . – Hasya Jul 22 '16 at 05:19
1

add this code to your app delegate file

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    //you will get notification data in userInfo dict

    UIStoryboard * mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    notificationViewController * notificationVC = [mainStoryboard instantiateViewControllerWithIdentifier: @"notificationViewController"];//set your controller here 

    [(UINavigationController *)self.window.rootViewController pushViewController:notificationVC animated:YES];

    completionHandler(UIBackgroundFetchResultNewData);
 }
Pranav
  • 701
  • 4
  • 18
  • 1
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { this is where you you do your job when post notification received – Devang Tandel Jul 20 '16 at 07:00