收到通知时刷新 table 视图
refresh table view when notification is received
我的应用程序中有一个非常小的聊天。每当用户收到消息时,服务器都会发送通知。在 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()
}
此外,我可以正常获取数据
问题是它没有为 table 视图重新加载数据!!
有人知道为什么会发生这种情况吗?
更新:
收到数据时运行以下代码:
func gettingUserChatWithSUCCESS(responce: [chatRomModel]) {
print("✅ getting chat room data SUCCESS")
data = responce
tableView.reloadData()
}
你可以使用notification observer解决这个问题,Like follow this code。
在您的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)
注意:- 当您的 viewcontrller 将消失时删除通知观察器,使用此代码
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self, name: "ReceiveData", object: nil)
}
实施 UNUserNotificationCenterDelegate
方法并 post 您的通知。
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...
}
包含 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
我的应用程序中有一个非常小的聊天。每当用户收到消息时,服务器都会发送通知。在 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()
}
此外,我可以正常获取数据
问题是它没有为 table 视图重新加载数据!!
有人知道为什么会发生这种情况吗?
更新: 收到数据时运行以下代码:
func gettingUserChatWithSUCCESS(responce: [chatRomModel]) {
print("✅ getting chat room data SUCCESS")
data = responce
tableView.reloadData()
}
你可以使用notification observer解决这个问题,Like follow this code。
在您的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)
注意:- 当您的 viewcontrller 将消失时删除通知观察器,使用此代码
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self, name: "ReceiveData", object: nil)
}
实施 UNUserNotificationCenterDelegate
方法并 post 您的通知。
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...
}
包含 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