Reputation: 77
I have a very small chat in my app. whenever a user receives a message, the server sends a notification. In appDelegate I have implemented the code below:
@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")
}
}
}
and whenever it runs, it calls the API and gets the chat data. the code below shows how it gets the data:
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()
}
in addition, I can get the data properly.
THE PROBLEM is it does not reload the data for the table view!!
does anybody has any solution why this is happening?
UPDATE: the code below runs when data is recieved:
func gettingUserChatWithSUCCESS(responce: [chatRomModel]) {
print("✅ getting chat room data SUCCESS")
data = responce
tableView.reloadData()
}
Upvotes: 4
Views: 4229
Reputation: 629
INCLUDING WITH 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
Upvotes: 0
Reputation: 4570
You can solve this issue using notification observer, Like follow this code.
Add notification observer in your viewcontroller
NotificationCenter.default.addObserver(self, selector: #selector(onReceiveData(_:)), name: "ReceiveData", object: nil)
@objc func onReceiveData(_ notification:Notification) {
// Do something now //reload tableview
}
call observer using this code in your appDelegate.swift
file receive notification method
NotificationCenter.default.post(name: Notification.Name("ReceiveData"), object: nil)
Note:- Remove notification observer when your viewcontrller will Disappear, using this code
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self, name: "ReceiveData", object: nil)
}
Upvotes: 10
Reputation: 2859
Implement UNUserNotificationCenterDelegate
method and post your notification.
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)
}
}
In your view controller add an observer to check on notifications ( Add your own selector method.)
NotificationCenter.default.addObserver(self,
selector: #selector(updateMessages(notification:)),
name: .newMessage,
object: nil)
@objc func updateMessages(notification: NSNotification) {
// Handle your notifications...
}
Upvotes: 1