dev_marou
dev_marou

Reputation: 11

UILocalNotification repeatInterval on 20 days

I'd like to create local notification with custom interval repeat (every 20 days for example). I know we have NSDayCalendarUnit, kCFCalendarUnitMonth ... but I hope to set repeat interval at 20 days. I don't want to create a notification for every day.

My real need is to repeat a notification for consecutive 21 days, then don't launch it for 7 days later, then a new 21 days with notification and 7 days without ... etc. I should schedule all these days even if application is inactive.

To do this I decide to create a 21 notifications since a fire date with repeatInterval = 28days (here is the problem)

Upvotes: 1

Views: 654

Answers (2)

Alex Cio
Alex Cio

Reputation: 6052

Try this, you can change the intervall by selecting setDay, setMonth, .... :

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

If you want to set a specific time after which the UILocalNotifications should appear you can create a method of the above solution and loop over an array which indicates the days you like to show a notification:

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

And here is the method you have to call

+ (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];
}

}

Upvotes: 2

SierraMike
SierraMike

Reputation: 1569

If you wanted to schedule a UILocalNotification for 20 days in the future from the current time you would do this:

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

You will need to modify the UILocalNotification for what message, sound, etc and then fire the notification once done customizing.

Upvotes: 0

Related Questions