Aditya Kaushik
Aditya Kaushik

Reputation: 261

How to delete a local notification in iPhone

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.

Upvotes: 18

Views: 27718

Answers (5)

Axel Guilmin
Axel Guilmin

Reputation: 11746

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

Upvotes: 0

winsmith
winsmith

Reputation: 21562

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.

Upvotes: 71

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

Upvotes: 2

apoorv shah
apoorv shah

Reputation: 171

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

Upvotes: 1

Paulo Casaretto
Paulo Casaretto

Reputation: 1017

[[UIApplication sharedApplication] cancelLocalNotification:notification]

Upvotes: 12

Related Questions