如何确定后台应用在点击本地通知时进入前台

How to determine background app come to foreground in action of tapping local notification

Apple 文档 Getting the User’s Attention While in the Background

Notifications are a way for an app that is suspended, is in the background, or is not running to get the user’s attention.

我的应用程序被 iOS 唤醒,因为区域监控以及 is in the background 和 post 本地通知。用户点击通知,应用程序将在前台。

如何确定应用程序因用户点击通知而进入前台?

哪个委托方法将包含通知信息。

didFinishLaunchingWithOption or didReceiveLocalNotification

如果您的应用程序 运行 在后台运行,并且您在 LocalNotification 横幅上被点击,那么您将通过以下方法调用:

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

在 iOS 8:

之后
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler

如果应用程序不在后台 运行,您将在以下位置收到通知:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if ([launchOptions valueForKey:@"UIApplicationLaunchOptionsLocalNotificationKey"]) {
 // here you will get
}

当 UILocalNotification 触发时,您可以检测到您的应用程序的状态是什么,如果 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 被调用,这确保收到本地通知。

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateInactive) {
        // Application was in the background when notification was delivered.
    } else {

    }
}

您可以使用 application.applicationState

来确定
 if(application.applicationState == UIApplicationStateInactive)
     {
           NSLog(@" Inactive");
      // when you tapping on notification

     }
    else if (application.applicationState == UIApplicationStateBackground)
    {
           NSLog(@" background");
    }
    else
    {
          NSLog(@" Active");

    }