2011-08-25 3 views
2

이 코드는 로컬 알림을위한 것이며, 나는 자신의 메서드를 사용하여 scheduleNotification과 clearNotification을 가지고 있습니다.특정 UILocalNotification 취소

- (void)clearNotification { 
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 
} 

- (void)scheduleNotification { 
    [reminderText resignFirstResponder]; 
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

    Class cls = NSClassFromString(@"UILocalNotification"); 
    if (cls != nil) { 
     UILocalNotification *notif = [[cls alloc] init]; 
     notif.fireDate = [[datePicker date] dateByAddingTimeInterval:-30]; 
     notif.timeZone = [NSTimeZone defaultTimeZone]; 

     notif.alertBody = @"Evaluation Planner"; 
     notif.alertAction = @"Details"; 
     notif.soundName = UILocalNotificationDefaultSoundName; 
     notif.applicationIconBadgeNumber = 1; 

    NSDictionary *userDict = [NSDictionary dictionaryWithObject:reminderText.text forKey:kRemindMeNotificationDataKey]; 
    notif.userInfo = userDict; 
    [[UIApplication sharedApplication] scheduleLocalNotification:notif]; 
    [notif release]; 
    } 
} 

이 코드는 잘 작동하지만 지금 나는 그것을 삭제되는 통지 객체 아는 방법을 알고 싶어요 : 다음은 코드입니다. 알림 용 ID를 만들고 싶습니다. 즉, 하나의 ID는 하나의 알림과 같습니다. 그러나 나는 어느 부분에서 그렇게해야하는지 모른다. 게다가 나는 이것을 plist에 포함시키는 방법을 찾아야한다.

누군가가 나를 도울 수 있기를 바랍니다. 감사.

답변

11
NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; 
for (UILocalNotification *not in notifications) { 
    NSString *dateString=[not.userInfo valueForKey:@"EndDate"]; 
    if([dateString isEqualToString:@"CompareString"]) 
    { 
     [[UIApplication sharedApplication] cancelLocalNotification:not]; 
    } 
} 
  1. 로컬 알림 (이 키 - 값 쌍입니다) 만들 때마다 사용자 정보를 지정합니다.
  2. 알림을 반복 (모든 로컬 알림 포함)하고 알려진 키의 값을 비교합니다. 위의 예제에서 EndDate를 키로 사용하고 CompareString을 값으로 사용하고 있습니다.

저와 잘 작동합니다.

건배 ..

+0

변수 이름을 "not"로 지정하면 오류가 발생합니다. – OthmanT

2
(void)cancelLocalNotification:(NSString*)notificationID 
{ 

    // UILocalNotification *cancelThisNotification = nil; 
    // BOOL hasNotification = NO; 

    for (int j =0;j<[[[UIApplication sharedApplication]scheduledLocalNotifications]count]; j++) 
    { 
     UILocalNotification *someNotification = [[[UIApplication sharedApplication]scheduledLocalNotifications]objectAtIndex:j]; 
     if([[someNotification.userInfo objectForKey:@"drdid"] isEqualToString:notificationID]) 
     { 
      NSLog(@"id,notificationID(App) %@ %@ ",[someNotification.userInfo objectForKey:@"drdid"],notificationID); 
      NSLog(@"canceled notifications %@",someNotification); 
      [[UIApplication sharedApplication] cancelLocalNotification:someNotification]; 
     } 

    } 
} 
+0

userInfo는 내가 찾던 고맙습니다! – Stephanie

0

다른 언급이 나는 UILocalNotification에 사용자 정보 속성을 사용하는 것이 좋습니다 것입니다. 허용되는 대답이 다음과 같이 간단한 구현입니다.

for(UILocalNotification* notification in [[UIApplication sharedApplication]scheduledLocalNotifications]) 
{ 
     if([[notification.userInfo objectForKey:@"notification_identifier"] isEqualToString:@"notification_001"]) 
     { 
      [[UIApplication sharedApplication] cancelLocalNotification:notification]; 
     } 
} 

for 루프는 매우 간단합니다. 나는 그것이 다소 이상적인지 잘 모르겠지만, 확실히 읽기가 쉽다. 그리고 나는 어쨌든 반복 할 수있는 몇 가지 알림만을 가지고 있다고 가정한다.