Ios NSNotificationCenter是否需要取消UILocalNotification?

Ios NSNotificationCenter是否需要取消UILocalNotification?,ios,objective-c,uilocalnotification,nsnotificationcenter,Ios,Objective C,Uilocalnotification,Nsnotificationcenter,我想取消UILocalnotification,当我取消通知时,仍然会触发通知。我想知道我是否必须调用appdelegate中的任何委托方法来取消通知。我正在使用的代码正在正确执行。但是通知正在被触发。 我应该使用具有removeObserver方法的NSNotification center取消uilocalnotification吗 UILocalnotification是否从应用程序或设备触发通知 更新-这是我安排通知的方式 -(UILocalNotification *)schedul

我想取消UILocalnotification,当我取消通知时,仍然会触发通知。我想知道我是否必须调用appdelegate中的任何委托方法来取消通知。我正在使用的代码正在正确执行。但是通知正在被触发。 我应该使用具有removeObserver方法的NSNotification center取消uilocalnotification吗

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]);

     }
取消通知

 -(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]);

     }
将为您提供所有计划通知的列表并使用

- (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;
        }
    }
}
希望对你有帮助

具有removeObserver方法以取消uilocalnotification的NSNotification center


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

是否必须将通知作为参数传递,或者它可以是remedyID。.因为我正在传递remedyID。.因为如果remedyID与notificationID相同,那么我将取消通知。.但我仍然收到通知我正在将remedyID与notifRemedyID进行比较。.编辑中的第一个代码应该添加它吗计划通知并在我的取消通知方法中使用第二个代码?我已经添加了scheduleNotification方法