【发布时间】:2016-10-06 20:55:11
【问题描述】:
当我的应用程序处于前台时,我正在尝试处理 PushNotifcation。
如果刚刚激活的ViewController 是一个特定的,我想重新加载它的数据。如果它处于非活动状态,我希望我的应用实例化一个特定的 viewController
这是我在appDelegate中的代码:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if userInfo["matchInfo"] != nil {
if application.applicationState == .active {
if self.window!.rootViewController is chatUebersichtTVC{
let uebersicht = self.window!.rootViewController as! chatUebersichtTVC
uebersicht.refresh()
}
else{
return
}
}
else{
print ("trying to activate")
let mainStoryBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let chat:chatUebersichtTVC = mainStoryBoard.instantiateViewController(withIdentifier: "chatUebersichtTVC") as! chatUebersichtTVC
let mainPageNav = UINavigationController(rootViewController: chat)
let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = mainPageNav
}
}
else{
PFPush.handle(userInfo)
}
completionHandler(.newData)
}
我可以接听self.window!.rootViewController is chatUebersichtTVC
但它不会在之后执行。此外,当应用关闭时,它始终会打开最后打开的控制器,而不是特定的 chatUebersichTVC。
我做错了什么?
【问题讨论】:
-
我建议在 AppDelegate 的 didReceiveRemoteNotification 中使用 NotificationCenter(用于活动状态)。在 viewWillAppear 中将您的视图控制器添加为观察者,并在 viewDidDisappear 中将其删除。这将确保您在正确的实例上进行刷新,并且如果您以后需要,也可以更轻松地刷新任何其他控制器。
-
你的代码是否进入了这个if语句
if self.window!.rootViewController is chatUebersichtTVC的主体? -
@alex 你有这个示例代码吗?
-
@AdeelMiraj 不,它挂在这个声明上。它没有进入它。
-
@JVS 你不应该依赖 self.window!.rootViewController 类型,因为它很容易成为一个 UINavigationController,你的 chatUebersichtTVC 在顶部并且检查会失败。
标签: ios parse-platform push-notification appdelegate