如何在点击本地通知的过程中确定后台应用程序的前景

时间:2016-06-21 04:53:01

标签: ios objective-c appdelegate region-monitoring

Apple doc Getting the User’s Attention While in the Background

  

通知是暂停的应用的一种方式,位于   背景,或没有运行以引起用户的注意。

由于区域监控和is in the background我的应用程序被iOS唤醒并发布本地通知。用户点击通知,应用程序将在前台。

如果由于用户点击通知,如何确定应用程序是否在前台?

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

didFinishLaunchingWithOption or didReceiveLocalNotification

3 个答案:

答案 0 :(得分:1)

如果您的应用在后台运行,并且您点击了LocalNotification Banner,那么您将按照以下方法调用:

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

答案 1 :(得分:1)

您可以在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 {

    }
}

答案 2 :(得分:0)

您可以使用application.applicationState

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

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

    }