收到通知时刷新表视图

时间:2019-02-12 06:07:19

标签: ios swift uitableview push-notification listener

我的应用程序中的聊天很小。每当用户收到消息时,服务器就会发送通知。在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()
    }

3 个答案:

答案 0 :(得分:0)

您可以使用通知观察器解决此问题,就像遵循以下代码一样。

在您的视图控制器中添加通知观察器

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)

注意:-使用此代码,当您的viewcontrller消失时,删除通知观察器

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

答案 1 :(得分: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...
   }

答案 2 :(得分:0)

包括 SWIFT 5.4

let myNotificationKey = "notificationKey" //Declare variable at the global level

NotificationCenter.default.addObserver(self,
                                       selector: #selector(notifyMe),
                                       name: NSNotification.Name(rawValue: myNotificationKey),
                                       object: nil) //In First VC's viewDidLoad()

func notifyMe() { print("Update tableview") } //In First VC

NotificationCenter.default.post(name: Notification.Name(rawValue: myNotificationKey), object: self) 
//Use this from you want to update the notification center for eg: OnClick of UIButton