Iphone 确定应用程序在前台还是后台触发UILocalNotification

Iphone 确定应用程序在前台还是后台触发UILocalNotification,iphone,objective-c,ios4,uikit,uilocalnotification,Iphone,Objective C,Ios4,Uikit,Uilocalnotification,当收到UILocalNotification 当应用程序位于前台时,调用方法application:DidReceiveLocalNotification 当应用程序位于后台时,它会打开应用程序,然后调用application:DidReceiveLocalNotification 我如何确定发生了哪种情况?我想以不同的方式对待他们 如果应用程序在后台,则用户表示他们希望查看事件。所以在application:DidReceiveLocalNotification中,我不想再问他们,我只想带他们

当收到
UILocalNotification

  • 当应用程序位于前台时,调用方法
    application:DidReceiveLocalNotification
  • 当应用程序位于后台时,它会打开应用程序,然后调用
    application:DidReceiveLocalNotification
    我如何确定发生了哪种情况?我想以不同的方式对待他们

    如果应用程序在后台,则用户表示他们希望查看事件。所以在
    application:DidReceiveLocalNotification
    中,我不想再问他们,我只想带他们去参加活动

    但是,如果当通知触发并调用
    application:DidReceiveLocalNotification
    时,应用程序位于前台,我想询问用户是否希望查看事件

    基本上,我会:

    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    
    if (appLauncedFromBackground)
      // go to event
    else
      // ask if user wants to view event
    
    }
    
    我曾想过在
    应用程序willenterforeground:application
    中向NSUserDefault写入bool值,然后在
    应用程序:DidReceiveLocalNotification
    中读取并重置它。但我想,如果有一种测试方法,那将是一种更好的方法

    谢谢,马克

    编辑

    谢谢徐哲,但在这种情况下这是行不通的。这里有更多的细节,可以解释为什么它不起作用,也许可以帮助别人帮我回答这个问题:

    我在这里设置的
    UILocalNotification
    是一个类似日历事件的事件,计划在用户选择的时间触发,因此在计划通知时,我不知道用户在触发通知时将要做什么。我正在使用通知的userInfo来存储关于计划的事件的数据,但是设置像inBackground这样的值作为建议是行不通的

    我知道如果用户决定要“查看”事件,这将是两种情况之一。(这一切都将假定应用程序位于后台或前台,并且不会退出)

    1-如果在应用未被使用时触发&&应用在后台且未退出,iOS将通知用户发生事件,并提供转到负责应用的选项(在本例中是我的)。此时,假设用户说“yes take me to this cool app”,它将从后台打开应用程序并调用应用程序:DidReceiveLocalNotification,在那里我可以获取所有通知userInfo

    2-当计划的事件发生时,用户碰巧在我的应用程序中。此时,再次调用application:DidReceiveLocalNotification方法,然后我可以从中获取所有通知userInfo

    但就在这一点上,我想知道这两个场景中的哪一个刚刚发生了1或2

    希望这篇关于我使用
    UILocalNotification
    的更多细节能够帮助回答这个问题。提前谢谢-马克


    结束编辑************************

    最简单的方法可能是当你的应用程序在前台时,不要发送本地通知,只带你的用户去参加活动

    但是,如果您坚持使用LocalNotification,那么这里有一个简单的方法可以检测当UILocalNotification启动时应用程序的状态

    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
        //[super application:application didReceiveLocalNotification:notification]; // In most case, you don't need this line
        UIApplicationState state = [application applicationState];
        if (state == UIApplicationStateInactive) {
            // Application was in the background when notification was delivered.
        } else {
    
        }
    }
    

    这就是我添加到这些函数体中的内容。。。
    应用程序使用选项完成启动

    NSUserDefaults* userDefaults= [NSUserDefaults standardUserDefaults];
    UILocalNotification *localNotif =
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotif) {  //launched from notification
        [userDefaults setBool:YES forKey:@"deactivate in-app notification"];
        [userDefaults synchronize];
    }
    else{
        [userDefaults setBool:NO forKey:@"deactivate in-app notification"];
        [userDefaults synchronize];
    }
    
    应用程序标识符背景中

    NSUserDefaults* userDefaults= [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:YES forKey:@"deactivate in-app notification"];
    [userDefaults synchronize];
    
    NSUserDefaults* userDefaults= [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:YES forKey:@"deactivate in-app notification"];
    [userDefaults synchronize];
    
    应用程序中,将进入前台

    NSUserDefaults* userDefaults= [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:YES forKey:@"deactivate in-app notification"];
    [userDefaults synchronize];
    
    NSUserDefaults* userDefaults= [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:YES forKey:@"deactivate in-app notification"];
    [userDefaults synchronize];
    
    ApplicationIDBecomeActive

    NSUserDefaults* userDefaults= [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:NO forKey:@"deactivate in-app notification"];
    [userDefaults synchronize];
    
    direceivelocalnotification

    NSUserDefaults* userDefaults= [NSUserDefaults standardUserDefaults];
    if(![userDefaults boolForKey:@"deactivate in-app notification"]){
        UIAlertView* alert= [[UIAlertView alloc]initWithTitle:@"My App" message:notif.alertBody delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    
        NSUserDefaults* userDefaults= [NSUserDefaults standardUserDefaults];
        [userDefaults setBool:NO forKey:@"deactivate in-app notification"];
        [userDefaults synchronize];
    }
    

    现在这两个通知都消失了(特别是当应用程序处于后台并且您从通知警报打开它时出现的通知。

    didReceiveLocalNotification
    中,您可以检查应用程序状态:

    [UIApplication shareApplication].applicationState

    如果它等于
    UIApplicationStateInactive
    ,则您知道该应用程序位于后台,并且用户使用本地通知警报打开了它


    如果它是
    UIApplicationStateActive
    您可能希望向用户显示您自己的警报。

    我编辑了这个问题以帮助更好地解释。谢谢@xuzhe,但这对我不起作用。我没有设置LocalNotification,它是一个UILocalNotification,已计划在某个日期发出。因此我无法确定是什么原因用户将在那时做。再次编辑答案。您可以使用UIApplicationState属性轻松检查应用程序的状态。非常非常好。修复了我一段时间以来遇到的一个问题。例如,区分从后台返回和当前由于来电而挂起如何?我们必须求助于吗要在ApplicationIdentinterBackground/applicationWillEnterForeground中设置标志,是否有更好的方法?是否确实需要调用
    super