user1681673
user1681673

Reputation: 368

How to cancel a UILocalNotification based on time

I'm writing an application based on UILocalNotifications. I having the user input a time on a date picker and that time will represent when the last UILocalNotification will fire. After the user hits a UIButton UILocalNotifications will fire based on a time interval until the time they selected occurs, then the app will stop.

I schedule the maximum number of UILocalNotifications before the user inputs a time. Is there a way to cancel the UILocalNotifications that are scheduled after the user's input time, based on time? Or is there a way to cancel all scheduled notifications after the inputted notification fires? The code is setting the last timer and the first timer based of the input time. Thanks for the help.

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

NSDate *pickerDate = [self.datePicker date];

NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                               fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                               fromDate:pickerDate]; 

NSDateComponents *dateComps = [[NSDateComponents alloc] init];

[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:0];

NSDate *itemDate = [calendar dateFromComponents:dateComps];


/**************Setting the final alarm*********************/

UILocalNotification *finalAlarm = [[UILocalNotification alloc] init];
if (finalAlarm) {
    finalAlarm.fireDate = itemDate;
    finalAlarm.timeZone = [NSTimeZone defaultTimeZone];
    finalAlarm.repeatInterval = 0;
    finalAlarm.soundName = UILocalNotificationDefaultSoundName;
    finalAlarm.alertBody = @"Test message...";
    [[UIApplication sharedApplication] scheduleLocalNotification:finalAlarm];
}

//Formatting original date in the date picker for readability 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH"];
NSString *formattedDateString = [dateFormatter stringFromDate:datePicker.date];

//isolate current date/time
NSDate *currentDate = [NSDate date];
//formatting the current date for readability 
[dateFormatter setDateFormat:@"HH"];
NSString *currentForamttedDate = [dateFormatter stringFromDate:currentDate];
//current hour
int currentHour = [currentForamttedDate intValue];


/**************Setting the first alarm*********************/

//current hour plus 2
int firstAlarmHour = currentHour + 2;

//creating the first alarm
NSDateComponents *firstAlarm = [[NSDateComponents alloc]init];

[firstAlarm setDay:[dateComponents day]];
[firstAlarm setMonth:[dateComponents month]];
[firstAlarm setYear:[dateComponents year]];
[firstAlarm setHour:firstAlarmHour];
[firstAlarm setMinute:[dateComponents minute]];
[firstAlarm setSecond:0];

//creating a date from the components
NSDate *firstAlarmDate = [calendar dateFromComponents:firstAlarm];

//setting the first alarm
[self scheduleNotificationForDate: firstAlarmDate];

Upvotes: 0

Views: 182

Answers (1)

AdamG
AdamG

Reputation: 3718

You can use the following code to get all the notifications and then cancel them based on your specifications.

 if ([[UIApplication sharedApplication].scheduledLocalNotifications count] > 1){

        for (int i = 0; i < [[UIApplication sharedApplication].scheduledLocalNotifications count]; i++){

            UILocalNotification * notification = [[UIApplication sharedApplication].scheduledLocalNotifications objectAtIndex:i];

            if (/* if after your fire date */){
                [[UIApplication sharedApplication] cancelLocalNotification:notification];
            }

        }
    }

Upvotes: 1

Related Questions