简体   繁体   中英

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. I can create the alert in didReceiveLocalNotification, but this method can only be called when the app is active. 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. 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

- (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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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