如何仅取消通知?

时间:2013-04-20 17:59:05

标签: iphone ios xcode localnotification

我想做点什么。我的通知设置为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];

抱歉我的英语不好。

1 个答案:

答案 0 :(得分: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;
    }
}

一切顺利......