暂停UILocalNotification

use*_*282 0 objective-c ios uilocalnotification

我正在制作计时器应用程序.用户设置时间,应用程序从那里倒计时.我添加了一个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];
}
Run Code Online (Sandbox Code Playgroud)

在我的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];
}
Run Code Online (Sandbox Code Playgroud)

会发生什么,我设置UILocalNotification在用户定义的秒数过去后关闭.但是,我的应用程序允许您暂停计时器.暂停时,UILocalNotification将继续运行,并在秒数过后熄灭.有没有办法暂停本地通知?

Mar*_*n R 5

无法暂停本地通知.如果计时器在您的应用程序中暂停,您应该取消通知,并在计时器恢复时创建/安排新通知.