18

I am making an application that sets a local notification.

Thankfully I was able to set the local notification but I don't know how to delete the notification which is set by my application.

The XCode does provide functionality of delete with removeAllNotifications but you cannot remove specific notifications set by the application.

Thanks a lot.

Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
Aditya Kaushik
  • 261
  • 2
  • 3
  • 8

5 Answers5

71

You asked this question twice, so I'm answering on both questions in the hopes of it reaching you:

Cancel all local notifications with this code:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

Cancel one local notification with this line of code:

[[UIApplication sharedApplication] cancelLocalNotification:theNotification];

where theNotification is a UILocalNotification object, so in order to cancel a specific notification you need to hold on to it's UILocalNotification.


You can find more stuff in apple's documentation.

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
winsmith
  • 20,791
  • 9
  • 39
  • 49
  • Hi Thomas, where would you save or get that notification object? – Rafael Sanches Apr 28 '12 at 05:21
  • If you don't want to cancel all notifications, you'll have to hold the NSNotification you might want to cancel in memory somewhere. Maybe a property of an appropriate class that holds an array of NSNotifications. – winsmith Apr 30 '12 at 15:03
  • thomas, I have found the answer by serializing the UILocalNotification object – Rafael Sanches Apr 30 '12 at 17:35
12
[[UIApplication sharedApplication] cancelLocalNotification:notification]
Paulo Casaretto
  • 967
  • 10
  • 33
2

helloou, look, in swift you can create a local notification :

var notif = UILocalNotification()
        notif.timeZone = NSTimeZone.defaultTimeZone()

        let morningOfChristmasComponents = NSDateComponents()
        morningOfChristmasComponents.year = 2016
        morningOfChristmasComponents.month = 03
        morningOfChristmasComponents.day = 30
        morningOfChristmasComponents.hour = 15
        morningOfChristmasComponents.minute = 59
        morningOfChristmasComponents.second = 0

        let morningOfChristmas = NSCalendar.currentCalendar().dateFromComponents(morningOfChristmasComponents)!

        let formatter = NSDateFormatter()
        formatter.dateStyle = NSDateFormatterStyle.LongStyle
        formatter.timeStyle = .MediumStyle

        let dateString = formatter.stringFromDate(morningOfChristmas)

        notif.fireDate = morningOfChristmas
        notif.alertBody = "alarma wolf"
        notif.userInfo = ["identificador": "wolf"]
        UIApplication.sharedApplication().scheduleLocalNotification(notif)
        print("alarma fijada para \(dateString)")

loo the userInfo is a indeitifer to you local notification, now, if you want delete a specific local notificaction please try:

var uidtodelete = "wolf"
        var app:UIApplication = UIApplication.sharedApplication()
        for oneEvent in app.scheduledLocalNotifications! {
            var notification = oneEvent as UILocalNotification
            let userInfoCurrent = notification.userInfo! as! [String:AnyObject]
            let uid = userInfoCurrent["identificador"]! as! String
            if uid == uidtodelete {
                //Cancelling local notification
                app.cancelLocalNotification(notification)
                break;
            }
        }

look the method, above, userInfoCurrent is the identificator of your's local notification, and uitodelete is a string that contain the specific key of the locla notification that you want delete...

aaaa... if you want to delete all local notification you can use

UIApplication.sharedApplication().cancelAllLocalNotifications()

oki

I hope you serve yourself or someone else this information..

good bye, andforgive my bad English

1

You can cancel notification by following function:[[UIApplication sharedApplication]cancelNotification:object Of your UILocalNotification]

apoorv shah
  • 171
  • 3
  • 12
0

If you are using User Notifications framework, available since iOS 10.0, given the identifier you provided is foo:

UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: "foo")
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: "foo")

Or in Objective-C :

[[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[@"foo"]];
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[@"foo"]];
Axel Guilmin
  • 11,454
  • 9
  • 54
  • 64