我试图使用设置包来安排UIlocalnotification.在设置中,您可以选择是否要每天通知(每天1天),每2天1次,仅限星期日或从不.
这是我使用的代码(这一切都在AppDelegate.m中):
-(void)defaultsChanged:(NSNotification *)notification { [[UIApplication sharedApplication] cancelAlllocalnotifications]; [[NSUserDefaults standardUserDefaults]synchronize]; Nsstring *testValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"multi_preference"]; NSLog(@"%@",testValue); NSDate *today = [NSDate date]; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComponents* compoNents = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:today]; // Get necessary date components [compoNents month];[compoNents day]; NSDictionary *dictToday= [self getDataFromdate : [compoNents day] month:[compoNents month]]; if ([testValue isEqualToString:@"one"]){ UIlocalnotification* localnotification = [[UIlocalnotification alloc] init]; localnotification.fireDate = [[NSDate date]dateByAddingTimeInterval:20]; localnotification.alertAction = @"View"; localnotification.alertBody = [dictToday objectForKey:@"saint_description"]; localnotification.repeatInterval = NSDayCalendarUnit; localnotification.timeZone = [NSTimeZone defaultTimeZone]; localnotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; [[UIApplication sharedApplication] schedulelocalnotification:localnotification]; [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self]; } if (testValue==Nil){ UIlocalnotification* localnotification = [[UIlocalnotification alloc] init]; localnotification.fireDate = [[NSDate date]dateByAddingTimeInterval:20]; localnotification.alertAction = @"View"; localnotification.alertBody = [dictToday objectForKey:@"saint_description"]; localnotification.repeatInterval = NSDayCalendarUnit; localnotification.timeZone = [NSTimeZone defaultTimeZone]; localnotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; [[UIApplication sharedApplication] schedulelocalnotification:localnotification]; [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self]; } if ([testValue isEqualToString:@"two"]){ UIlocalnotification* localnotification = [[UIlocalnotification alloc] init]; localnotification.fireDate = [[NSDate date]dateByAddingTimeInterval:86401]; localnotification.alertAction = @"View"; localnotification.alertBody = [dictToday objectForKey:@"saint_description"]; localnotification.repeatInterval = NSDayCalendarUnit; localnotification.timeZone = [NSTimeZone defaultTimeZone]; localnotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; [[UIApplication sharedApplication] schedulelocalnotification:localnotification]; [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self]; } if ([testValue isEqualToString:@"three"]){ NSDate *today2 = [[NSDate alloc] init]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // Get the weekday component of the current date NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:today2]; /* Create a date components to represent the number of days to subtract from the current date. The weekday value for Sunday in the Gregorian calendar is 1,so subtract 1 from the number of days to subtract from the date in question. (If today is Sunday,subtract 0 days.) */ NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init]; [componentsToSubtract setDay: 0 - ([weekdayComponents weekday] - 1)]; NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract toDate:today2 options:0]; /* Optional step: beginningOfWeek Now has the same hour,minute,and second as the original date (today). To normalize to midnight,extract the year,month,and day components and create a new date from those components. */ NSDateComponents *components = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate: beginningOfWeek]; beginningOfWeek = [gregorian dateFromComponents:components]; UIlocalnotification* localnotification = [[UIlocalnotification alloc] init]; localnotification.fireDate = beginningOfWeek; localnotification.alertAction = @"View"; localnotification.alertBody = [dictToday objectForKey:@"saint_description"]; localnotification.repeatInterval = NSWeekdayCalendarUnit; localnotification.timeZone = [NSTimeZone defaultTimeZone]; localnotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; [[UIApplication sharedApplication] schedulelocalnotification:localnotification]; [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self]; } } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(defaultsChanged:) name:NSUserDefaultsDidChangeNotification object:nil]; UIlocalnotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionslocalnotificationKey]; if (locationNotification) { // Set icon badge number to zero application.applicationIconBadgeNumber = 0; } return yes; } - (void)application:(UIApplication *)application didReceivelocalnotification:(UIlocalnotification *)notification { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Today's Saint" message:notification.alertBody delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; if (notification.alertBody!=Nil) [alert show]; [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self]; // Set icon badge number to zero application.applicationIconBadgeNumber = 0; }
解决方法
您似乎在用户更改首选项时调度通知.但是,您永远不会取消调度以前安排的通知,这就是为什么您会在一段时间内观察突发的通知,这些通知的时间将完全对应于您每天或两天前反复更改设置的时间.
您计划的通知是与您要修改的通知集不同的对象.不幸的是,UIlocalnotifications没有标识符令牌.
但是,您可以在收到带有[[UIApplication sharedApplication] cancelAlllocalnotifications]的defaultsChanged:消息时取消调度所有以前的通知;在你重新安排之前.这将解决您的问题.
还要仔细查看this solution,建议即使在启动时也可以取消并重新安排通知,以避免在用户重新安装应用程序时发生突发或重复通知.
原文地址:https://www.jb51.cc/iOS/335916.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。