2012-03-26 82 views
0

我有一个UILocalNotification,并且我想在应用程序从背景恢复到前景时触发警报。我可以在didReceiveLocalNotification中创建警报,但只能在应用程序处于活动状态时调用此方法。现在我想检查应用程序在后台时是否触发了通知,然后在应用程序恢复时触发警报。当调用UILocalNotification时触发警报

这是我目前的方法

- (void)applicationDidBecomeActive:(UIApplication *)application 
    { 

// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 

application.applicationIconBadgeNumber = 0; 

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 

NSLog(@"prefs %@",[prefs stringForKey:@"kTimerNotificationUserDef"]); 

if([prefs stringForKey:@"kTimerNotificationUserDef"] != nil) 
{ 
    [prefs setObject:nil forKey:@"kTimerNotificationUserDef"]; 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Timer Alert" message:[prefs stringForKey:@"kTimerNotificationUserDef"] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 

    [alert show]; 
    } 
} 

的是NSUserDefaults的时候我初始化设置的通知,但是,这个警报被称作即使该通知并没有到达。所以,我假设我可以做类似的事情:

if([prefs stringForKey:@"kTimerNotificationUserDef"] != nil && didFireNotifFromBackground) 

有什么建议吗?谢谢!

回答

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

notification.applicationIconBadgeNumber = 0; 
NSString *reminderText = [notification.userInfo objectForKey:kRemindMeNotificationDataKey]; 
[viewController showReminder:reminderText]; 
} 

显示,其中U类要显示

- (void)showReminder:(NSString *)text 
{ 

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Reminder" message:text delegate:nil cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
[alertView show]; 
[alertView release]; 
} 
+2

didReceiveLocalNotification不能叫。 – Diffy 2012-03-26 09:41:43

+0

从这里引用http://useyourloaf.com/blog/2010/7/31/adding-local-notifications-with-ios-4.html – akk 2012-03-26 09:56:36

+0

我试过这个示例项目,但我没有在应用程序时收到警报从背景恢复到前景。 – Diffy 2012-03-26 10:28:56

0

这是您的设置alertview。设置 - >通知 - >您的应用程序在右侧 - >选择您的警报风格。

然后你得到警报,而直接射击本地通知时,应用程序在后台