Reputation: 3818
I am making a timer app. The user sets the time and the app counts down from there. I have added a UILocalNotification, so that it pops up even when you aren't in the app to tell you the timer has finished:
IN MY VIEW CONTROLLER:
- (void)setupLocalNotifications {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
totalSeconds = (setHour * 60 * 60) + (setMinute * 60) + (setSecond);
NSDate *now = [NSDate date];
NSDate *dateToFire = [now dateByAddingTimeInterval:totalSeconds];
localNotification.fireDate = dateToFire;
localNotification.alertBody = @"Timer Done";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1; // increment
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Object 1", @"Key 1", @"Object 2", @"Key 2", nil];
localNotification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
IN MY APPDELEGATE:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.;
ScrollViewController *sv = [[ScrollViewController alloc] init];
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
[self showAlarm:notification.alertBody];
NSLog(@"AppDelegate didFinishLaunchingWithOptions");
application.applicationIconBadgeNumber = 0;
}
self.window.rootViewController = sv; // Make tab bar controller the root view controller
[self.window makeKeyAndVisible];
return YES;
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[self showAlarm:notification.alertBody];
application.applicationIconBadgeNumber = 0;
NSLog(@"AppDelegate didReceiveLocalNotification %@", notification.userInfo);
}
- (void)showAlarm:(NSString *)text {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Timer"
message:text
delegate:self
cancelButtonTitle:@"Stop Timer"
otherButtonTitles:nil];
[alertView show];
}
What happens is, I set a UILocalNotification to go off after the user-defined number of seconds has passed. However, my app allows you to pause the timer. When paused, the UILocalNotification will carry on, and go off after the seconds have passed. Is there any way to pause the local notification?
Upvotes: 0
Views: 442
Reputation: 539745
A local notification cannot be paused. If the timer is paused in your application, you should cancel the notification, and create/schedule a new one if the timer is resumed.
Upvotes: 5