2012-03-26 102 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。設置 - >通知 - >您的應用程序在右側 - >選擇您的警報風格。

然後你得到警報,而直接射擊本地通知時,應用程序在後臺