2013-04-18 58 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 2013-04-18 07:17:38

回答

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 2013-04-18 07:48:16

+0

我的意思是取消所有通知只有一次。運行後,您可以刪除此代碼(這是爲了刪除不需要的通知)。然後檢查通知計數是否與您預定的通知數量相同 – 2013-04-18 07:54:18

+0

我會嘗試..分享您的代碼..將您的項目放在[Drop box](https://www.dropbox.com/referrals/NTMwNzUzNTQ3OQ?src= global9) – 2013-04-18 08:58:57

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 2013-04-18 07:40:40