• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 为什么通知适用于 NSHourCalendarUnit 但不适用于 NSWeekCalendarUnit?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 18:06:07 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我不明白为什么 LocalNotification 可以完美地与 NSHourCalendarUnit 一起工作,但永远不会与 NSWeekCalendarUnitNSDayCalendarUnit 一起工作,尽管它显示了正确的时间日志!!当设置 weekday 我需要它来触发时,一定有什么我错过了。它会是什么?

请查看我的代码日志出来,以便您可以指导我正在做的错误:

-(void) Monday {   // 9th Notification Every 1:35

NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.timeZone = [NSTimeZone timeZoneWithName"GMT"];

NSDateComponents *components = [calendar components: NSDayCalendarUnit | NSYearCalendarUnit | NSWeekCalendarUnit fromDate:[NSDate date]];

components.weekday = 2;
components.hour   = 10;
components.minute = 56;
components.second = 35;

NSDate *fire = [calendar dateFromComponents:components];
NSLog(@"The Fire Day is:: %@", fire);


UILocalNotification *notif = [[UILocalNotification alloc]  init] ;
if (notif == nil)
    return;

notif.fireDate = fire;
notif.repeatInterval= NSWeekCalendarUnit ;
notif.soundName = @"ring.wav";
notif.alertBody = [NSString stringWithFormat: @"THANKS for Your HELP "] ;
[[UIApplication sharedApplication] scheduleLocalNotification:notif] ;

NSLog(@"*****NEW LOG Notification Details*****%@", notif);
}

这是日志:

2014-01-20 10:09:12.935 daily notification test[17591:c07] The Fire Day is:: 2014-01-20 10:10:35 +0000

*日志更新:*

2014-01-20 10:56:21.694 daily notification test[17823:c07] The Fire Day is:: 2014-01-20 10:56:35 +0000
2014-01-20 10:56:21.697 daily notification test[17823:c07] *****NEW LOG Notification Details*****<UIConcreteLocalNotification: 0x7180360>{fire date = Monday, January 20, 2014, 11:56:35 AM Central European Standard Time, time zone = (null), repeat interval = NSWeekCalendarUnit, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Monday, January 20, 2014, 11:56:35 AM Central European Standard Time, user info = (null)}
.

2014-01-20 10:56:21.694 每日通知测试[17823:c07] The Fire Day is::2014-01-20 10:56:35 +0000 2014-01-20 10:56:21.697 每日通知测试[17823:c07] **新日志通知详细信息**{发布日期 = 2014 年 1 月 20 日星期一, 11:56:35 AM 中欧标准时间,时区 = (null),重复间隔 = NSWeekCalendarUnit,重复计数 = UILocalNotificationInfiniteRepeatCount,下一个触发日期 = 2014 年 1 月 20 日,星期一,11:56:35 AM 中欧标准时间, 用户信息 = (null)}



Best Answer-推荐答案


只需替换

notif.repeatInterval= NSHourCalendarUnit ;

notif.repeatInterval= NSWeekCalendarUnit;

编辑

现在试试这个,它可能会有所帮助

NSCalendar *gregorian1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        [gregorian1 setLocale:[NSLocale currentLocale]];

        NSDateComponents *nowComponents = [gregorian1 components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate new]];

        [nowComponents setWeekday:2]; //Monday
       [nowComponents setWeek: [nowComponents week] + 1]; //Next week
        [nowComponents setHour:8]; //8a.m.
       [nowComponents setMinute:0];
        [nowComponents setSecond:0];

        NSDate *beginningOfWeek = [gregorian1 dateFromComponents:nowComponents];

关于ios - 为什么通知适用于 NSHourCalendarUnit 但不适用于 NSWeekCalendarUnit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21230024/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap