【问题标题】:UILocalNotification with repeatInterval set NSWeekdayCalendarUnit doesn't trigger weekly带有 repeatInterval 设置 NSWeekdayCalendarUnit 的 UILocalNotification 不会每周触发
【发布时间】:2015-04-24 04:39:48
【问题描述】:

我正在尝试将本地通知设置为每周重复一次。
这是我的设置:

UILocalNotification* localNotification =  [[UILocalNotification alloc] init];
...
localNotification.repeatInterval = NSWeekdayCalendarUnit;

在控制台日志中:

localNotif:{触发日期 = 2015 年 4 月 24 日星期五下午 12:27:33 新加坡标准时间,时区 = 亚洲/新加坡 (GMT+8) 偏移 28800,重复间隔 = NSWeekdayCalendarUnit,重复计数 = UILocalNotificationInfiniteRepeatCount,下次开火日期 = 新加坡标准时间 2015 年 4 月 25 日星期六下午 12:27:33,用户信息 = { KUserLocalNotficationKey = "2015-04-24 04:27:33 +0000"; }}

如您所见,下一个触发日期是在第二天触发。这是一个错误吗?
我已经在 iOS 7.1、8.1、8.3 中测试过了。

【问题讨论】:

  • 你想要NSWeekCalendarUnit
  • 你有两个选择取决于你如何使用
  • @Paulw11 哦,是的,应该是NSWeekCalendarUnit 。谢谢!

标签: ios objective-c uilocalnotification nscalendar


【解决方案1】:

你必须像这样编码

UILocalNotification* localNotification =  [[UILocalNotification alloc] init];
...
localNotification.repeatInterval = NSCalendarUnitWeekOfYear;

它每周触发通知。

【讨论】:

  • NSCalendarUnitWeekOfYear, NSCalendarUnitWeekOfMonth, NSCalendarUnitWeekdayOrdinal 呢?
  • @Supertecnoboff 查看苹果文档。你可以更好地了解它
  • @chiragshah NSCalendarUnitWeekOfYear 工作年改了吗?
  • @Gati 我不确定,所以不要给你任何评论
【解决方案2】:

PLz试试这个

  UILocalNotification *localNotif=[[UILocalNotification alloc]init];
        localNotif.fireDate =currentDate;
        localNotif.timeZone=[NSTimeZone defaultTimeZone];
        localNotif.alertBody = @"MazeRoll";
        localNotif.soundName = UILocalNotificationDefaultSoundName;
        localNotif.applicationIconBadgeNumber = 1;
        localNotif.repeatInterval=NSWeekCalendarUnit;
        UIApplication *app=[UIApplication sharedApplication];
        [app scheduleLocalNotification:localNotif];
        NSLog(@"sdfsdfsdf%@",[[UIApplication sharedApplication] scheduledLocalNotifications]);

【讨论】:

  • NSWeekCalendarUnit 在 ios 7.0 中被废弃
  • iOS 9 应该使用什么? NSCalendarUnitWeekOfYear, NSCalendarUnitWeekOfMonth, NSCalendarUnitWeekdayOrdinal??
【解决方案3】:

您可以使用NSCalendarUnitWeekOfYear 设置本地通知并设置适当的触发日期。

UILocalNotification *localNotification=[[UILocalNotification alloc]init];
localNotification.fireDate =[NSDate date]; // set your week day on which repeatedly you want local notfication. for example Monday, then set Fire date of next monday.
localNotification.timeZone=[NSTimeZone defaultTimeZone];
localNotification.repeatInterval=NSCalendarUnitWeekOfYear;
NSLog(@"%@",localNotification);

希望这会有所帮助。享受编码!

【讨论】:

    【解决方案4】:
     NSDate *currentDate = [NSDate date];
        NSDate *futureTime = [currentDate dateByAddingTimeInterval:60*60*168];
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
        [calendar setTimeZone:timeZone];
        NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit fromDate:futureTime];
        if ([components hour] >= 19) { // make it the next day
            [components setDay:[components day] + 1 ];
        }
        [components setHour:8];
        [components setMinute:00];
        NSDate *alertTime = [calendar dateFromComponents:components];
        NSLog(@"alertTime %@",alertTime.description);
        UILocalNotification *localNotif=[[UILocalNotification alloc]init];
        localNotif.fireDate =alertTime;
        localNotif.timeZone=[NSTimeZone defaultTimeZone];
        localNotif.alertBody = @"MazeRoll";
        localNotif.soundName = UILocalNotificationDefaultSoundName;
        localNotif.applicationIconBadgeNumber = 1;
        localNotif.repeatInterval=NSDayCalendarUnit;
        UIApplication *app=[UIApplication sharedApplication];
        [app scheduleLocalNotification:localNotif];
        NSLog(@"sdfsdfsdf%@",[[UIApplication sharedApplication] scheduledLocalNotifications]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-14
      • 2014-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多