我有一個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