我正在制作一个计时器应用程序。用户设置时间,应用程序从那里开始计数。我添加了一个UILocalNotification,这样即使你不在应用程序中告诉你计时器已经完成,它也会弹出:
在我看来,控制器:
- (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];
}在我的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];
}所发生的情况是,在用户定义的秒数过去之后,我设置了一个UILocalNotification。然而,我的应用程序允许您暂停计时器。当暂停时,UILocalNotification将继续进行,并在秒过后离开。有没有办法暂停本地通知?
发布于 2013-12-08 16:53:26
不能暂停本地通知。如果应用程序中暂停了计时器,则应取消通知,并在恢复计时器时创建/调度一个新的通知。
https://stackoverflow.com/questions/20455919
复制相似问题