7

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;
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hacker
  • 8,919
  • 12
  • 62
  • 108

3 Answers3

37

You can try with the local notification NSNotificationCenter to reload your table when a push notification is received.

When a push notification is received then fire your local notification. In your view controller, listen to the local notification and perform the task.

For example:

In your didReceiveRemoteNotification:

[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTheTable" object:nil];

In your ViewController add Observer (in viewDidLoad):

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

and implement the following method:

- (void)reloadTable:(NSNotification *)notification
{
    [yourTableView reloadData];
}

Also remove observer in viewDidUnload or viewWillDisappear.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Maulik
  • 19,348
  • 14
  • 82
  • 137
  • 1
    That worked perfectly, I actually logged in to StackOverflow just to upvote you. Never knew about Observers in iOS, thank you – Slavenko Miljic Jul 05 '13 at 12:11
  • Thank you very much!!!! I use it to display a custom notification when recived a remote notification and app is in foregorund. You save my day :D +1! – Tenaciousd93 Mar 20 '14 at 09:38
  • 1
    Don't you have to remove Observer in viewWillDisappear too? – carmine Jul 03 '14 at 13:55
  • @carmine: yes true..you should.. its just an idea. Anyways I have edited the answer so it might be helpful. Thanks – Maulik Jul 04 '14 at 07:06
4

Swift 3 version:

in didReceiveRemoteNotification

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadTheTable"), object: nil)

in your ViewController add Observer (in viewDidLoad):

NotificationCenter.default.addObserver(self, selector: #selector(self.reloadTable), name: NSNotification.Name(rawValue: "reloadTheTable"), object: nil)

and in viewWillDissapear

 NotificationCenter.default.removeObserver("reloadTheTable")
Faruk Duric
  • 794
  • 1
  • 7
  • 10
3

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

abanet
  • 1,327
  • 17
  • 22