Search code examples
iosobjective-cnsdateuilocalnotification

How to take next coming date as per the time to run same NSLocalNotification multiple time in iOS SDK?


I am working on an alarm app where I will get time from server with comma separated values say (10:00, 21:00). The notification should pop up on daily basis for the time set.

Now I want to make the app run the same notification for multiple times. How to take the next coming date and time from the NSDate so as to facilitate the alarm system. Say if I open the app at 7:00, I will have to run 2 notifications (10:00 & 21:00) but if I open the app at 13:00, I will have to run only 1 notification i.e. at 21:00.

Also the next day, it should run twice i.e. at (10:00 & 21:00). I am doing this:

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

- (void)generateLocalNotificationDaily
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    NSString *strTime = [Helper getPREF:PREF_AlARM_DAILY_TIME];
    NSArray *arrtime = [strTime componentsSeparatedByString:@":"];
    int hh = (int)[[arrtime objectAtIndex:0]integerValue];
    int mm  = (int)[[arrtime objectAtIndex:1]integerValue];

    NSDate *now = [NSDate date];
    NSTimeInterval secondsPerDay = 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 *todaySpecificTime = [calendar dateFromComponents:components];

    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = todaySpecificTime;
    localNotification.alertBody = @"Your notification!";
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
}

I am able to run the notification once but not able to run the same multiple times.


Solution

  • You can set the repeatInterval to whatever repeat duration you desire.

    UILocalNotification *localNot = [[UILocalNotification alloc] init];
    localNot.repeatInterval = //Calendar unit for which you need to repeat;
    

    repeatInterval can have the following values

        NSCalendarUnitEra                = kCFCalendarUnitEra,
        NSCalendarUnitYear               = kCFCalendarUnitYear,
        NSCalendarUnitMonth              = kCFCalendarUnitMonth,
        NSCalendarUnitDay                = kCFCalendarUnitDay,
        NSCalendarUnitHour               = kCFCalendarUnitHour,
        NSCalendarUnitMinute             = kCFCalendarUnitMinute,
        NSCalendarUnitSecond             = kCFCalendarUnitSecond,
        NSCalendarUnitWeekday            = kCFCalendarUnitWeekday,
        NSCalendarUnitWeekdayOrdinal     = kCFCalendarUnitWeekdayOrdinal,
    

    You cannot choose for the UILocalnotification to repeat at custom intervals.You can only choose either of the NSCalendarUnit intervals.

    Also, whenever the App is launched, you can fetch all the UILocalNotifications which you have scheduled and make whatever edits what want to make on them.

            UIApplication *app = [UIApplication sharedApplication];
            for (UILocalNotification *localNotification in [app scheduledLocalNotifications])
            {
    //Do whatever you need to do with them (cancel them, tweak times etc.)
            }