How do I reload the tableview when I am getting the push notification?

Viewed 7635

I have an iPhone application in which I am adding push notifications.

When I am receiving the push notification I need to go to a particular view where I am loading a table view after calling a web service here. The problem is when I am standing in the same view. If I got a push message I need to reload the tableview both in the foreground and background. But when I am doing that it is not working correctly. How do I achieve this?

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

    NSLog(@"########################################didReceiveRemoteNotification****************************###################### %@",userInfo);

    // Check application in forground or background
    if (application.applicationState == UIApplicationStateActive)
    {
        if (tabBarController.selectedIndex==0)
        {
            NSArray *mycontrollers = self.tabBarController.viewControllers;
            NSLog(@"%@",mycontrollers);
            [[mycontrollers objectAtIndex:0] viewWillAppear:YES];
            mycontrollers = nil;
        }

        tabBarController.selectedIndex = 0;
        //NSLog(@"FOreGround");
        //////NSLog(@"and Showing %@",userInfo)
    }
    else {
        tabBarController.selectedIndex = 0;
    }
}
3 Answers

Swift 4 version: in didReceiveRemoteNotification

NotificationCenter.default.post(name: Notification.Name(rawValue: "reloadEventsTable"), object: nil)

in your ViewController add Observer (in viewDidLoad):

NotificationCenter.default.addObserver(self, selector: #selector(onReloadEventsTable), name: Notification.Name(rawValue: "reloadEventsTable"), object: nil)

About removing the observer I used to do this in deinit() but it's interesting to know that from iOS 9 you don't need to remove observers yourself, if you're not using block based observers though. https://stackoverflow.com/a/40339926/3793165

Related