首页 文章

20天内UILocalNotification repeatInterval

提问于
浏览
1

我想用 custom interval repeat 创建本地通知(例如每20天) . 我知道我们有NSDayCalendarUnit,kCFCalendarUnitMonth ...但我希望在20天设置重复间隔 . 我不想每天都创建通知 .

我真正的需要是连续21天重复通知,然后在7天后再发布,然后是新的21天通知和7天没有......等等 . 我应该安排所有这些天,即使申请是无效 .

要做到这一点,我决定创建21个通知,因为fireInterval = 28days的火灾日期(这是问题)

2 回答

  • 2

    试试这个,您可以通过选择 setDaysetMonth ,....来更改intervall:

    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:3];
    
    NSDate *date3Days = [calendar dateByAddingComponents:components 
                                                   toDate:[NSDate date] 
                                                  options:0];
    
    UIApplication* app = [UIApplication sharedApplication];
    NSArray*    oldNotifications = [app scheduledLocalNotifications];
    if ( oldNotifications ) {
        [app cancelAllLocalNotifications];
        app.applicationIconBadgeNumber = 0;
    }
    
    UILocalNotification* notifyAlarm = [[UILocalNotification alloc] init];
    if (notifyAlarm) {
        notifyAlarm.fireDate = date3Days;
        notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
        notifyAlarm.alertBody = NSLocalizedString( @"Push message", @"");
        notifyAlarm.soundName = @"sound.wav";
        [app scheduleLocalNotification:notifyAlarm];
    }
    

    如果要设置 UILocalNotifications 应该出现的特定时间,您可以创建上述解决方案的方法并循环显示您希望显示通知的日期的数组:

    NSArray *arrayNumbers = @[@5, @7, @14, @21, @30, @60, @90, @120];
    NSDictionary *dictNotifications = 
    [[NSUserDefaults standardUserDefaults]    objectForKey:kUserDefAppStarts];
    
    for ( NSNumber *bla in arrayNumbers ){
    
        NSString *strKey = [NSString stringWithFormat:@"%@%@", kUserDefAppStarts, bla];
        NSDictionary *dict = dictNotifications[strKey];
        NSString *strMessageQuestion = dict[kKeyMessage];
    
        [self createNotificationWithNumberOfDays:[bla integerValue]
                                      andMessage:strMessageQuestion
                                        userInfo:dict];
    }
    

    这是你必须打电话的方法

    + (void)createNotificationWithNumberOfDays:(int)days 
                                    andMessage:(NSString *)message 
                                      userInfo:(NSDictionary *)dict{
    
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:days];
    
    NSDate *dateAlert = [gregorian dateByAddingComponents:components toDate:[NSDate date] options:0];
    
    UIApplication *app = [UIApplication sharedApplication];
    UILocalNotification *notifyAlarm = [[UILocalNotification alloc] init];
    
    if( notifyAlarm ){
        [notifyAlarm setFireDate:dateAlert];
        [notifyAlarm setTimeZone:[NSTimeZone defaultTimeZone]];
        [notifyAlarm setSoundName:@"soundname"];
        [notifyAlarm setAlertBody:message];
        [notifyAlarm setUserInfo:dict];
        [app scheduleLocalNotification:notifyAlarm];
    }
    

    }

  • 0

    如果您希望将来从当前时间开始安排UILocalNotification 20天,您可以执行以下操作:

    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComp = [[NSDateComponents alloc] init];
    int desiredAmountOfMonths = 4;
    for (int month = 0; month < desiredAmountOfMonths; month++)
    {
        dateComp.month = month;
        dateComp.day = 20;
        NSDate *fireDate = [currentCalendar dateByAddingComponents:dateComp toDate:[NSDate date] options:NSCalendarMatchNextTimePreservingSmallerUnits];
    
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        notification.fireDate = fireDate;
        notification.timeZone = [NSTimeZone defaultTimeZone];
    }
    

    您需要修改UILocalNotification以获取消息,声音等,然后在完成自定义后触发通知 .

相关问题