2012-03-26 8 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

+0

ここから参照してくださいhttp://useyourloaf.com/blog/2010/7/31/adding-local-notifications-with-ios-4.html – akk

+0

私はそのサンプルプロジェクトを試しましたが、背景から前景へと回復する。 – Diffy

0

を表示したいalertviewを示しました。設定 - >通知 - >右側のアプリケーション - >アラートスタイルを選択します。アプリがバックグラウンドで動作しているとき

関連する問題