Sergey
Sergey

Reputation: 35

iOS click on push notification open concrete viewcontroller

Is it possible to open concrete viewcontroller when user tap on push notification?

I have news app, main view controller have a lot of news, and when user tap on some new I open next viewcontroller with description of this news.

When user tap push notification, how I can open second view controller?

I tried to send Notification to my UINavigationController and push two controllers, but it doesn't work fine, it open only first viewcontroller.

Upvotes: 0

Views: 1252

Answers (1)

Soto_iGhost
Soto_iGhost

Reputation: 671

you can handle your app, when you tap a local or push notification, implementing the application:didReceiveLocalNotification: or application: didReceiveRemoteNotification: method in your AppDelegate.m

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

}

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

}

If your app it's closed (not in background) you can verify if you received a notification in

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    NSDictionary *userInfo = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
    if ( userInfo != nil )
        [self application:application didReceiveRemoteNotification:userInfo];
}

I don't know how you are handling your news but you can post a notification to your main view controller informing that you have receive a notification, passing the "id" or the info of your new and then create your second view controller (you will need to verify if your MainViewController is already created in the stack of your NavigationController):

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

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

}

Upvotes: 2

Related Questions