【问题标题】:Determining if UILocalNotification fired with app in foreground or background确定 UILocalNotification 是在前台还是后台使用应用程序触发的
【发布时间】:2011-11-02 09:41:09
【问题描述】:

收到UILocalNotification时

  1. 当应用在前台时调用application:DidReceiveLocalNotification方法。
  2. 当应用程序处于后台时,它会打开应用程序,然后调用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

}

我曾想过将布尔值写入applicationWillEnterForeground:application 内的 NSUserDefault,然后在 application:DidReceiveLocalNotification 内读取并重置它。但我想如果有一种方法可以测试,那将是一条更好的路线。

谢谢,马克

编辑 ************ *************** ***

感谢 xuzhe,但在这种情况下这行不通。这里有一些更详细的信息,可以解释为什么它不起作用,也许可以帮助某人为我回答这个问题:

我在这里设置的UILocalNotification 是一个类似于日历事件的事件,它计划在用户选择的时间触发,所以在安排通知时,我不知道用户在收到通知时会做什么火灾。我正在使用通知的 userInfo 来存储有关已安排事件的数据,但按照建议设置像 inBackground 这样的值是行不通的。

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

1 - 如果它在应用程序未使用时触发 && 应用程序在后台并且没有退出,iOS 将通知用户发生了事件并提供前往负责应用程序的选项(这是我的在这种情况下)。此时假设用户说“是的,带我去这个很酷的应用程序”,它将从后台打开应用程序并调用应用程序:DidReceiveLocalNotification,我可以在其中获取所有通知 userInfo。

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

但正是在这一点上,我想知道这两种情况中的哪一种刚刚发生 1 或 2。

希望有关我使用UILocalNotification 的更多详细信息将有助于回答这个问题。在此先感谢 - 标记

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

【问题讨论】:

    标签: iphone objective-c ios4 uikit uilocalnotification


    【解决方案1】:

    也许最简单的方法是当您的应用程序处于前台时,不要发送 LocalNotification 而只是将您的用户带到事件中。

    但如果你坚持使用 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 {
    
        }
    }
    

    【讨论】:

    • 我已编辑问题以帮助更好地解释。谢谢@xuzhe,但这对我不起作用。我没有启动 LocalNotification,它是一个 UILocalNotification,计划在某个日期启动。因此,我无法确定用户当时会做什么。
    • 再次编辑了答案。您可以使用 UIApplicationState 属性轻松检查应用的状态。
    • 非常非常好。修复了我很长一段时间以来遇到的一个问题。
    • 如何区分从后台返回和当前由于来电而暂停?我们必须求助于在 applicationDidEnterBackground/applicationWillEnterForeground 中设置标志还是有更好的方法?
    • 真的有必要拨打super吗?
    【解决方案2】:

    这是我添加到这些函数的主体... application didFinishLaunchingWithOptions:

    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];
    }
    

    在applicationDidEnterBackground:

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

    在applicationWillEnterForeground:

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

    applicationDidBecomeActive:

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

    didReceiveLocalNotification:

    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];
    }
    

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

    【讨论】:

      【解决方案3】:

      在didReceiveLocalNotification可以查看应用状态:

      [UIApplication shareApplication].applicationState

      如果等于UIApplicationStateInactive,您就知道该应用在后台,并且用户使用本地通知警报打开它。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-24
        • 2016-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多