【问题标题】:UILocalNotification every Saturday at 12:00 amUILocalNotification 每周六上午 12:00
【发布时间】:2013-02-28 05:17:57
【问题描述】:

我尝试每周六重复一次 UILocalNotification,但没有成功。 我尝试以下方法:

// Create notification for every saturday at 12:00 am
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSUIntegerMax fromDate:[NSDate date]];
[components setWeekday:7]; // Saturday
[components setHour:12]; // 12 am
[components setMinute:0];
[components setSecond:0];

NSDate *fireDate = [gregorian dateFromComponents:components];         
NSLog(@"%@", fireDate);

[[NotificationsManager sharedManager] configureLocalAlertMessage:message
                                                                    fireDate:fireDate
                                                          repeatIntervalUnit:NSWeekCalendarUnit
                                                                        type:kNotificationTypeMoneyBox];

但是“fireDate”返回:“2013-03-12 11:00:00 +0000”,即今天。

有什么帮助吗? 谢谢!

编辑:

配置通知的代码

- (void)configureLocalAlertMessage:(NSString *)message fireDate:(NSDate *)date repeatIntervalUnit:(NSCalendarUnit)repeat type:(NotificationType)type {

    // Local notification
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.timeZone = [NSTimeZone systemTimeZone];
    notification.fireDate = date;
    notification.repeatInterval = repeat;
    notification.soundName = UILocalNotificationDefaultSoundName;
    notification.hasAction = YES;
    notification.alertBody = message;
    notification.userInfo = @{kNotificationTypeKey : @(type)};
    notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

    // Schedule
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

}

【问题讨论】:

  • 设置本地通知的代码在哪里。此代码仅用于创建日期。
  • 我添加了新信息。谢谢!
  • "当信息不一致时,日历可能会忽略某些组件参数或者方法可能返回nil。"
  • 好的,但是我该怎么做呢?我需要在 12 周内的每个星期六凌晨 12:00 为 UILocalNotification 编程...
  • 提供一致的信息。 NSCalendar 不知道将什么日期放入 NSDate,因此显然它默认为今天。

标签: objective-c nsdate uilocalnotification repeat nsdatecomponents


【解决方案1】:
    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *weekdayComponents =[gregorian components:(NSDayCalendarUnit |               NSWeekdayCalendarUnit | NSWeekCalendarUnit | NSYearCalendarUnit) fromDate:idate];

    int weekday = [weekdayComponents weekday];
    NSDate *nextdate ;
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    if(weekday <= 5)
    {
       /* it is my case to check the weekday, if it is less or greater than 5*/
    }
    else
    {
        [offsetComponents setDay:7];
        [offsetComponents setHour:12];
        [offsetComponents setMinute:0];
        [offsetComponents setSecond:0];
        nextdate = [gregorian dateByAddingComponents:offsetComponents toDate:idate options:0];
    }
    [offsetComponents release];
    [gregorian release];

您可以将此下一个日期用作本地通知的触发日期。 这里 idate 是 NSDate *idate = [NSDate date];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多