【问题标题】:Why only one local notification is set?为什么只设置了一个本地通知?
【发布时间】: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 是未来的日期和时间。
  • 我查过了,只有过去的祈祷有旧的日期(
  • 请同时考虑日期和时间。如果日期和时间是未来的日期和时间,我已经在这里测试了它的工作正常

标签: ios uilocalnotification


【解决方案1】:

请重新检查 UILocalNotification fireDate ,它不应该是过去的日期意味着小于当前的日期和时间。

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

希望这会对你有所帮助。

【讨论】:

  • 当我检查所有日期都在未来,但最后显示 [[UIApplication sharedApplication] scheduledLocalNotifications] 调用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-03
  • 1970-01-01
  • 2013-11-09
  • 2013-01-24
  • 2023-04-02
相关资源
最近更新 更多