0

我想让用户在两种类型的通知之间进行选择。其中之一是 10 个不同的本地通知,它们在白天触发,每个通知之间间隔 1 小时,效果很好。第二个选项是一次触发所有通知(我的意思是所有十个通知,每个通知之间的时间间隔为 3 秒)。这是我的方法调度通知:

-(void)scheduleForToday
{
  for (UILocalNotification *notif in [[UIApplication sharedApplication]scheduledLocalNotifications])
      [[UIApplication sharedApplication]cancelLocalNotification:notif];

 for (NSString *string in self.words){
                  NSDateComponents *currentComponents = [[NSCalendar currentCalendar]components:NSYearCalendarUnit|NSDayCalendarUnit|NSMonthCalendarUnit|NSHourCalendarUnit fromDate:[NSDate date]];

    [currentComponents setHour:hour];
    [currentComponents setMinute:minute];
    currentComponents.second = 0;

                  UILocalNotification *notif = [[UILocalNotification alloc] init];
                  notif.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
                  currentComponents.second = [self.words indexOfObject:string]*3;
                  notif.fireDate = [calendar dateFromComponents:currentComponents];
                  notif.alertBody = [NSString stringWithFormat:@"%@",word];
                  notif.alertAction = NSLocalizedString(@"key", nil);
                  notif.userInfo = userDict;
                  [[UIApplication sharedApplication] scheduleLocalNotification:notif];
}

}

方法执行后,我把

NSLog(@"Finished Setiing Today Notifications %@",[[UIApplication sharedApplication] scheduledLocalNotifications]);

而且我得到了正确的通知时间表,但是当我的应用程序在后台时,无论是在设备上还是在模拟器上,它们都没有显示出来。可能是什么问题?任何建议表示赞赏。 通知时间表

4

2 回答 2

1

首先,您可以摆脱取消通知的初始循环并使用:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

然后,不要使用 for-in 语句,而只需使用常规 for 语句,这样您就可以跟踪索引。

for (int i = 0; i < self.words.count; i ++) {

}

然后,对于开火日期,您应该尝试使用 NSDatedateByAddingTimeInterval:并将 index+1 乘以您希望在通知之间的时间量。您之前的做法是或多或少地安排所有通知在彼此的毫秒内触发。

notif.fireDate = [[NSDate date] dateByAddingTimeInterval:(i + 1) * 3];
于 2013-08-09T11:13:00.443 回答
-3

为 NSLocal 通知设置 repeatInterval

 notif.repeatInterval=3*NSSecondCalendarUnit;
于 2013-08-09T11:11:18.430 回答