【问题标题】:Handling PushNotifcations when in a specific viewController在特定视图控制器中处理 PushNotifcations
【发布时间】: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


【解决方案1】:

这是一个使用 NotificationCenter 的示例代码:

首先,您必须声明通知名称。在 Swift 3 之前,我们为此使用字符串,现在它应该作为 Notification.Name 的扩展来完成:

extension Notification.Name {
    static let pushNotificationReceived = Notification.Name("pushNotificationReceived")
}

现在您可以在您的方法中发布通知:

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
}

【讨论】:

  • 所以我只是收到通知,不是吗?我真的不需要弹出通知。
  • 对不起,我刚刚看到错误是由于我的远程设置和您的代码造成的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多