【问题标题】:UILocalNotification firing repeatative for particular daysUILocalNotification 在特定日期重复触发
【发布时间】:2016-03-17 06:42:06
【问题描述】:

我正在开发闹钟应用程序,我想做与 iOS 闹钟应用程序中相同的重复闹钟功能,我们可以在其中选择重复选项,如每周一、每周二等。

示例:如果我只选择每周一、每周三和每周 星期五然后对于所有即将到来的星期一、星期三和星期五 应触发相应的警报通知。

我试过了:

  NSCalendar *calendar = [NSCalendar currentCalendar];
  NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitWeekOfYear |  NSCalendarUnitHour | NSCalendarUnitMinute| NSCalendarUnitSecond | NSCalendarUnitWeekday) fromDate: [dictionaryForAlarm objectForKey:kAlarmSnoozeDate]];

 NSArray *arrayForRepeatDays = @[@"2",@"4",@"6"];

 for (NSString *dayOfWeek in arrayForRepeatDays) {

     [componentsForFireDate setDay:[dayOfWeek integerValue]];
     localNotification.repeatInterval =  NSCalendarUnitWeekOfMonth;;

     [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

但它在相应的日子里没有着火,请纠正我

提前感谢您!

【问题讨论】:

    标签: ios uilocalnotification repeatingalarm


    【解决方案1】:

    我认为你需要做这样的代码:

    NSCalendar *calendar = [NSCalendar currentCalendar];
      NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitWeekOfYear |  NSCalendarUnitHour | NSCalendarUnitMinute| NSCalendarUnitSecond | NSCalendarUnitWeekday) fromDate: [dictionaryForAlarm objectForKey:kAlarmSnoozeDate]];
    
     NSArray *arrayForRepeatDays = @[@"2",@"4",@"6"];
    
     for (NSString *dayOfWeek in arrayForRepeatDays) {
    
         [componentsForFireDate setWeekday: [dayOfWeek integerValue]] 
         [componentsForFireDate setHour: 10] ; // here you can set hr as per you need
         localNotification.repeatInterval =  NSWeekCalendarUnit;
         localNotification.fireDate=[calendar dateFromComponents:componentsForFireDate];
         [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    }
    

    我猜这两个会让这个工作:

    [componentsForFireDate setWeekday: [dayOfWeek integerValue]] 
    localNotification.repeatInterval =  NSWeekCalendarUnit;
    

    【讨论】:

    • 使用 NSCalendarUnitWeekOfMonth 因为 NSWeekCalendarUnit 仍然没有发出通知,当我保存它时,立即触发 3 个通知(因为今天保持为星期五,星期一和星期三已经过去)如何处理?跨度>
    • 检查我更新的答案我认为你忘记设置 fireDate
    • setHour 到底是什么意思?
    • 它是指那天的时间,例如你设置的星期一但是星期一是哪个时间???那个通知火了??所以我们使用 hr,min,sec 来设置一天中的时间
    • 我一定会试试这个
    猜你喜欢
    • 2013-03-16
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多