1

I am creating an app which should open specific view controller when I tap on UILocalNotification. My code is :

func application(application:UIApplication!, didReceiveLocalNotification notification: UILocalNotification)
{
  var root = self.window!.rootViewController as ViewController
  let main: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
  var setview = main.instantiateViewControllerWithIdentifier("destination") as tapViewController

  if application.applicationState == UIApplicationState.Inactive
  {
     root.presentViewController(setview, animated:false , completion: nil)
  }
}

TapViewController is displayed even if I don't tap on notification when I pull down the notification centre from my app,because the application state is changing from inactive to active when I do so.

So how can I detect whether the notification is tapped in this case? What is the condition to check along with application state?

davegson
  • 8,205
  • 4
  • 51
  • 71
  • possible duplicate of [What method is triggered when local notification is dismissed from notification centre of iOS?](http://stackoverflow.com/questions/29096282/what-method-is-triggered-when-local-notification-is-dismissed-from-notification) – Rajesh Loganathan Mar 20 '15 at 03:48

2 Answers2

2

Try this code to check active / inactive state when receive notification.

func application(application: UIApplication, didReceiveLocalNotification userInfo: [NSObject : AnyObject]) {

    if application.applicationState == UIApplicationState.Inactive || application.applicationState == UIApplicationState.Background {
        //opened from a push notification when the app was on background

    }

}
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
  • Tried the changes u mentioned in the code but same output as I mentioned in the question. The problem occurs when app is transitioning from active to inactive state. :( It is working fine for when I tap on the notification when the app is in background @SVMRAJESH – Vedapragnareddy Aramati Mar 20 '15 at 02:38
0

Try this code (written in Objective C, you can switch to Swift easily)

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

    NSAssert(notification.fireDate != nil, @"Please use scheduleLocalNotification instead of presentLocalNotificationNow with fireDate.");

    // 0.2s is threshold time, you can adjust it
    if (application.applicationState != UIApplicationStateActive && [notification.fireDate timeIntervalSinceNow] < -0.2) {
        // user tapped the notification
        ...
    }
}

I can't understand why don't Apple provide the method to check the notification is tapped.

jeilsoft
  • 572
  • 5
  • 13