0

我想做点什么。我的通知设置为 11 和 12 小时。我想取消订阅这些设置的通知仅在 11 小时内。我该怎么做?

我的代码;

NSCalendar *greg = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents * component = [greg components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
        [component setYear:2013];
        [component setMonth:m];
        [component setDay:d];
        [component setHour:x];
        [component setMinute:y];

        UIDatePicker *dd = [[UIDatePicker alloc]init];
        [dd setDate:[greg dateFromComponents:component]];

        UILocalNotification * bildirim = [[UILocalNotification alloc]init];
        [bildirim setAlertBody:newtask.name];
        [bildirim setFireDate:dd.date];
        bildirim.soundName = UILocalNotificationDefaultSoundName;
        bildirim.alertAction = NSLocalizedString(@"Test", nil);
        //[bildirim setFireDate:[NSDate dateWithTimeIntervalSinceNow:0]];
        [bildirim setTimeZone:[NSTimeZone defaultTimeZone]];

        [[UIApplication sharedApplication] scheduleLocalNotification:bildirim];

我找到了一个代码。不幸的是,此代码正在删除所有通知。

[[UIApplication sharedApplication] cancelAllLocalNotifications];

对不起,我的英语不好。

4

1 回答 1

0

创建时有两件事local notification,使用一些唯一标识创建它,以便您可以像下面这样标识

UILocalNotification * notification = [[UILocalNotification alloc]init];
notification.userInfo=[NSDictionary dictionaryWithObject:@"UNIQUE_ID" forKey:@"notifcation_id"];

然后要取消特定的,您需要以下步骤

for(UILocalNotification *notificaiton in [[UIApplication sharedApplication] scheduledLocalNotifications]){

    if([[notificaiton.userInfo objectForKey:@"notifcation_id"] isEqualToString:UNIQUE_ID]){
        [[UIApplication sharedApplication] cancelLocalNotification:notificaiton];
        break;
    }
}

一切顺利..

于 2013-04-20T18:24:49.983 回答