工作日本地通知未触发
Weekday Local Notification Not Firing
我正在构建一个允许用户在每个工作日重复本地通知的应用程序,因为苹果没有 WeekDays 的重复间隔我需要为每个工作日创建一个本地通知。我做了一个 for 循环,循环次数是工作周天数的 5 倍。不过好像没开火。但是当我删除通知时,我看到了所有五个,只是不会开火。这就是我的代码的样子。
NSDate *today = [NSDate date];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = _alarmTime;
localNotification.alertBody = @"Wake up or pay-up!";
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.alertAction = @"Wake up or pay-up!";
localNotification.soundName = @"sound_ring.caf";
NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:_alarmTime];
switch (_repeatInt) {
case 1:
localNotification.repeatInterval = NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
_repeatString = @"Everyday";
break;
case 2:
//Attempting to create 5 local notifications with different days but same time.
for (int i = 2; i <= 6; i++){
[dateComponent setWeekday:i]; // 2 = mon // 3= tues // 4 = wends // 5 = thurs // 6 = fri
NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];
UILocalNotification *localNotification2 = [localNotification copy];
localNotification2.fireDate = fireDate;
localNotification2.repeatInterval = NSWeekCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
NSLog(@"%D",i);
}
如果有人有任何想法,请告诉我!提前致谢。
更新:
NSDate *startDate = [gregCalendar dateFromComponents:dateComponent];
[dateComponent setMonth:0];
[dateComponent setDay:i]; // 2 = mon // 3= tues // 4 = wends // 5 = thurs // 6 = fri
[dateComponent setYear:0];
NSDate *fireDate = [gregCalendar dateByAddingComponents:dateComponent toDate:startDate options:0];
UILocalNotification *localNotification2 = [localNotification copy];
localNotification2.fireDate = fireDate;
localNotification2.repeatInterval = NSCalendarUnitWeekOfYear;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
NSLog(@"%D",i);
他们不会解雇,因为你给他们的日期无效。
以下是我生成未来 X 天日期的方法:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:[NSDate date]];
NSDate *startDate = [calendar dateFromComponents:components];
[components setMonth:0];
[components setDay:X];
[components setYear:0];
NSDate *endDate = [calendar dateByAddingComponents:components toDate:startDate options:0];
好吧,我终于能够让本地通知在每个工作日重复,我添加了 [setHour]
和 [setMinute]
并将 for 循环中的 int 更改为 NSUInteger。下面是我的代码。
NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:_alarmTime];
switch (_repeatInt) {
case 1:
localNotification.repeatInterval = NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
_repeatString = @"Everyday";
break;
case 2:
for (NSUInteger i = 2; i <= 6; i++) {
NSDateComponents *components = [gregCalendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:_alarmTime];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
[dateComponent setMinute:minute];
[dateComponent setWeekday:i]; // 2 = mon // 3= tues // 4 = wends // 5 = thurs // 6 = fri
[dateComponent setHour:hour];
NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];
UILocalNotification *localNotification2 = [[UILocalNotification alloc] init];
localNotification2.fireDate = fireDate;
localNotification2.alertBody = @"Wake up or pay-up!";
localNotification2.timeZone = [NSTimeZone systemTimeZone];
localNotification2.alertAction = @"Wake up or pay-up!";
localNotification2.soundName = @"sound_ring.caf";
localNotification2.repeatInterval = NSCalendarUnitWeekOfYear;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
NSLog(@"%lu",(unsigned long)i);
}
我正在构建一个允许用户在每个工作日重复本地通知的应用程序,因为苹果没有 WeekDays 的重复间隔我需要为每个工作日创建一个本地通知。我做了一个 for 循环,循环次数是工作周天数的 5 倍。不过好像没开火。但是当我删除通知时,我看到了所有五个,只是不会开火。这就是我的代码的样子。
NSDate *today = [NSDate date];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = _alarmTime;
localNotification.alertBody = @"Wake up or pay-up!";
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.alertAction = @"Wake up or pay-up!";
localNotification.soundName = @"sound_ring.caf";
NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:_alarmTime];
switch (_repeatInt) {
case 1:
localNotification.repeatInterval = NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
_repeatString = @"Everyday";
break;
case 2:
//Attempting to create 5 local notifications with different days but same time.
for (int i = 2; i <= 6; i++){
[dateComponent setWeekday:i]; // 2 = mon // 3= tues // 4 = wends // 5 = thurs // 6 = fri
NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];
UILocalNotification *localNotification2 = [localNotification copy];
localNotification2.fireDate = fireDate;
localNotification2.repeatInterval = NSWeekCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
NSLog(@"%D",i);
}
如果有人有任何想法,请告诉我!提前致谢。
更新:
NSDate *startDate = [gregCalendar dateFromComponents:dateComponent];
[dateComponent setMonth:0];
[dateComponent setDay:i]; // 2 = mon // 3= tues // 4 = wends // 5 = thurs // 6 = fri
[dateComponent setYear:0];
NSDate *fireDate = [gregCalendar dateByAddingComponents:dateComponent toDate:startDate options:0];
UILocalNotification *localNotification2 = [localNotification copy];
localNotification2.fireDate = fireDate;
localNotification2.repeatInterval = NSCalendarUnitWeekOfYear;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
NSLog(@"%D",i);
他们不会解雇,因为你给他们的日期无效。
以下是我生成未来 X 天日期的方法:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:[NSDate date]];
NSDate *startDate = [calendar dateFromComponents:components];
[components setMonth:0];
[components setDay:X];
[components setYear:0];
NSDate *endDate = [calendar dateByAddingComponents:components toDate:startDate options:0];
好吧,我终于能够让本地通知在每个工作日重复,我添加了 [setHour]
和 [setMinute]
并将 for 循环中的 int 更改为 NSUInteger。下面是我的代码。
NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:_alarmTime];
switch (_repeatInt) {
case 1:
localNotification.repeatInterval = NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
_repeatString = @"Everyday";
break;
case 2:
for (NSUInteger i = 2; i <= 6; i++) {
NSDateComponents *components = [gregCalendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:_alarmTime];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
[dateComponent setMinute:minute];
[dateComponent setWeekday:i]; // 2 = mon // 3= tues // 4 = wends // 5 = thurs // 6 = fri
[dateComponent setHour:hour];
NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];
UILocalNotification *localNotification2 = [[UILocalNotification alloc] init];
localNotification2.fireDate = fireDate;
localNotification2.alertBody = @"Wake up or pay-up!";
localNotification2.timeZone = [NSTimeZone systemTimeZone];
localNotification2.alertAction = @"Wake up or pay-up!";
localNotification2.soundName = @"sound_ring.caf";
localNotification2.repeatInterval = NSCalendarUnitWeekOfYear;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
NSLog(@"%lu",(unsigned long)i);
}