Search code examples
iosswiftuilocalnotification

Cancel local notification


I want to list scheduled local notifications in order to check fired date, to cancel the good one.

This is how I'm adding local notifications :

let notification = UILocalNotification()
notification.fireDate = date
notification.alertBody = message
notification.soundName = UILocalNotificationDefaultSoundName
notification.timeZone = NSTimeZone.defaultTimeZone()

This is how I'm listing all notification to find the good one :

for oneEvent in UIApplication.sharedApplication().scheduledLocalNotifications! {
    if date == notification.fireDate {
        app.cancelLocalNotification(notification)
    }
}

Problem is that I create a lot of notifications and there are always only one notification in scheduledLocalNotifications !

Notification are fired correctly but I can't retrieve them is scheduledLocalNotifications variable.


Solution

  • i am not getting your question but i have some doubt in bellow code try this:

    you must write this line after creating notification.

    UIApplication.sharedApplication().scheduleLocalNotification(notification)
    

    then

    for oneEvent in UIApplication.sharedApplication().scheduledLocalNotifications! {
        if date == oneEvent.fireDate {
             app.cancelLocalNotification(oneEvent)
        }
    }