1

我想取消 UILocalnotification。我创建了一个名为 cancelNotification 的方法,我正在取消通知。但是,即使在取消后我仍然收到通知我在应用程序委托中听到我们需要取消通知的少数委托方法。谁能解释我在哪里调用取消方法..如果有人可以帮助我,那就太好了

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

     }
4

1 回答 1

1

[[UIApplication sharedApplication] cancelAllLocalNotifications];

只需将其复制到方法 didLaunchWithOptions 或 viewDidLoad 下,甚至复制到您的下一个 UILocalNotification 代码中。

我相信这就是你在代表中的意思?如果您在应用程序启动后调用此方法,它将取消您之前的所有本地通知。

于 2013-05-05T02:30:56.883 回答