i created a small iOS app with multiple Views. There is also a chat view on which I use jsqmessagesViewVontroller. I have implemented push notifications to call a method which asks my server to get new messages. The Method is within the chatController. When a push notification is received a singleton property with an property observer is set. The Observer creates a new object of the Controller, sets the required variables and calls the receiveChat() Method. To this point everything works fine. But when the chatController is opened and a new push notification receives the chat won't be refreshed. I've also tried to realize it with the NSNotificationCenter but it also didn't worked for me. How can I call the open chat view that there are new Messages for the corresponding chat and refresh the view so that the new Messages are displayed or tell the chat view when it's opened that it should reload the chat messages from CoreData to refresh the chat?
My Property Observer:
var newChat: Bool = false {
didSet {
let vc = ChatController()
vc.setupClass()
vc.receiveChat()
}
}
My AppDelegate to handle the notification (including the line on which I tested the NSNotificationCenter approach):
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
NSNotificationCenter.defaultCenter().postNotificationName(NSNotificationcenterKeys().reloadChatNotificationKey, object: self)
//sharedData.newChat = true
}
ChatController registering the NSNotification Observer:
func registerObserver() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ChatController.doNotification(_:)), name: NSNotificationcenterKeys().reloadChatNotificationKey, object: nil)
}
ChatController NSNotification method:
func doNotification(note: NSNotification) {
receiveChat()
}
ChatController receive Message method:
func receiveChat() {
... prepare the request...
Alamofire.request(.GET, req, parameters: parameters, headers: authHeader).validate().responseJSON {
response in
debugPrint(response)
if response.result.error == nil {
... a lot json stuff happens here...
self.dataManager.save()
if self.chat.serverId == chatId {
self.addMessage(author, text: nachricht, date: NSDate(timeIntervalSince1970: timestamp/1000))
self.finishReceivingMessage()
}
} catch {
print(error)
}
}