0

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))
    {

}
  • 2
    possible duplicate of [didReceiveRemoteNotification: fetchCompletionHandler: open from icon vs push notification](http://stackoverflow.com/questions/22085234/didreceiveremotenotification-fetchcompletionhandler-open-from-icon-vs-push-not) – maty_nz Apr 16 '15 at 19:25

1 Answers1

1

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 !!

Viral Savaj
  • 3,379
  • 1
  • 26
  • 39
  • never hits second function because i use content avaibility = 1 it is working on - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^) (UIBackgroundFetchResult))completionHandler { –  Apr 16 '15 at 16:12
  • Not getting you, can u elaborate it further. – Viral Savaj Apr 16 '15 at 16:20
  • i dont use didreceiveremotenotification i use fetchCompletionHandler:(void (^) (UIBackgroundFetchResult))completionHandler { –  Apr 16 '15 at 16:30