0

我想取消UILocalnotification,当我取消通知仍然通知被解雇。我想知道我是否必须调用appdelegate中的任何委托方法来取消通知。我使用的代码正在正确执行,但通知被解雇了。 我应该使用NSNotification中心有removeObserver方法取消uilocalnotification。NSNotificationCenter是否需要取消UILocalnotficiation?

UILocalnotification是否会触发来自应用程序或设备的通知。

更新 - 这就是我如何安排我的通知

-(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)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]; 
     NSLog(@"remedyID : %d",remedyId); 
     NSLog(@"notifyId : %d",notifRemedyId); 
     if (remedyId == notifRemedyId) { 
      [[UIApplication sharedApplication] cancelLocalNotification:notification]; 
      } 
     } 

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

    } 

回答

0

scheduledLocalNotifications会给你所有的计划通知的列表,并使用

- (void)cancelLocalNotification:(UILocalNotification *)notification 

或你可以使用全部取消:

[[UIApplication sharedApplication] cancelAllLocalNotifications]; 

编辑:

NSString *notifRemedyId = @"notifRemedyId"; 
UILocalNotification *localnotif=[[UILocalNotification alloc]init]; 

NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:@"kRemindMeNotificationRemedyIDKey", kRemindMeNotificationRemedyIDKey,YOUR_REMEDYID,@"notifRemedyId",nil]; 
localnotif.userInfo = userDict; 
[[UIApplication sharedApplication] scheduleLocalNotification:localnotif]; 

取消为:

for (UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) { 
    if ([[aNotif.userInfo objectForKey: kRemindMeNotificationRemedyIDKey] isEqualToString:@"kRemindMeNotificationRemedyIDKey"]) { 
     if (remedyId == [[aNotif.userInfo objectForKey: kRemindMeNotificationRemedyIDKey] intValue]) { 
      [[UIApplication sharedApplication] cancelLocalNotification:aNotif]; 
      break; 
     } 
    } 
} 

希望它可以帮助你。

+0

是否强制将通知作为参数传递,或者它可以成为补救ID ..因为我传递remedyID原因,如果remedyID与notificationID相同,那么我取消通知..但我仍然收到通知 – raptor 2013-05-06 17:13:22

+0

是的,您需要通过通知。检查我更新的答案。 – 2013-05-06 17:18:48

+0

我编辑了我的答案。检查出来.. – 2013-05-06 17:23:05

1

NSNotification center has removeObserver方法取消uilocalnotification。

NSNotificationCenter有没有与UILocalNotification有关。我很抱歉他们都在他们的名字中使用了“通知”,但这真的只是巧合。

+0

好吧谢谢澄清@matt – raptor 2013-05-06 17:32:27