Determine if the user opens from the app

If you ever download the Telegram app or other messaging app, you will see that when you are in the app (app status is active) when someone you inform it, it will show you a custom notification inside the app at the top of the screen ... When you touch this custom notification, it will redirect you to the chat screen.

But when the app is inactive (the app is in the background) and you get a chat notification outside of the app, perhaps on the lock screen or another app. If you touch it, it will open the app and redirect you to the chat screen without any special notification in the app .

In order to do this, I think I need to know how to determine whether an app is open from a notification or not. The question is, how to determine if an app was opened from a notification or is currently active?

+3


source to share


1 answer


Objective-C:

-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    if ([UIApplication sharedApplication].applicationState==UIApplicationStateActive) {
       NSLog(@"App already open");
    } else {
       NSLog(@"App opened from Notification");
    }
}

      



Swift:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    if application.applicationState == UIApplicationState.Active {
        println("App already open")
    } else {
        println("App opened from Notification")
    }
}

      

+8


source







All Articles