how to create after receiving local notification data pass to Main view controller using objective c?

1k views Asked by At

I have creating local notification process using objective C for iPhone application. I want to pass the data after didReceiveLocalNotification to Main view controller.

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {


    if (app.applicationState == UIApplicationStateInactive ) {
        NSLog(@"app not running");

       // I need to pass the data to main view controller 


    }else if(app.applicationState == UIApplicationStateActive )  {
        NSLog(@"app running");


        // I need to pass the data to main view controller

    }

    NSLog(@"Recieved Notification %@",notif);  // Handle the notificaton when the app is running 

}
1

There are 1 answers

9
ChintaN -Maddy- Ramani On BEST ANSWER

you can use NSNotificationCenter to pass data.

in MainViewController.m

in viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifMainController:) name:@"notifMainController" object:nil];

-(void)notifMainController:(NSNotification *)notif
{
    NSLog(@"%@",notif.object);
}

in AppDelegate

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif 
{

 NSLog(@"Recieved Notification %@",notif);  // Handle the notificaton when the app is running 

if (app.applicationState == UIApplicationStateInactive ) 
{
    NSLog(@"app not running");
   // I need to pass the data to main view controller 
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            MainViewController *obj = [[MainViewController alloc]initWithNibName:@"MainViewController" bundle:nil];
            [self.navC pushViewController:obj animated:NO];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"notifMainController" object:@"your string"];
        });

}
else if(app.applicationState == UIApplicationStateActive )  
{
    NSLog(@"app running");
    // I need to pass the data to main view controller
    [[NSNotificationCenter defaultCenter] postNotificationName:@"notifMainController" object:@"your string"];
}

}

you can pass string in [[NSNotificationCenter defaultCenter] postNotificationName:@"notifMainController" object:@"your string"]; like this in object. Maybe this will help you.