user3236289
user3236289

Reputation:

detect app opened from push notification (background fetch)

I use UIBackgroundFetchResult to catch push notifications as below code...i use content availability = 1 also to detect in background mode

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)   (UIBackgroundFetchResult))completionHandler {

Push is coming and is executed on here while app is active or background mode always BUT when i opened push,i cannot detect whether app is opened from push or not because it always enters if state

 if ( (application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground)  && (NotiType == 1 || NotiType == 2))
    {

}

Upvotes: 0

Views: 1082

Answers (1)

Viral Savaj
Viral Savaj

Reputation: 3389

You can check whether your app is launched from APNS (Apple Push notification service), from app delegate method

application:didFinishLaunchingWithOptions:

In which you can check whether app launched from notification or not like this,

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if (launchOptions != nil) {
          // Launched from push notification
          NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    }
}

If your app is in background, this method will not invoke. for that you can check in another method with this way,

- (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
    }
}

HTH, Enjoy Coding !!

Upvotes: 1

Related Questions