Cancelling a specific UILocalNotification

I have this code for local notification, and I have a scheduleNotification and clearNotification using my own method. These are the codes:

- (void)clearNotification {
   [[UIApplication sharedApplication] cancelAllLocalNotifications];

- (void)scheduleNotification {
   [reminderText resignFirstResponder];
   [[UIApplication sharedApplication] cancelAllLocalNotifications];

   Class cls = NSClassFromString(@"UILocalNotification");
   if (cls != nil) {
      UILocalNotification *notif = [[cls alloc] init];
      notif.fireDate = [[datePicker date] dateByAddingTimeInterval:-30];
      notif.timeZone = [NSTimeZone defaultTimeZone];

      notif.alertBody = @"Evaluation Planner";
      notif.alertAction = @"Details";
      notif.soundName = UILocalNotificationDefaultSoundName;
      notif.applicationIconBadgeNumber = 1;

     NSDictionary *userDict = [NSDictionary dictionaryWithObject:reminderText.text forKey:kRemindMeNotificationDataKey];
     notif.userInfo = userDict;
     [[UIApplication sharedApplication] scheduleLocalNotification:notif];
     [notif release];

These codes works well, but now I want to know how do I know which notification object will it delete. I would like to create an ID for a notification, meaning, one ID is equivalent to one notification. But I don’t know at which part I should do that. Plus I need to find a way to include all this to be in a plist.

  • Last In-First Out Stack with GCD?
  • Displaying inline images on iPhone, iPad
  • FBSDK Login Error Code: 308 in Objective-C
  • UIScrollView blocks run loop?
  • In iOS, what's the difference between autoresizing, AutoLayout, and constraints?
  • What is a graphic context? (iOS)
  • Hope somebody can help me. Thanks.

    3 Solutions Collect From Internet About “Cancelling a specific UILocalNotification”

    NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
    for (UILocalNotification *not in notifications) {
        NSString *dateString=[not.userInfo valueForKey:@"EndDate"];
        if([dateString isEqualToString:@"CompareString"])
            [[UIApplication sharedApplication] cancelLocalNotification:not];
    1. Give user info whenever you create local notification (this is a key-value pair).
    2. Iterate through notifications (it contains All Local Notifications) and compare value for the known key. In the above example I am using EndDate as the key and CompareString as the value.

    Its Working Fine With Me.


       // UILocalNotification *cancelThisNotification = nil;
      //  BOOL hasNotification = NO;
        for (int j =0;j<[[[UIApplication sharedApplication]scheduledLocalNotifications]count]; j++)
            UILocalNotification *someNotification = [[[UIApplication sharedApplication]scheduledLocalNotifications]objectAtIndex:j];
            if([[someNotification.userInfo objectForKey:@"drdid"] isEqualToString:notificationID])
                NSLog(@"id,notificationID(App) %@ %@ ",[someNotification.userInfo objectForKey:@"drdid"],notificationID);
                NSLog(@"canceled notifications %@",someNotification);
               [[UIApplication sharedApplication] cancelLocalNotification:someNotification];

    I would suggest using the userInfo property on UILocalNotification, as others have mentioned. A simpler implementation that the accepted answer would be:

    for(UILocalNotification* notification in [[UIApplication sharedApplication]scheduledLocalNotifications])
           if([[notification.userInfo objectForKey:@"notification_identifier"] isEqualToString:@"notification_001"])
                [[UIApplication sharedApplication] cancelLocalNotification:notification];

    A for loop like this is much simpler. I’m not sure if it’s more or less optimal, but it’s certainly easier to read, and I assume you only have a few notifications to loop through anyway.