2013-04-18 3 views
1

이미 예약 된 알림을 취소하려고하는데 알림이 호출되고 있지만 알림을 취소하려고하면 취소되지 않습니다.UILocalNotification이 취소되지 않습니다.

예약 된 알림이 하나 뿐인 경우 NSArray 알림에 임의의 값이 포함됩니다. 누구든지 나를 도울 수 있겠 어. 특정 bookID에 대한 알림을 취소하고 싶습니다.

UPDATE : 하나의 예약 통지가있을 때

-(UILocalNotification *)scheduleNotification :(int)remedyID 
     { 
      NSString *descriptionBody; 

      NSInteger frequency; 

      UILocalNotification *notif = [[UILocalNotification alloc] init]; 

      NSLog(@"%d",remedyID); 

      descriptionBody =[[self remedyDetailsForRemedyID:remedyID] objectForKey:@"RemedyTxtDic"]; 
      frequency = [[[self remedyDetailsForRemedyID:remedyID] objectForKey:@"RemedyFrequency"]intValue]; 

      NSArray *notificationFireDates = [self fireDatesForFrequency:frequency]; 

      for (NSDate *fireDate in notificationFireDates) 
      { 
        notif.timeZone = [NSTimeZone defaultTimeZone]; 


        notif.repeatInterval = NSDayCalendarUnit; 
        notif.alertBody = [NSString stringWithString:descriptionBody]; 
        notif.alertAction = @"Show me"; 
        notif.soundName = UILocalNotificationDefaultSoundName; 

        notif.applicationIconBadgeNumber = 1; 

        notif.fireDate = fireDate; 

        NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:notif.alertBody,           @"kRemindMeNotificationDataKey", [NSNumber numberWithInt:remedyID],kRemindMeNotificationRemedyIDKey, 
               nil]; 

        notif.userInfo = userDict; 

        [[UIApplication sharedApplication] scheduleLocalNotification:notif]; 
       } 

       return notif; 

    } 



- (void)cancelNotification:(int)bookID 
{ 
    int notificationBook=0; 

     NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; 

    for (UILocalNotification *notification in notifications) 
    { 
     int notifBookId = [[notification.userInfo objectForKey:kRemindMeNotificationBookIDKey] intValue]; 


     for (int i=0; i<[bookArray count]; i++) 
     { 
      notificationBook =[[[remedyArray objectAtIndex:i] objectForKey:@"BookID"] intValue]; 
     } 

     NSLog(@"%d",[[notification.userInfo objectForKey:kRemindMeNotificationBookIDKey]intValue]); 

     NSLog(@"%d",notifBookId); 

     if (bookId == notifBookId) 
     { 
      [[UIApplication sharedApplication] cancelLocalNotification:notification]; 
     } 
    } 

} 
+0

내부를 확인 이동해야? – manujmv

답변

2

NSArray를 통지가 어떤 임의의 값을 포함한다.

해당 응용 프로그램의 이전에 예약 된 알림이 이미있을 수 있습니다. 응용 프로그램을 다시 시작

[[UIApplication sharedApplication] cancelAllLocalNotifications]; 

귀하의 주요 문제가

- (void)cancelNotification:(int)remedyId 
{ 
NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; 
NSLog(@"Cancelling... Before %d",[[[UIApplication sharedApplication]scheduledLocalNotifications]count]); 

    for (UILocalNotification *notification in notifications) 
    { 

    int notifRemedyId = [[notification.userInfo objectForKey:@"kRemindMeNotificationRemedyIDKey"]intValue]; // I change the key value 

    NSLog(@"remedyID : %d",remedyId); 
    NSLog(@"notifyId : %d",notifRemedyId); 
    if (remedyId == notifRemedyId) { 
     [[UIApplication sharedApplication] cancelLocalNotification:notification]; 
     } 
    } 

NSLog(@"Cancelling... After %d",[[[UIApplication sharedApplication]scheduledLocalNotifications]count]); 


} 

당신이 준 열쇠는 잘못의 일단 모든 알림을 취소하려고합니다. 나는 그것이 당신 문제라고 생각합니다. 나는 당신이 각 알림을 두 번 스케쥴하고있는 한 가지 더 생각했다. 이유는 모른다. 그것으로 확인하십시오.

+0

그런 다음 두 가지 알림이 발생하면 잠긴 화면에 표시되지 않습니다. – raptor

+0

모든 알림을 한 번만 취소한다는 의미입니다. 한 번 실행 한 후이 코드를 제거 할 수 있습니다 (원하지 않는 알림을 제거하기위한 것임). 그런 다음 알림 개수가 예약 한 알림 수와 동일 함을 확인하십시오. –

+0

코드를 공유하려고합니다. [Drop box] (https://www.dropbox.com/referrals/NTMwNzUzNTQ3OQ?src=) global9) –

0

당신은 당신이 경우 루프에 입력 할 수있는 루프

for (int i=0; i<[bookArray count]; i++) 
{ 
    int bookId =[[[remedyArray objectAtIndex:i] objectForKey:@"BookID"] intValue]; 

    if (bookId == notifBookId) 
    { 
     [[UIApplication sharedApplication] cancelLocalNotification:notification]; 
    } 
} 
+0

을 업데이트했습니다. 또한 루프에서 개수를 계산하고 개체를 제거 할 구제 배열에 bookArray를 사용하는 이유는 무엇입니까? – RJR

관련 문제