0
我正在使用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?應爲每個通知更新徽章號碼。 – Vizllx
請嘗試重新檢查所有dateBeforePray是未來的日期和時間。 –
我查過,只有過去的祈禱有舊約會(<5),其他有未來約會。 –