调用UILocalNotification时触发警报

时间:2012-03-26 09:19:49

标签: iphone objective-c ios xcode uilocalnotification

我有一个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)

有什么建议吗?谢谢!

2 个答案:

答案 0 :(得分:1)

- (void)application:(UIApplication *)application 
    didReceiveLocalNotification:(UILocalNotification *)notification {

 notification.applicationIconBadgeNumber = 0;
 NSString *reminderText = [notification.userInfo objectForKey:kRemindMeNotificationDataKey];
 [viewController showReminder:reminderText];
 }

显示你想要显示的课程的警报视图

- (void)showReminder:(NSString *)text 
{

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Reminder" message:text delegate:nil  cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
[alertView show];
[alertView release];
}

答案 1 :(得分:0)

它在您的设置中。设置 - >通知 - >您的应用程序在右侧 - >选择您的提醒方式。

然后在发出本地通知时直接收到警报