Reputation: 3146
I'm new to iOS development and I'm having a little trouble understanding how to make sure a view controller gets a push notification regardless if the view is opened or not.
Basically, what I want is the view controller interface to get updated with the push notification message.
I have a storyboard that is set up like this:
Navigation Controller--> View Controller 1 --> View Controller 2 --> View Controller 3
I want View Controller 3 to get updated. However, in my app delegate -- it receives the remote notifications. So, it knows what the message is.
I've tried several things and I can get View Controller 3 to update ONLY if its view controller is in the foreground and the user is looking at that View.
However, if the user is on View Controller 1 -- then when I receive a push notification and I go to the View Controller 3 -- it doesn't update the data.
My only assumption is that View Controller 3 has been deallocated since the view hasn't loaded. So, it's not able to listen for the notifications. How can I get View Controller 3 to listen for remote notifications?? Do I have to have some kind of reference to it in my app delegate file? If so, How do I set that up??
In my app delegate file, method: didReceiveRemoteNotification:
[[NSNotificationCenter defaultCenter] postNotificationName:messageNotificationName object:nil userInfo:userInfo];
In my View Controller 3, viewDidLoad Method, I register for notifications:
// register to find out when push notifications are received.
NSNotificationCenter * notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(notificationRecieved:)
name:messageNotificationName
object:nil];
Currently, I haven't unregistered for the notification -- b/c I want it to receive it even when the view isn't loaded.
Any help or suggestions would be greatly appreciated.
I've been reading the Apple Local and Remote Notification Guide. I have also researched a TON on stackoverflow trying to find something that will help me and point me in the right direction.
I have also written some code for the didFinishLaunchingWithOptions just in case the application is opened from the notification.
NSDictionary *notif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
//Accept push notification when app is not open
if (notif) {
// Extract the payload
NSDictionary *tempDict = [notif valueForKey:@"aps"];
[[NSNotificationCenter defaultCenter] postNotificationName:messageNotificationName object:nil userInfo:tempDict];
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
ViewController3 *controller = (ViewController3*)[mainStoryboard instantiateViewControllerWithIdentifier: @"ViewController3"];
[navigationController pushViewController:controller animated:YES];
}
Upvotes: 1
Views: 1238
Reputation: 719
It seems as though your posting the notification before ViewController3
is instantiated. If you place breakpoints when you post the notification and when you set up the notification listener, you'll notice that the listener is set up after the notification fires.
Architecturally speaking, you could probably not have the notification at all. Simply update your data model, then have ViewController3
utilize your data model to populate it's views.
Upvotes: 1