【发布时间】:2012-04-09 19:15:04
【问题描述】:
我有一个 UILocalNotification,我想在应用从后台恢复到前台时发出警报。我可以在 didReceiveLocalNotification 中创建警报,但是这个方法只能在应用程序处于活动状态时调用。现在我想检查应用在后台时是否触发了通知,然后在应用恢复时触发警报。
这是我目前的方法
- (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)
有什么建议吗?谢谢!
【问题讨论】:
标签: iphone objective-c ios xcode uilocalnotification