2015-06-03 14 views
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]; 
} 

感謝任何前進!

編輯:

唯一的一個通知,該圖是在循環中的最後一個本地通知。

+0

爲什麼localNotif.applicationIconBadgeNumber = 1?應爲每個通知更新徽章號碼。 – Vizllx

+0

請嘗試重新檢查所有dateBeforePray是未來的日期和時間。 –

+0

我查過,只有過去的祈禱有舊約會(<5),其他有未來約會。 –

回答

0

請重新檢查UILocalNotification fireDate,它不應該過去日期表示小於當前日期&時間。

localNotif.fireDate = dateBeforePray; 
NSLog(@"%@",localNotif.fireDate); 

希望這會幫助你。

+0

當我檢查所有日期在未來,但只有最後顯示** [[UIApplication sharedApplication] scheduledLocalNotifications] ** –

相關問題