4

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?

Edward Anthony
  • 3,354
  • 3
  • 25
  • 40
  • 1
    See if this thread helps, it may be already answered: http://stackoverflow.com/questions/16393673/detect-if-the-app-was-launched-opened-from-a-push-notification – Gurtej Singh Aug 10 '15 at 06:05

1 Answers1

9

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")
    }
}
Krunal Nagvadia
  • 1,083
  • 2
  • 12
  • 33
Roland Keesom
  • 8,180
  • 5
  • 45
  • 52