Ios 计划本地通知问题-每日发射问题

Ios 计划本地通知问题-每日发射问题,ios,objective-c,xcode,uilocalnotification,Ios,Objective C,Xcode,Uilocalnotification,我在AppDelegate上为我的通用应用程序设置了多个本地通知,确切地说是14。这些通知具有特定的时间和工作日,它们应该在这些时间和工作日发出 问题是所有这些通知每天都会发出 例如,我有一个周六早上的通知,但它每天都会出现 请给我一个建议或指示,为什么我的通知每天都被解雇?我似乎找不到解决办法 请查看我的代码,谢谢 AppDelegate.m - (void)applicationDidEnterBackground:(UIApplication *)application { /* Use

我在AppDelegate上为我的通用应用程序设置了多个本地通知,确切地说是14。这些通知具有特定的时间和工作日,它们应该在这些时间和工作日发出

问题是所有这些通知每天都会发出

例如,我有一个周六早上的通知,但它每天都会出现

请给我一个建议或指示,为什么我的通知每天都被解雇?我似乎找不到解决办法

请查看我的代码,谢谢

AppDelegate.m

- (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, called instead of applicationWillTerminate: when the user quits.
 */


NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponent = [gregCalendar components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];


[dateComponent setWeekday:2];
[dateComponent setHour:11];
[dateComponent setMinute:44];



UILocalNotification *notification = [[UILocalNotification alloc]init];
[notification setAlertBody:@"GOOD MORNING!"];
[notification setFireDate:[gregCalendar dateFromComponents:dateComponent]];
notification.repeatInterval = NSWeekdayCalendarUnit;
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];


NSCalendar *gregCalendar1 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponent1 = [gregCalendar1 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];


[dateComponent1 setWeekday:3];
[dateComponent1 setHour:11];
[dateComponent1 setMinute:44];




UILocalNotification *notification1 = [[UILocalNotification alloc]init];
[notification1 setAlertBody:@"GOOD MORNING!"];
[notification1 setFireDate:[gregCalendar1 dateFromComponents:dateComponent1]];
notification1.repeatInterval = NSWeekdayCalendarUnit;
[notification1 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification1];



NSCalendar *gregCalendar2 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponent2 = [gregCalendar2 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];


[dateComponent2 setWeekday:4];
[dateComponent2 setHour:11];
[dateComponent2 setMinute:44];



UILocalNotification *notification2 = [[UILocalNotification alloc]init];
[notification2 setAlertBody:@"GOOD MORNING!"];
[notification2 setFireDate:[gregCalendar2 dateFromComponents:dateComponent2]];
notification2.repeatInterval = NSWeekdayCalendarUnit;
[notification2 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification2];

NSCalendar *gregCalendar3 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponent3 = [gregCalendar3 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];


[dateComponent3 setWeekday:5];
[dateComponent3 setHour:11];
[dateComponent3 setMinute:44];




UILocalNotification *notification3 = [[UILocalNotification alloc]init];
[notification3 setAlertBody:@"GOOD MORNING!"];
[notification3 setFireDate:[gregCalendar3 dateFromComponents:dateComponent3]];
notification3.repeatInterval = NSWeekdayCalendarUnit;
[notification3 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification3];

NSCalendar *gregCalendar4 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponent4 = [gregCalendar4 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];


[dateComponent4 setWeekday:6];
[dateComponent4 setHour:11];
[dateComponent4 setMinute:44];




UILocalNotification *notification4 = [[UILocalNotification alloc]init];
[notification4 setAlertBody:@"GOOD MORNING!"];
[notification4 setFireDate:[gregCalendar4 dateFromComponents:dateComponent4]];
notification4.repeatInterval = NSWeekdayCalendarUnit;
[notification4 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification4];





NSCalendar *gregCalendar5 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponent5 = [gregCalendar5 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];


[dateComponent5 setWeekday:2];
[dateComponent5 setHour:14];
[dateComponent5 setMinute:59];




UILocalNotification *notification5 = [[UILocalNotification alloc]init];
[notification5 setAlertBody:@"GOOD AFTERNOON!"];
[notification5 setFireDate:[gregCalendar5 dateFromComponents:dateComponent5]];
notification5.repeatInterval = NSWeekdayCalendarUnit;
[notification5 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification5];

NSCalendar *gregCalendar6 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponent6 = [gregCalendar6 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];


