How to reload a table view when I receive a push notification?

I have an iPhone app in which I add push notifications.

When I receive a push notification, I need to go to a specific view, where I load the table view after calling the web service Here. The problem is that I stand at one glance. If I received a push message, I need to reload the tableview both in the foreground and in the background. But when I do this, it does not work correctly. How to 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; } } 
+4
source share
2 answers

You can try the local NSNotificationCenter notification to reload the table when you receive a push notification.

When a push notification is received, start the local notification. In your view controller, listen for the local notification and complete the task.

For instance:

In your didReceiveRemoteNotification:

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

In your ViewController add an 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 the observer in viewDidUnload or viewWillDisappear .

+35
source

Swift 3 version:

in didReceiveRemoteNotification

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

in your ViewController add an 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") 
+3
source

All Articles