Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if the user open app from notification

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

But when the app is inactive (the app is in background), and you get the chat notification outside the app, probably on the lock screen or another app. If you touch it, it will open the app and redirect you to the chat screen without in-app custom notification.

To do this I think I should know how to determine is the app opened from the notification or not. Question, how to determine if the app opened from notification or currently active?

like image 783
Edward Anthony Avatar asked Jan 08 '23 19:01

Edward Anthony


1 Answers

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 {
        print("App already open")
    } else {
        print("App opened from Notification")
    }
}
like image 74
Roland Keesom Avatar answered Jan 10 '23 09:01

Roland Keesom