Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

iphone

ipad

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;
    }
}
like image 732
hacker Avatar asked Jul 20 '12 06:07

hacker


People also ask

How do I activate push notifications?

Turn on notifications for Android devicesTap More on the bottom navigation bar and select Settings. Tap Turn on notifications. Tap Notifications. Tap Show notifications.

What is push notification setting?

Push notifications look like SMS text messages and mobile alerts, but they only reach users who have installed your app. All the mobile platforms – iOS, Android, Fire OS, Windows and BlackBerry – have their own services for supporting push.

How do I download push notifications?

From the “Settings” menu, tap “Notifications”. From here, find the app you wish to receive push notifications for. From here, tap “Allow Notifications” and then choose your options for how you wish to receive push notifications: a.


2 Answers

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.

like image 154
Maulik Avatar answered Sep 23 '22 21:09

Maulik


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")
like image 31
Faruk Duric Avatar answered Sep 21 '22 21:09

Faruk Duric