I want to detect if user click on push notification to launch the app.or to get it in foreground.
            Asked
            
        
        
            Active
            
        
            Viewed 1,258 times
        
    2 Answers
0
            
            
        Just implement in your AppDelegate the method
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
 
    
    
        ObjectAlchemist
        
- 1,109
- 1
- 9
- 18
0
            
            
        If the application was not running the didFinishLaunchingWithOptions method gets called when the application starts and you can check the launchOptions parameters like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if (launchOptions != nil) {
         NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
         if (notification) {
             // Launched from push notification
         }
    }
}
If the application is already launched you can use this method:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground  )
    {
         //opened from a push notification when the app was on background
    }
}
You can also check: Detect if the app was launched/opened from a push notification
- 
                    What if application is already launched I need to handle both condition. – Aashish Nagar Jul 24 '16 at 10:24
- 
                    if app is coming from background to foreground , at the same time if app receive push notification , that time I would get the state as InActive. ( How to solve this ? – Aashish Nagar Jul 24 '16 at 10:29
 
     
     
    