[dateComponent6 setWeekday:3];
[dateComponent6 setHour:14];
[dateComponent6 setMinute:59];



UILocalNotification *notification6 = [[UILocalNotification alloc]init];
[notification6 setAlertBody:@"GOOD AFTERNOON!"];
[notification6 setFireDate:[gregCalendar6 dateFromComponents:dateComponent6]];
notification6.repeatInterval = NSWeekdayCalendarUnit;
[notification6 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification6];


NSCalendar *gregCalendar7 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponent7 = [gregCalendar7 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];


[dateComponent7 setWeekday:4];
[dateComponent7 setHour:14];
[dateComponent7 setMinute:59];




UILocalNotification *notification7 = [[UILocalNotification alloc]init];
[notification7 setAlertBody:@"GOOD AFTERNOON!"];
[notification7 setFireDate:[gregCalendar7 dateFromComponents:dateComponent7]];
notification7.repeatInterval = NSWeekdayCalendarUnit;
[notification7 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification7];

NSCalendar *gregCalendar8 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponent8 = [gregCalendar8 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];


[dateComponent8 setWeekday:5];
[dateComponent8 setHour:14];
[dateComponent8 setMinute:59];




UILocalNotification *notification8 = [[UILocalNotification alloc]init];
[notification8 setAlertBody:@"GOOD AFTERNOON!"];
[notification8 setFireDate:[gregCalendar8 dateFromComponents:dateComponent8]];
notification8.repeatInterval = NSWeekdayCalendarUnit;
[notification8 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification8];

NSCalendar *gregCalendar9 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponent9 = [gregCalendar9 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];


[dateComponent9 setWeekday:6];
[dateComponent9 setHour:14];
[dateComponent9 setMinute:59];




UILocalNotification *notification9 = [[UILocalNotification alloc]init];
[notification9 setAlertBody:@"GOOD AFTERNOON!"];
[notification9 setFireDate:[gregCalendar9 dateFromComponents:dateComponent9]];
notification9.repeatInterval = NSWeekdayCalendarUnit;
[notification9 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification9];

NSCalendar *gregCalendar10 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponent10 = [gregCalendar10 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];


[dateComponent10 setWeekday:6];
[dateComponent10 setHour:19];
[dateComponent10 setMinute:59];



UILocalNotification *notification10 = [[UILocalNotification alloc]init];
[notification10 setAlertBody:@"FRIDAY NIGHT!"];
[notification10 setFireDate:[gregCalendar10 dateFromComponents:dateComponent10]];
notification10.repeatInterval = NSWeekdayCalendarUnit;
[notification10 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification10];

NSCalendar *gregCalendar11 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponent11 = [gregCalendar11 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];


[dateComponent11 setWeekday:7];
[dateComponent11 setHour:9];
[dateComponent11 setMinute:59];




UILocalNotification *notification11 = [[UILocalNotification alloc]init];
[notification11 setAlertBody:@"HAVE A GREAT WEEKEND!"];
[notification11 setFireDate:[gregCalendar11 dateFromComponents:dateComponent11]];
notification11.repeatInterval = NSWeekdayCalendarUnit;
[notification11 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification11];

NSCalendar *gregCalendar12 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponent12 = [gregCalendar12 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];


[dateComponent12 setWeekday:7];
[dateComponent12 setHour:14];
[dateComponent12 setMinute:58];




UILocalNotification *notification12 = [[UILocalNotification alloc]init];
[notification12 setAlertBody:@"ENJOY YOUR WEEKEND!"];
[notification12 setFireDate:[gregCalendar12 dateFromComponents:dateComponent12]];
notification12.repeatInterval = NSWeekdayCalendarUnit;
[notification12 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification12];

NSCalendar *gregCalendar13 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponent13 = [gregCalendar13 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];


[dateComponent13 setWeekday:7];
[dateComponent13 setHour:19];
[dateComponent13 setMinute:58];




UILocalNotification *notification13 = [[UILocalNotification alloc]init];
[notification13 setAlertBody:@"SATURDAY NIGHT PARTY!"];
[notification13 setFireDate:[gregCalendar13 dateFromComponents:dateComponent13]];
notification13.repeatInterval = NSWeekdayCalendarUnit;
[notification13 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification13];



}

谢谢

这些行导致通知每天重复一次:

