【问题标题】:fire an alert when a UILocalNotification is called调用 UILocalNotification 时触发警报
【发布时间】: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


    【解决方案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];
    }
    

    【讨论】:

    • didReceiveLocalNotification 应用在后台时无法调用。
    • 我尝试了那个示例项目,但是当应用从后台恢复到前台时我没有收到警报。
    【解决方案2】:

    它在您的设置中。设置->通知->右侧的应用程序->选择您的警报样式。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多