首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >暂停UILocalNotification

暂停UILocalNotification
EN

Stack Overflow用户
提问于 2013-12-08 16:42:29
回答 1查看 433关注 0票数 0

我正在制作一个计时器应用程序。用户设置时间,应用程序从那里开始计数。我添加了一个UILocalNotification,这样即使你不在应用程序中告诉你计时器已经完成,它也会弹出:

在我看来,控制器:

代码语言:javascript
复制
- (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中:

代码语言:javascript
复制
- (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将继续进行,并在秒过后离开。有没有办法暂停本地通知?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-08 16:53:26

不能暂停本地通知。如果应用程序中暂停了计时器,则应取消通知,并在恢复计时器时创建/调度一个新的通知。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20455919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档