notification.repeatInterval = NSWeekdayCalendarUnit;
删除这些行,它将解决您的问题。用户可以确切地说明此属性的功能:

如果您指定了日历单位,例如每周(NSWeekCalendarUnit)或每年(NSYearCalendarUnit),则系统会重新安排通知以指定的时间间隔发送。默认值为0,表示不重复


这些行导致通知每天重复一次:

notification.repeatInterval = NSWeekdayCalendarUnit;
删除这些行,它将解决您的问题。用户可以确切地说明此属性的功能:

如果您指定了日历单位,例如每周(NSWeekCalendarUnit)或每年(NSYearCalendarUnit),则系统会重新安排通知以指定的时间间隔发送。默认值为0,表示不重复


要在一周中的特定日期和特定时间发出通知,应使用以下代码:

NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit  | NSWeekCalendarUnit fromDate:[NSDate date]];

[dateComponent setWeekday:2]; // For Monday
[dateComponent setHour:11];
[dateComponent setMinute:44];

NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];

UILocalNotification *notification = [[UILocalNotification alloc]init];
[notification setAlertBody:@"GOOD MORNING!"];
[notification setFireDate:fireDate];
notification.repeatInterval = NSWeekCalendarUnit;
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
第一部分计算下一个星期一或上一个星期一的日期。 第二部分定义了重复间隔为一周的通知


无论火灾日期是否为前一个星期一,“下一个火灾日期”将自动调整为下周。(您可以验证使用
NSLog(@“%@”,notification)

在一周中的特定日期和特定时间发出通知时,以下代码应起作用:

NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit  | NSWeekCalendarUnit fromDate:[NSDate date]];

[dateComponent setWeekday:2]; // For Monday
[dateComponent setHour:11];
[dateComponent setMinute:44];

NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];

UILocalNotification *notification = [[UILocalNotification alloc]init];
[notification setAlertBody:@"GOOD MORNING!"];
[notification setFireDate:fireDate];
notification.repeatInterval = NSWeekCalendarUnit;
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
第一部分计算下一个星期一或上一个星期一的日期。 第二部分定义了重复间隔为一周的通知


无论火灾日期是否为前一个星期一,“下一个火灾日期”将自动调整为下周。(您可以使用
NSLog(@“%@”,notification)

验证重复间隔必须使用NSWeekCalendarUnit,而不是NSWeekdayCalendarUnit,请参见。谢谢Martin,因此我应该将其设置为:
notification.repeatInterval=NSWeekCalendarUnit用于每个通知,但如何在我的
NSDateComponents
中声明它?有必要吗?谢谢重复间隔必须使用NSWeekCalendarUnit,而不是NSWeekdayCalendarUnit,请参见。谢谢Martin,因此我应该将其设置为:
notification.repeatInterval=NSWeekCalendarUnit用于每个通知,但如何在我的
NSDateComponents
中声明它?有必要吗?谢谢谢谢你的回复。我尝试删除
notification.repeatInterval=NSWeekdayCalendarUnit之前的一行代码,它仍然每天都在发送通知。不过,我对解释的第二部分有点困惑,我是否应该仍然使用repeatInterval,而是使用NSWeekCalendarUnit?谢谢你的帮助!谢谢你的回复。我尝试删除
notification.repeatInterval=NSWeekdayCalendarUnit之前的一行代码,它仍然每天都在发送通知。不过,我对解释的第二部分有点困惑,我是否应该仍然使用repeatInterval,而是使用NSWeekCalendarUnit?谢谢你的帮助!嗨,Martin,我实现了对代码的这些更改,但是我仍然有同样的问题。例如,该应用程序刚刚启动了计划在周五和周六发布的通知。我做错了什么?在安排新设置之前,您是否取消了所有旧通知(
[[UIApplication sharedApplication]cancelAllLocalNotifications]
)?谢谢Martin。运行了一些测试,通知按计划启动@esierr:好的,我很高兴它起到了作用。嗨,Martin,我实现了对代码的这些更改,但是我仍然有同样的问题。例如,该应用程序刚刚启动了计划在周五和周六发布的通知。我做错了什么?在安排新设置之前,您是否取消了所有旧通知(
[[UIApplication sharedApplication]cancelAllLocalNotifications]
)?谢谢Martin。运行了一些测试,通知按计划启动@埃西尔:好的,我很高兴这有帮助。