【发布时间】:2015-08-28 12:26:04
【问题描述】:
我正在使用for 循环添加本地通知,但是当我检查当前使用[[UIApplication sharedApplication] scheduledLocalNotifications] 安排了多少通知时,它显示当前只安排了一个通知。
这是我的代码:
for (int i = ((int)day - 1) * 6; i < ((int)day - 1) * 6 + 64; i++) {
NSLog(@"Notification #%d", i);
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSDictionary *currentPrayTime = [self.prayTimes objectAtIndex:i];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy.MM.dd HH:mm"];
NSDate *dateToSetNotification = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@ %@", [currentPrayTime objectForKey:@"date"], [currentPrayTime objectForKey:@"time"]]];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setMinute:-10];
NSDate *dateBeforePray = [gregorian dateByAddingComponents:offsetComponents toDate:dateToSetNotification options:0];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = dateBeforePray;
localNotif.timeZone = [NSTimeZone localTimeZone];
localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%d минут до %@", nil), [[NSUserDefaults standardUserDefaults] integerForKey:@"minutesBefore"], [currentPrayTime objectForKey:@"title"]];
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.userInfo = currentPrayTime;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
感谢您的提前!
编辑:
唯一显示的通知是循环中的最后一个本地通知。
【问题讨论】:
-
为什么 localNotif.applicationIconBadgeNumber = 1 ?应为每个通知更新徽章编号。
-
请尝试重新检查所有 dateBeforePray 是未来的日期和时间。
-
我查过了,只有过去的祈祷有旧的日期(
-
请同时考虑日期和时间。如果日期和时间是未来的日期和时间,我已经在这里测试了它的工作正常