我有一个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)
有什么建议吗?谢谢!
didReceiveLocalNotification不能叫。 – Diffy 2012-03-26 09:41:43
从这里引用http://useyourloaf.com/blog/2010/7/31/adding-local-notifications-with-ios-4.html – akk 2012-03-26 09:56:36
我试过这个示例项目,但我没有在应用程序时收到警报从背景恢复到前景。 – Diffy 2012-03-26 10:28:56