开发者

How to schedule LocalNotifications to fire on Every 2 Days or Every 3 Days.

开发者 https://www.devze.com 2023-02-15 05:39 出处:网络
I am having an app in which I have to schedule Local Notifications. Consider a user enters the time 7 AM, then the Notificatio开发者_StackOverflown has to be fired on 7 AM every day (or, ever 2 days,

I am having an app in which I have to schedule Local Notifications. Consider a user enters the time 7 AM, then the Notificatio开发者_StackOverflown has to be fired on 7 AM every day (or, ever 2 days, or every 3 days) based on the settings he set. This Notification has to be fired on regular basis until the user turns of the Notification.

My doubt is how to schedule the Notification to fire on every 2 days or every 3 days or every 2 weeks like that? Sorry if this is a very basic question. I am new to Local Notifications.


What you need you need to make a date list from a start date to end date then need to enter a gap in days for alarms and according to that gap you need to set alarms.

date with time is required for alarms.

see this function this return an array of dates with gap in days

-(NSMutableArray *)getDatesArrayBetweenDates:(NSString *)minDate toDate:(NSString *)maxDate freqDays:(NSInteger )dayfreq
{
    // minDate and maxDate represent your date range
    NSMutableArray *resultArray=[NSMutableArray array];
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    NSDateComponents *days = [[NSDateComponents alloc] init];
    NSDate *minDateObject;
    NSDate *maxDateObject;
    NSDateFormatter *df=[[NSDateFormatter alloc] init];
    [df setDateFormat:@"MM/dd/yyyy"];
    minDateObject=[df dateFromString:minDate];
    maxDateObject=[df dateFromString:maxDate];
    NSInteger dayCount = 0;
    while ( TRUE ) {

        NSDate *date = [gregorianCalendar dateByAddingComponents: days toDate: minDateObject options: 0];
        [days setDay: ++dayCount];
        if ( [date compare: maxDateObject] == NSOrderedDescending )
            break;
        // Do something with date like add it to an array, etc.
        NSString *dateForAdd=[df stringFromDate:date];
        //if(![self.dateListArray containsObject:dateForAdd])
            [resultArray addObject:dateForAdd];
    }
    [df release];
    [days release];
    [gregorianCalendar release];

    NSMutableArray *returnableArray=[NSMutableArray array];
    NSInteger day=0;
    for(int i=0;i<[resultArray count];i++)
    {
       if(day==0)
       {
           [returnableArray addObject:[resultArray objectAtIndex:i]];
       }

        if(day==dayfreq)
            day=0;
        else {
            ++day;
        }

    }
    return returnableArray;
}

what you need to pass a start date and end date in string and the days gap for alarm and this function returns you a array of dates in strings.Now you need to use these date and append your time then make date object by NSDateFormatter then set it for fireDate of local notification.

see this link for more info


UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [put a NSDate object here];
localNotification.repeatInterval = NSDayCalendarUnit;

This creates a local notification that repeats daily.

0

精彩评论

暂无评论...
验证码 换一张
取 消