在未来日期范围内的schedulelocalnotification

时间:2013-02-17 16:12:59

标签: ios localnotification

- (void) scheduleGeneralNotificationFrequentlyWithItem:(ToDoItem *)item interval:(int)minutesBefore frequent:(NSCalendarUnit)frequentUnit{
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:item.day];
    [dateComps setMonth:item.month];
    [dateComps setYear:item.year];
    [dateComps setHour:item.hour];
    [dateComps setMinute:item.minute];
    NSDate *itemDate = [calendar dateFromComponents:dateComps];
    [dateComps release];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];

    if (localNotif == nil)
        return;
    localNotif.fireDate = [itemDate dateByAddingTimeInterval:-(minutesBefore*60)];
    localNotif.timeZone = [NSTimeZone defaultTimeZone];

    if (!item.eventName) {
        item.eventName = @"My event name";
    }
    localNotif.alertBody = item.alertBody;
    localNotif.alertAction = NSLocalizedString(@"Details", nil);

    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;


    localNotif.userInfo = item.data; // some data
    localNotif.repeatInterval = frequentUnit; // here I put NSDayCalendarUnit
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];
    NSString *isDisableLocalPush = [Settings getSetting:kPraktijDisableLocalPushKey];
    if ([isDisableLocalPush isEqualToString:kJSONValueYes]) { //disable all notifications after added it
        [self disableAllLocationPushNotifications];
    }
}

在上面的代码中,它会每天安排到localNotif.fireDate,对吗?

我想要实现的目标是: 安排下一个月到另一个月的每一天的通知 例如:2013年/ 3月/ 01日至2013年5月的每日通知

1 个答案:

答案 0 :(得分:0)

不,它会安排从开火日期开始的每日通知(直到您取消它)。

如果您想安排一个日期范围并且它不超过您可以一次设置的本地通知限制(这是64,仍然少于您需要的2个月),我认为最好是安排每天一个接一个的本地通知。

首先使用NSDate对象作为开始日期,安排本地通知而不重复间隔,将NSDate增加一天,然后重复安排本地通知,直到达到范围的结束日期。

相关问题