Dattatray Deokar
Dattatray Deokar

Reputation: 2103

Conditional navigation on tap of notification ios

I want to navigate specific screen when i will tap on notification, i tried to do this but not able to decide the approach. So any one ca guide me how to do this.

I tried by deciding to put json string for "alert" key but when app is closed that json is displaying as it is.

Depend on text in notification i have to decide the which screen needs to be displayed. also have read some private id. which should not be displayed in notification.

Upvotes: 0

Views: 114

Answers (1)

arturdev
arturdev

Reputation: 11039

Your server, who sends you push notifications, must include some field in the payload which you must read when a user opens the app and show a screen based on that field's value.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //...
    NSDictionary *pushUserInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (pushUserInfo) {
        [self handlePushNotificationWithUserInfo:pushUserInfo];
    }
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [self handlePushNotificationWithUserInfo:userInfo];
}

- (void)handlePushNotificationWithUserInfo:(NSDictionary *)userInfo
{
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
        return;
    }

    id someValue = userInfo[@"someField"]; 
    if (someValue == ...) {
       //Open screen
    } else if (someValue == ...) {
       //Open another screen
    } // and so on
}

Upvotes: 1

Related Questions