如何根据时间取消UILocalNotification

时间:2013-06-08 23:30:42

标签: iphone ios cocoa uilocalnotification

我正在编写一个基于UILocalNotifications的应用程序。我让用户在日期选择器上输入时间,该时间将表示最后一次UILocalNotification将触发的时间。在用户点击UIButton后,UILocalNotifications将根据他们选择的时间间隔触发,然后应用程序将停止。

我在用户输入时间之前安排了UILocalNotifications的最大数量。有没有办法取消根据时间在用户输入时间之后安排的UILocalNotifications?或者有没有办法在输入的通知触发后取消所有预定的通知?代码根据输入时间设置最后一个计时器和第一个计时器。谢谢您的帮助。

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

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码获取所有通知,然后根据您的规范取消它们。

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

        }
    }