在特定 viewController 中处理 PushNotifcations
Handling PushNotifcations when in a specific viewController
当我的应用程序处于前台时,我正在尝试处理 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。
我做错了什么?
这是使用 NotificationCenter 的示例代码:
首先,您必须声明通知名称。在 Swift 3 之前,我们为此使用字符串,现在它应该作为 Notification.Name:
的扩展来完成
extension Notification.Name {
static let pushNotificationReceived = Notification.Name("pushNotificationReceived")
}
现在您可以post在您的方法中发送通知:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if userInfo["matchInfo"] != nil {
if application.applicationState == .active {
NotificationCenter.default.post(name: Notification.Name.pushNotificationReceived, object: nil)
}
else{
//create a new view controller and add it to the hierarchy
}
}
completionHandler(.newData)
}
现在任何对象都可以收听此通知并执行任何需要的操作。您只需要在需要时订阅和取消订阅。在您的情况下,明智的做法是在 viewWillAppear 和 viewDidDisappear 方法中执行此操作:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.refresh), name: Notification.Name.pushNotificationReceived, object: nil)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
NotificationCenter.default.removeObserver(self, name: Notification.Name.pushNotificationReceived, object: nil)
}
func refresh() {
//do your stuff here
}
当我的应用程序处于前台时,我正在尝试处理 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。
我做错了什么?
这是使用 NotificationCenter 的示例代码:
首先,您必须声明通知名称。在 Swift 3 之前,我们为此使用字符串,现在它应该作为 Notification.Name:
的扩展来完成extension Notification.Name {
static let pushNotificationReceived = Notification.Name("pushNotificationReceived")
}
现在您可以post在您的方法中发送通知:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if userInfo["matchInfo"] != nil {
if application.applicationState == .active {
NotificationCenter.default.post(name: Notification.Name.pushNotificationReceived, object: nil)
}
else{
//create a new view controller and add it to the hierarchy
}
}
completionHandler(.newData)
}
现在任何对象都可以收听此通知并执行任何需要的操作。您只需要在需要时订阅和取消订阅。在您的情况下,明智的做法是在 viewWillAppear 和 viewDidDisappear 方法中执行此操作:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.refresh), name: Notification.Name.pushNotificationReceived, object: nil)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
NotificationCenter.default.removeObserver(self, name: Notification.Name.pushNotificationReceived, object: nil)
}
func refresh() {
//do your stuff here
}