Reputation: 883
I want to display multiple (remote) notifications in a table view. My problem is, that only one message is being displayed.
In my AppDelegate.swift i've got this:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]){
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let rVC = storyboard.instantiateViewControllerWithIdentifier("PushVC")
let push = rVC as! PushVC
self.window!.rootViewController = rVC
if let aps = userInfo["aps"] as? NSDictionary {
if let alert = aps["alert"] as? NSDictionary {
if let message = alert["message"] as? NSString {
push.addPushNote(message as String)
}
} else if let alert = aps["alert"] as? NSString {
push.addPushNote(alert as String)
}
}
}
addPushNote is a method in my ViewController to add new Notifications to the table view:
public func addPushNote(message: String){
pushNotes.append(message)
table.reloadData()
print(message)
}
When receiving multiple messages, print(message) shows me all of them - but just the first one gets displayed. I suspect, that for every push notification the message is added to a different instance of PushVC.
Any help would be highly appreciated.
Upvotes: 3
Views: 1130
Reputation: 14571
Its because of different instance for which you are again and again changing the root view controller and we have a fresh instance of the VC in which you have your table view. In order to solve your problem keep all your notifications in Array and store that array in user default.
Later, whenever any new notifications arrives, then first retrieve the array from UserDefaults and append the notification there and save that array back in userdefaults.
Also make sure to give the datasource of table view as your stored NSUserDefault array.
You can also store your notifications in CoreData, Sqlite etc
Upvotes: 3