Ios 如何将NSLocalNotification设置为每月或每周在计划的同一时间运行?

Ios 如何将NSLocalNotification设置为每月或每周在计划的同一时间运行?,ios,uilocalnotification,Ios,Uilocalnotification,我的应用程序包含来自服务器的数据。我有两种类型的通知,比如每月一次。现在,我将使用(日期-时间格式)2-2:30,6-9:00,23-10:00,26-12:00获取月份数据。其中2表示日期,后跟冒号(:)格式的时间 现在,我希望每月在相同的日期和时间运行通知。我能够运行多个通知。但它们并不是每个月都重复。怎么做?下面是我的代码 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

我的应用程序包含来自服务器的数据。我有两种类型的通知,比如每月一次。现在,我将使用(日期-时间格式)2-2:30,6-9:00,23-10:00,26-12:00获取月份数据。其中2表示日期,后跟冒号(:)格式的时间

现在,我希望每月在相同的日期和时间运行通知。我能够运行多个通知。但它们并不是每个月都重复。怎么做?下面是我的代码

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
          [UIApplication sharedApplication].idleTimerDisabled = YES;
            UIUserNotificationSettings *notiSett = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:NULL];
            [[UIApplication sharedApplication]registerUserNotificationSettings:notiSett];
                    [self generateLocalNotificationDaily];
    }

   -(void)generateLocalNotificationDaily
{
//  [[UIApplication sharedApplication] cancelAllLocalNotifications];
    [self compareDateDaily];
}

-(void)compareDateDaily
{
    NSString *strTime = [Helper getPREF:PREF_AlARM_DAILY_TIME];
    NSArray *arrtime = [strTime componentsSeparatedByString:@","];

    for(int i=0;i<[arrtime count];i++)
    {
        NSArray *arrfirstTime = [[NSString stringWithFormat:@"%@",[arrtime objectAtIndex:i]]componentsSeparatedByString:@":"];

        int hh = (int)[[arrfirstTime objectAtIndex:0]integerValue];
        int mm = (int)[[arrfirstTime objectAtIndex:1]integerValue];

        NSDate *now = [NSDate date];
        NSTimeInterval secondsPerDay = 1;//24 * 60 * 60;
        NSDate *date = [now dateByAddingTimeInterval:secondsPerDay];
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];
        NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
        [components setHour:hh];
        [components setMinute:mm];
        NSDate *todaySpecificDate = [calendar dateFromComponents:components];

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        [formatter setTimeZone:[NSTimeZone systemTimeZone]];
        NSDate *currentDate = [NSDate date];
        NSString *strCurrentDate = [formatter stringFromDate:currentDate];
        NSString *strSystemDate = [formatter stringFromDate:todaySpecificDate];
        currentDate = [formatter dateFromString:strCurrentDate];
        todaySpecificDate = [formatter dateFromString:strSystemDate];

        NSComparisonResult result = [currentDate compare:todaySpecificDate];

        NSString *strAlertMessage = [NSString stringWithFormat:@"%@",[Helper getPREF:PREF_AlARM_MESSAGE]];
        NSString *strUserName = [Helper getPREF:PREF_NAME];
        NSString *strmsg = [strAlertMessage stringByReplacingOccurrencesOfString:@"user_name" withString:strUserName];
        NSString *alertMsg = [strmsg stringByReplacingOccurrencesOfString:@"notification_type" withString:@"daily"];

        //
        switch (result)
        {
            case NSOrderedAscending:
            {
                UILocalNotification* localNotificationDaily = [[UILocalNotification alloc] init];
                localNotificationDaily.fireDate = todaySpecificDate;
                localNotificationDaily.alertBody = alertMsg;
                localNotificationDaily.repeatInterval = NSCalendarUnitDay;

                localNotificationDaily.soundName = UILocalNotificationDefaultSoundName;
                [[UIApplication sharedApplication] scheduleLocalNotification:localNotificationDaily];
                NSLog(@"%@ is in future from %@", todaySpecificDate, currentDate);
            }
                break;
            case NSOrderedDescending:
                NSLog(@"%@ is in past from %@", todaySpecificDate, currentDate);
                break;
            case NSOrderedSame:
                NSLog(@"%@ is the same as %@", todaySpecificDate, currentDate);
                break;
            default:
                NSLog(@"erorr dates %@, %@", todaySpecificDate, currentDate);
                break;
        }
    }
}
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{
[UIApplication sharedApplication].idleTimerDisabled=是;
UIUserNotificationSettings*notiSett=[UIUserNotificationSettings设置类型:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound类别:NULL];
[[UIApplication sharedApplication]注册表通知设置:notiSett];
[自生通讯日报];
}
-(无效)每日生成本地通知
{
//[[UIApplication sharedApplication]取消所有本地通知];
[自我比较每日];
}
-(无效)每日比较
{
NSString*strTime=[Helper getPREF:PREF_AlARM_DAILY_TIME];
NSArray*arrtime=[strTime组件由字符串分隔:@“,”];

对于(int i=0;i,uiLocalNotification“repeatInterval”中有一个属性。您必须将重复间隔设置为如下月份

localNotificationMonthly.repeatInterval = NSCalendarUnitMonth;
它将根据您为通知设置的日期和时间,每月重复您的通知。有关进一步的查询,请参阅


对于周重复间隔为NSCalendarUnitWeekOfYear的情况,uiLocalNotification中有一个属性“repeatInterval”。您必须将重复间隔设置为月份,如下所示

localNotificationMonthly.repeatInterval = NSCalendarUnitMonth;
它将根据您为通知设置的日期和时间,每月重复您的通知。有关进一步的查询,请参阅


对于一周,重复间隔为NSCalendarUnitWeekOfYear

我尝试了每日通知localNotificationDaily.repeatInterval=NSCalendarUnitDay;并在今天的模拟器上进行了测试,将日期更改为7月12日。它不起作用:(应该是NSCalendarUnitMonth,因为您希望每月重复一次。是的,但我也希望是daily,这也不起作用。!!您的日期创建正确吗?请检查我的代码。我将其编辑为daily。它适用于“今天”但是,如果我将日期更改为7月12日,即明天,它将不会在模拟器中显示通知。我尝试使用daily notification localNotificationDaily.repeatInterval=NSCalendarUnitDay;并在模拟器上使用today和将日期更改为7月12日进行测试。它不起作用:(应该是NSCalendarUnitMonth,你想每月重复一次。是的,但我也想每天重复,但这也不起作用。!!你的日期创建正确吗?请检查我的代码。我将其编辑为每天。它适用于“今天”,但如果我将日期更改为7月12日,即明天,它不会在模拟器中显示通知。