简体   繁体   English

调用UILocalNotification时发出警报

[英]fire an alert when a UILocalNotification is called

I have a UILocalNotification and I want to fire an alert when the app recovers from background to foreground. 我有一个UILocalNotification,当应用程序从后台恢复到前台时,我想发出警报。 I can create the alert in didReceiveLocalNotification, but this method can only be called when the app is active. 我可以在didReceiveLocalNotification中创建警报,但是只能在应用程序处于活动状态时调用此方法。 Now I want to check if a notification was fired while the app is in background and then fire an alert when the app recovers. 现在,我想检查在后台运行时是否触发了通知,然后在恢复时触发警报。

this is my current method 这是我目前的方法

   - (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];
  }
}

the NSUserDefaults is set when I initialize the notification, however, this alert is called even though the notification did not arrive yet. 我初始化通知时设置了NSUserDefaults,但是,即使通知尚未到达,也会调用此警报。 So, Im assuming maybe I can do something like: 因此,我假设我可以做些类似的事情:

   if([prefs stringForKey:@"kTimerNotificationUserDef"] != nil && didFireNotifFromBackground)

any suggestions? 有什么建议么? thanks! 谢谢!

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

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

show the alertview where class u want to show 在您要显示的类上显示alertview

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

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

It is in your settings. 它在您的设置中。 Settings ->Notification->Your application in right side-> Choose your alert style. 设置->通知->您的应用程序在右侧->选择您的警报样式。

Then you get alert directly while firing local notification 然后您会在触发本地通知时直接收到警报

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM