2013-04-20 56 views
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]; 

對不起,我的英語不好。

回答

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

All the best ..

+0

試圖:http://pastie.org/7675392 錯誤:使用未聲明的標識符'eventID'。 什麼是eventID? :S – Salieh 2013-04-20 18:42:43

+0

你不能讓這段代碼運行,你需要定義你自己的'UniqueID/EventID' ..並使用這個ID你可以跟蹤你想取消哪個通知.. – iphonic 2013-04-20 18:57:18