应用程序收到推送通知后,我想更改主 ViewController 上某些按钮的标题。为了实现这种行为,我在我的应用程序委托(delegate)中重写了 application: didReceiveRemoteNotification: 方法,以使用我想要更新为根 View Controller 的 Controller 重新实例化 UINavigationController,将按钮的标题设置为我想要的任何内容:

- (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];
}

虽然这可能不是最好的解决方案(也许我可以完全忘记 UIButtons 并让 View Controller 使用 UITableView 并将其行作为按钮?),但它适用于以下场景:

1)应用程序在前台。弹出推送通知警报,用户点击确定, View 更新良好。

2)后台/关闭的应用程序。设备处于锁定模式。推送通知到达,用户解锁设备,加载应用程序, View 也更新得很好。

例如,当用户正在使用另一个应用程序并且推送通知到达时,问题似乎会出现,但用户不是通过推送通知而是通过点击应用程序图标打开应用程序。在这种情况下,application: didReceiveRemoteNotification: 似乎不会被调用,并且问题中的 View 永远不会更新。

希望我的解释清楚。我愿意接受关于不同方法的建议,或者如何使用我的方法处理最后一个场景。

谢谢!

最佳答案

来自 APNS 开发指南:



您遇到的行为是点击应用程序图标时的预期行为。在这种情况下,就好像用户正常启动了应用程序并且没有推送通知到达。在这种情况下,您显示不同内容的唯一方法(我能想到)是在您的应用程序启动时联系您的服务器,并获取一些表明最近向该设备发送了推送通知的信息。

关于iOS 6 : update view after push notification was received, 但应用程序已关闭,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14944024/

10-10 05:03
查看更多