2011-08-24 61 views
29

与应用烧制当接收到一个UILocalNotification确定是否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 - 当预定的事件发生时,偶然的用户恰好在我的应用程序中。此时应用程序:DidReceiveLocalNotification方法再次被调用,然后我可以获取所有通知userInfo。

但是,在这一点上,我想知道这两种情况只是发生在1或2

希望我的使用UILocalNotification这个额外的细节将在回答这个问题有所帮助。在此先感谢 - 马克

编辑完* ** * ** * ** * ** * ** * ** * ** * ** * ***

回答

46

也许最简单的方法是当你的应用程序在前台,不要发送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 { 

    } 
} 
+2

我编辑了这个问题,以帮助更好地解释。谢谢@xuzhe,但这对我不起作用。我没有启动LocalNotification,它是一个UILocalNotification,它已经计划在某个日期关闭。因此,我无法确定用户当时会做什么。 –

+0

再次编辑答案。您可以使用UIApplicationState属性轻松检查您的应用程序的状态。 – xuzhe

+0

非常非常好。修复了我一段时间以来的一个问题。 –

1

这是我加入这些功能的机构... 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]; 
} 

现在这两个通知都没有了(特别是当一个应用程序在后台,你从通知警报打开它出现。

1

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

[UIApplication shareApplication].applicationState

如果它等于你知道应用程序在后台和用户与本地通知警报打开它UIApplicationStateInactive

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