如何安排LocalNotifications每2天或每3天触发一次。 [英] How to schedule LocalNotifications to fire on Every 2 Days or Every 3 Days.

查看:97
本文介绍了如何安排LocalNotifications每2天或每3天触发一次。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我必须安排本地通知。考虑用户输入上午7点的时间,然后必须根据他设置的设置每天(或者,2天,或每3天)在上午7点触发通知。在用户关闭通知之前,必须定期触发此通知。

I am having an app in which I have to schedule Local Notifications. Consider a user enters the time 7 AM, then the Notification 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.

我怀疑如何安排通知每2天或每3天触发一次或者每两周一次?对不起,如果这是一个非常基本的问题。我是本地通知的新手。

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

您需要以字符串和日期传递开始日期和结束日期报警的间隙和这个函数返回一个字符串中的日期数组。现在你需要使用这些日期并追加你的时间,然后通过 NSDateFormatter 制作日期对象,然后将其设置为 fireDate 本地通知。

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.

请参阅链接了解更多信息

这篇关于如何安排LocalNotifications每2天或每3天触发一次。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