Will
Will

Reputation: 3044

Open View from didReceiveRemoteNotification

I have this question asked few times and I have tried a few different methods but have not been successful.

The latest method I have tried is as followed : I included the ViewController that I wanted to show. I then put this code in the didReceiveRemoteNotification method.

    CarFinderViewController *pvc = [[CarFinderViewController alloc] init];
   // [self.window.rootViewController presentViewController:pvc animated:YES completion:nil];
    [(UINavigationController *)self.window.rootViewController pushViewController:pvc animated:NO];

This did not work. I think the problem I may be having is that my initial view is not a navigation controller like a lot of the examples show.

enter image description here

This is a picture of my story board> The VC that I want to send the user to is the car finder (bottom right)

Can someone explain to me what I might be doing wrong?

Upvotes: 3

Views: 3991

Answers (2)

Engin Kurutepe
Engin Kurutepe

Reputation: 6757

I think the simplest solution would be to show the CarFinderViewController as a modal view instead of trying to push it to a navigation controller which may or may not be visible at the time.

Another important point to avoid further inconsistencies I'd recommend you instantiate your CarFinderViewController from the storyboard instead of directly through the class methods.

Something like:

UIViewController * vc = self.window.rootViewController;
// You need to set the identifier from the Interface
// Builder for the following line to work
CarFinderViewController *pvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"CarFinderViewController"];
[vc presentViewController:pvc animated:YES completion:nil];

Upvotes: 2

nsgulliver
nsgulliver

Reputation: 12671

You could use basically postNotification when you receive the Remote Notification for exmaple in your didReceiveRemoteNotification post notification like this

[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];

now in your FirstViewController's you can register the FirstViewController for this notification like this

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushNotificationReceived) name:@"pushNotification" object:nil];

and in your method

-(void)pushNotificationReceived{

CarFinderViewController *pvc = [[CarFinderViewController alloc] init];
[self presentViewController:pvc animated:YES completion:nil];

}

don't forget to remove the observer from notification in your dealloc method

-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

Upvotes: 5

Related Questions