2012-10-19 120 views
0

我做了一個具有自定義通知的應用程序,但無論我做什麼,我都無法刪除它們。刪除通知

[[UIApplication sharedApplication] cancelLocalNotification:[idkey objectAtIndex:indexPath.row]]; 

這是我用來移除的代碼;如果我全部使用它。 idkey是正確的 值,在這種情況下它是一個字符串中的數字@"3"

這是儲蓄功能的一部分:

-(void) addNewNotificationWithKey:(NSString*)key { 

    NSLog(@"%@",key); 

    Class cls = NSClassFromString(@"UILocalNotification"); 
    if (cls != nil) { 

     UILocalNotification *notif = [[cls alloc] init]; 
     notif.fireDate = [tempFormatter dateFromString:datuminura]; 
     notif.timeZone = [NSTimeZone defaultTimeZone]; 

     notif.alertBody = description1; 
     notif.alertAction = @"Open"; 
     notif.soundName = UILocalNotificationDefaultSoundName; 

是不是?

回答

1

檢查您想要刪除的通知並使用以下code取消它。

UIApplication *app = [UIApplication sharedApplication]; 
NSArray *eventArray = [app scheduledLocalNotifications]; 
for (int i=0; i<[eventArray count]; i++) 
{ 
    UILocalNotification* oneEvent = [eventArray objectAtIndex:i]; 
    NSDictionary *userInfoCurrent = oneEvent.userInfo; 
    NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"uid"]]; 
    if ([uid isEqualToString:uidtodelete]) 
    { 
     //Cancelling local notification 
     [app cancelLocalNotification:oneEvent]; 
     break; 
    } 
} 

[編輯]

添加下面一行到你的本地通知,使[UID isEqualToString:uidtodelete]狀態工作。

NSDictionary *infoDict = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%d",[idkey objectAtIndex:indexPath.row]] forKey:@"uid"]; 
notif.userInfo = infoDict; 
+0

謝謝。我會試試看,並回復你 – WildWorld

+0

數組的sice總是4 – WildWorld

+0

所有工作正常if([uid isEqualToString:[idkey objectAtIndex:indexPath.row]])將不會安靜的工作,我不知道wy – WildWorld