我正在以objective-c编写一个每小时计时器应用程序。该应用程序将安排本地通知,可选择在四分之一,一小时和一小时开火。代码正确计算了被激活的内容,并在适当的时间进入本地通知。无论应用是在前台还是在后台,重新安排下一个通知的代码也是有效的,但不是在手机被锁定时。我还没有找到一种方法来重新安排下一个间隔(季度小时,半小时或小时)的通知,而不会收到本地通知。这意味着用户必须接收警报,然后点击警报以使app delegate中的 didReceiveLocalNotification 方法运行。因此,问题的关键是在手机锁定时获取本地通知以唤醒应用代表。如果我在那里设置断点,则不会调用 launchOption objectForKey 方法。这可能吗?注意:这不是使用重复通知的问题。这是app委托代码:
//
// AppDelegate.m
// Hourly Chime2
//
// Created by Nelson Capes on 9/20/16.
// Copyright © 2016 Nelson Capes. All rights reserved.
//
#import "AppDelegate.h"
#import <AVFoundation/AVFoundation.h>
#import "HourlyChimeTableViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSError *sessionError = nil;
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&sessionError];
[[AVAudioSession sharedInstance] setActive: YES error: &activationError];
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings
settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|
UIUserNotificationTypeSound categories:nil]];
}
UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification) {
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
HourlyChimeTableViewController *controller = [[HourlyChimeTableViewController alloc]init];
[controller stopTimer];
[controller startTimer];
NSUserDefaults *storage = [NSUserDefaults standardUserDefaults];
NSInteger interval = [storage integerForKey:@"intervalToWatch"];
[controller setExpirationTime:interval];
}
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reminder"
message:notification.alertBody
delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
HourlyChimeTableViewController *controller = [[HourlyChimeTableViewController alloc]init];
[controller stopTimer];
[controller startTimer];
NSUserDefaults *storage = [NSUserDefaults standardUserDefaults];
NSInteger interval = [storage integerForKey:@"intervalToWatch"];
[controller setExpirationTime:interval];
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
HourlyChimeTableViewController *controller = [[HourlyChimeTableViewController alloc]init];
[controller stopTimer];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
- (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.
HourlyChimeTableViewController *controller = [[HourlyChimeTableViewController alloc]init];
[controller startTimer];
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
以下是收到本地通知时调用的代码(注释间隔设置为14分钟仅用于测试 - 一分钟后收到本地通知:
-(void)setExpirationTime:(NSInteger)interval{
NSDate *today = [[NSDate alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components= [calendar components:(NSCalendarUnitMinute) | (NSCalendarUnitSecond) fromDate: today];
NSInteger minute = [components minute];
NSInteger second = [components second];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
switch(interval){
case 0:{
NSInteger difference;
minute =14;
// NSInteger difference = 10;
[offsetComponents setHour:0];
[offsetComponents setMinute:0];
if(minute >= 0 && minute < 15){
difference = 900 - minute*60 - second;//seconds left to quarter past
}else{
if(minute >= 15 && minute < 30){
difference = 1800 - minute*60 - second;// seconds to half past
}else{
if (minute >= 30 && minute < 45){// seconds to quarter to
difference = 2700 - minute*60 - second;
}else{// seconds to the hour
difference = 3600 - minute*60 - second;
}
}
}
// difference = 60;
[offsetComponents setSecond:difference];
NSDate *fireDate = [calendar dateByAddingComponents:offsetComponents
toDate:today options:0];
[self startLocalNotification:fireDate];
break;
}
case 1:{
NSInteger difference;
// NSInteger difference = 60;
[offsetComponents setHour:0];
[offsetComponents setMinute:0];
if(minute >= 0 && minute < 30){
difference = 1800 - minute*60 - second;// seconds to half past
}else{
difference = 3600 - minute*60 - second;// seconds to the hour
}
[offsetComponents setSecond:difference];
NSDate *fireDate = [calendar dateByAddingComponents:offsetComponents
toDate:today options:0];
[self startLocalNotification:fireDate];
break;
}
case 2:{
NSInteger difference = 3600 - minute*60 - second;
// NSInteger difference = 60;
[offsetComponents setHour:0];
[offsetComponents setMinute:0];
[offsetComponents setSecond:difference];
NSDate *fireDate = [calendar dateByAddingComponents:offsetComponents
toDate:today options:0];
[self startLocalNotification:fireDate];
break;
}
}
}
-(void)startLocalNotification:(NSDate *)fireDate{
// [[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.fireDate = fireDate;
notification.alertBody =@"Timer Expired!";
notification.alertTitle = @"TimeChime Alert";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
// notification.repeatInterval = NSCalendarUnitMinute;
[[UIApplication sharedApplication]scheduleLocalNotification:notification];
}