iOS 6: update view after push notification was rec

2019-05-07 01:51发布

问题:

After the app receives a push notification, I'd like to change titles of some buttons on my main ViewController. To achieve this behavior, I overwrote in my app delegate the method application: didReceiveRemoteNotification: to re-instantiate the UINavigationController with the controller I'd like to update as its root view controller, setting the buttons' titles to whatever I want:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
       UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.welcomeViewController];
       [self.window setRootViewController:navController];
       [self.welcomeViewController.buttonToUpdate setTitle: @"Updated Text" forState: UIControlStateNormal];
}

While this may not be the best solution (maybe I could forget the UIButtons altogether and make the view controller use a UITableView with its rows acting as buttons?), but it works for the following scenarios:

1) The app in in the foreground. Push notification alert pops up, user touches OK, the view is updated fine.

2) The app in the background/closed. Device is in locked mode. Push notifications arrives, user unlocks the device, app is loaded, view is also updated fine.

The problem seems to arise when user is using another app for example, and push notification arrives, but the user opens the app NOT through the push notification but by tapping the app icon. In that case, application: didReceiveRemoteNotification: seems not be called, and the view in questions never gets updated.

Hope my explanation is clear. I'm open to suggestions on different approaches, or how to handle that last scenario using my approach.

Thanks!

回答1:

From the APNS development guide :

Let’s review the possible scenarios when the operating delivers a local notification or a remote notification for an application.

The notification is delivered when the application isn’t running in the foreground. In this case, the system presents the notification, displaying an alert, badging an icon, perhaps playing a sound.

As a result of the presented notification, the user taps the action button of the alert or taps (or clicks) the application icon. If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate’s application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).

If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification.

The behavior you are experiencing is the expected behavior when tapping the app icon. In this case, it's as if the user started the app normally and no push notification arrived. The only way (I can think of) for you to display something different in this case is to contact your server at the launch of your application and get some information that indicates a push notification was recently sent to that device.