uchicago-mobi / 2015-Winter-Forum

8 stars 1 forks source link

Local Notifications #192

Closed psajja closed 9 years ago

psajja commented 9 years ago

My application has a tab bar view controller with 3 view controllers.

When the user clicks on the local notification, I would like to have the 3rd tab of the tab bar view controller be displayed. Is there a method to get a handle on all the list of view controller in the tab bar vc? Thanks.

JRam13 commented 9 years ago

Yes. You should do it here:

AppDelegate.m

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler
{
     if ([identifier isEqualToString: @"??????"]) { //this if statement is up to you
          //change tab bar here
          UITabBarController *tab = (UITabBarController *)self.window.rootViewController;
          tab.selectedIndex = 2;
    }

    // Must be called when finished
    completionHandler();
}

Let me know if this works.

JRam13 commented 9 years ago

Notice I updated the code.

psajja commented 9 years ago

Thanks Jonny. I tried this but it appears to me that this method is not called when I click on the local notification. I tried to call the other method and it works fine.

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

}
tabinks commented 9 years ago
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler

is for interactive notifications.

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

is for non-interactive notifications where you click on the notification.

psajja commented 9 years ago

Thanks. Now it works !!

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{
    // Handle the notificaton when the app is running
    NSLog(@"Recieved Notification %@",notif);

    // Launch check in tab
    UITabBarController *tab = (UITabBarController *)self.window.rootViewController;
    tab.selectedIndex = 2;

}
JRam13 commented 9 years ago

Yep! There you go. Thanks Andrew.

For some reason, Apples documentation makes it sound like

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

only works if the app is in the foreground. "Handling a local notification when an app is already running". Obviously this isn't the case.

psajja commented 9 years ago

Thank you Jonny and Andrew.