Search code examples
iphonecocoa-touchnotificationsios4

Cancel UILocalNotification


I have a problem with my UILocalNotification.

I am scheduling the notification with my method.

- (void) sendNewNoteLocalReminder:(NSDate *)date  alrt:(NSString *)title
{
    // some code ...
    UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 

    if (localNotif == nil)  
        return;

    localNotif.fireDate = itemDate; 
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.alertAction = NSLocalizedString(@"View Details", nil); 
    localNotif.alertBody = title;
    localNotif.soundName = UILocalNotificationDefaultSoundName; 
    localNotif.applicationIconBadgeNumber = 0;

    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:stringID forKey:@"id"]; 
    localNotif.userInfo = infoDict; 

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
    [localNotif release];
}

Its work fine and I'm correctly receiving the notification. The problem is when I should cancel the notification. Im using this method.

- (void) deleteNewNoteLocalReminder:(NSString*) reminderID noteIDe:(NSInteger)noteIDE
{
    [[UIApplication sharedApplication] cancelLocalNotification:(UILocalNotification *)notification ????  
}

Im not sure what to do here, but my questions are:

How do I know which UILocalNotification object I should delete?
Is there a way to list all notifications?

The only thing I have is the ID of which reminder I should delete.
I was thinking about to save the UILocalNotification object in my "Note" object and get it that way, and when I saving to my SQLite database serialize the object and so on ... is there a smarter way?


Solution

  • Swift 5:

    UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: arrayContainingIdentifiers)