[英]Weekday Local Notification Not Firing
I'm building an application that allows the user to have a local notification to repeat every weekday, because apple doesn't have a repeat interval for WeekDays I needed to create a local notification for every week day. 我正在构建一个应用程序,允许用户在每个工作日重复发送本地通知,因为苹果没有工作日的重复间隔,因此我需要为每个工作日创建本地通知。 I made a for loop looping 5 times the number of days in a work week.
我做了一个for循环,是一周工作的天数的5倍。 However it doesn't seem to be firing.
但是它似乎并没有触发。 But when I got to delete the notifications I see all five, the just wont fire.
但是,当我删除通知时,我看到了全部五个通知,这简直就是开火。 This is what my code looks like.
这就是我的代码。
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);
}
If anyone has any ideas please let me know! 如果有人有任何想法,请告诉我! Thanks in advance.
提前致谢。
Update: 更新:
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);
Okay I was finaly able to get local notifications to repeat every weekday, I added [setHour]
and [setMinute]
as well as changed the int in the for loop to a NSUInteger. 好的,我最终能够在每个工作日重复发送本地通知,我添加了
[setHour]
和[setMinute]
,并将for循环中的int更改为NSUInteger。 Here is my code below. 这是下面的代码。
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);
}
They don't fire because the date you give them is invalid. 他们不会解雇,因为您给他们的日期无效。
Here's how I generated the date for X number of days in the future: 这是我如何生成未来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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.