收到通知时刷新表视图

问题描述 投票:1回答:2

我在我的应用程序非常小的聊天。每当用户接收到消息时,服务器发送的通知。在的appDelegate我已经实现代码如下:

@available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler(.alert)
//        print(UNNotificationAction)
        print("📲📲📲 recieved")
        if notification.request.content.title.contains("Message")
        {
            print("sub")
            print(notification.request.content.subtitle)
            print("body")
            print(notification.request.content.body)
            print("it containt DM ⏰")

            let storyboard:UIStoryboard = UIStoryboard(name:"Chat", bundle: nil)
            let vc = storyboard.instantiateViewController(withIdentifier: "chatRoomVc") as! ChatRoomViewController
            vc.becomeFirstResponder()

            vc.setRefreshListener {
                print("triggered")
            }

        }

    }

每当它运行时,它会调用API并获取聊天数据。下面的代码展示了如何获取数据:

 func setRefreshListener(listener:@escaping ()->Void){
    refreshListener = listener
    presenter.getttingChatRoomData(aptId: UserDefaults.standard.string(forKey: userAPTID)!, id: UserDefaults.standard.string(forKey: userID)!)
    presenter.attachView(view: self)
    super.loadView()
    super.reloadInputViews()
    tableView.reloadData()


}

此外,我可以正常获取数据。

问题是,它不重新加载数据为表视图!!

没有任何人有任何解决方案为什么会这样?

更新:下面运行的代码时,接收到的数据:

 func gettingUserChatWithSUCCESS(responce: [chatRomModel]) {
        print("✅ getting chat room data SUCCESS")
        data = responce
        tableView.reloadData()
    }
ios swift uitableview push-notification listener
2个回答
0
投票

您可以使用解决观察者通知这个问题,就像遵循这个代码。

新增通知观察者在你的ViewController

NotificationCenter.default.addObserver(self, selector: #selector(onReceiveData(_:)), name: "ReceiveData", object: nil)

@objc func onReceiveData(_ notification:Notification) {
    // Do something now //reload tableview
}

appDelegate.swift文件中使用此代码调用观察者收到通知方法

NotificationCenter.default.post(name: Notification.Name("ReceiveData"), object: nil)

注: - 删除通知观察者,当你的ViewController会消失,使用此代码

override func viewWillDisappear(_ animated: Bool) {
      super.viewWillDisappear(animated)
      NotificationCenter.default.removeObserver(self, name: "ReceiveData", object: nil)
}

0
投票

实施UNUserNotificationCenterDelegate方法并发布您的通知。

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {

    let userInfo = response.notification.request.content.userInfo
    if UIApplication.shared.applicationState == .background || UIApplication.shared.applicationState == .inactive {
                NotificationCenter.default.post(name: .newMessage, object: nil, userInfo:userInfo)
     }

}

在你看来控制器添加一个观察者来检查的通知(添加您自己的选择方法。)

  NotificationCenter.default.addObserver(self,
                                        selector: #selector(updateMessages(notification:)),
                                        name: .newMessage,
                                        object: nil)



    @objc func updateMessages(notification: NSNotification) {
     // Handle your notifications...
   }
© www.soinside.com 2019 - 2024. All rights reserved.