【问题标题】:Why Notification works with NSHourCalendarUnit but not with NSWeekCalendarUnit?为什么通知适用于 NSHourCalendarUnit 但不适用于 NSWeekCalendarUnit?
【发布时间】:2014-01-20 09:12:08
【问题描述】:

我不明白为什么 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)}

【问题讨论】:

  • 请注意,您可以 NSLog 通知本身以获取有关何时触发的信息。
  • @MartinR 怎么样?和我现在用的有区别吗?
  • NSLog(@"%@", notif) 打印 UILocalNotification 计算的“下一个触发日期”。这只是一个可能有助于解决您的问题的建议。
  • 你会如何处理你的第一点@MartinR?
  • @MartinR 这是一条值得正确答案的惊人信息。谢谢.. 我已经用新的日志信息更新了我的 Q,看看你或任何人是否可以帮助我找到谜团

标签: ios objective-c cocoa-touch notifications nsdate


【解决方案1】:

只需替换

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];

【讨论】:

  • 我已经更新了..仍然无法正常工作:/请再次检查问题..实际上是NSDateComponent问题
  • 我已经编辑了我的答案,试一次,如果它解决了你的目的,请告诉我。
  • 是的,这肯定会在下周的同一时间自动工作。
【解决方案2】:

您不能只在NSDateComponents 中设置weekday 而不调整day 因此。这个计算应该有效:

NSInteger wantedWeekday = 2; // Monday
NSDateComponents *components = [calendar components: NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit
                       fromDate:[NSDate date]];

components.day += wantedWeekday - components.weekday;
components.weekday = wantedWeekday;
components.hour   = 8;
components.minute = 10;
components.second = 35;
NSDate *fire = [calendar dateFromComponents:components];

这会计算上周一或下周一的日期,但不会 事情。如果fire 是过去的,UILocalNotification 会自动更正。

备注: NSLog(@"%@", aNotification) 显示下一个触发日期,计算公式为 UILocalNotification。这有助于诊断问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-09
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多