22

I am scheduling a location based UILocalNotification with the click of a button . But when i try to cancel the localNotification by clicking the same button again, it doesn't cancel the notification. I am using UIApplication.sharedApplication().cancelLocalNotification(localNotification) to cancel my scheduled location based local notification. What am i doing wrong ? here is my implementation

@IBAction func setNotification(sender: UIButton!) {
    if sender.tag == 999 {
        sender.setImage(UIImage(named: "NotificationFilled")!, forState: .Normal)
        sender.tag = 0
        regionMonitor() //function where notification get scheduled

    } else {
        sender.setImage(UIImage(named: "Notification")!, forState: .Normal)
        sender.tag = 999 }

what should i enter into the else block so that the scheduled notification gets canceled. Cannot clear all notifications. here is the didEnterRegion block code where i trigger the local notification

func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {
    localNotification.regionTriggersOnce = true
    localNotification.alertBody = "Stack Overflow is great"
    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
    NSLog("Entering region")
}
sumesh
  • 2,179
  • 2
  • 21
  • 37

6 Answers6

58

You could try to remove all notifications if this is acceptable in your context. Like this:

for notification in UIApplication.sharedApplication().scheduledLocalNotifications as! [UILocalNotification] { 
  UIApplication.sharedApplication().cancelLocalNotification(notification)
}

Or as stated by Logan:

UIApplication.sharedApplication().cancelAllLocalNotifications()

Or as stated by Gerard Grundy for Swift 4:

UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
Bob Gilmore
  • 12,608
  • 13
  • 46
  • 53
Renan Kosicki
  • 2,890
  • 2
  • 29
  • 32
  • 12
    If you're going to cancel all of them regardless, could probably just use: `UIApplication.sharedApplication().cancelAllLocalNotifications()` – Logan Aug 11 '15 at 20:29
  • Its cancelling the Local Notification but does not cancelling custom sound as its playing continuously around 3 to 4 seconds. – Gautam Sareriya Dec 26 '16 at 11:56
  • 2
    **Swift4.1** update Logans and → UIApplication.shared.cancelAllLocalNotifications() – uplearned.com Apr 12 '18 at 07:52
  • **Swift 5 update** UNUserNotificationCenter.current().removeAllPendingNotificationRequests() for some reason if you paste the same text is appears as an error in Swift 5. All you have to do is delete the last bit "removeAllPendingNotificationRequests()" and retype it and it will fix the error. (must be a bug) – uplearned.com Nov 09 '19 at 07:57
  • `UIApplication.shared.cancelAllLocalNotifications()` was deprecated in iOS 10.0. We have to use user notifications framework. – Arun Reddy Apr 13 '20 at 06:01
8

You can cancel a notification using its identifier :

let center = UNUserNotificationCenter.current()
center.removeDeliveredNotifications(withIdentifiers: [String])
center.removePendingNotificationRequests(withIdentifiers: [String])
DoesData
  • 6,594
  • 3
  • 39
  • 62
glemoulant
  • 517
  • 7
  • 18
4

Solution for iOS 10+ Swift 3.1

let center = UNUserNotificationCenter.current()
center.removeAllDeliveredNotifications() // To remove all delivered notifications
center.removeAllPendingNotificationRequests()
Md. Ibrahim Hassan
  • 5,359
  • 1
  • 25
  • 45
1

You can save a unique value for key in your local notification's userinfo and cancel it by getting the localnotification using that key's Value. Try this (for Objective C):

NSArray *notifArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
for (int i=0; i<[notifArray count]; i++)
{
    UILocalNotification* notif = [notifArray objectAtIndex:i];
    NSDictionary *userInfoDict = notif.userInfo;
    NSString *uniqueKeyVal=[NSString stringWithFormat:@"%@",[userInfoDict valueForKey:@"UniqueKey"]];
    if ([uniqueKeyVal isEqualToString:keyValToDelete])
    {
        [[UIApplication sharedApplication] cancelLocalNotification:notif];
        break;
    }
}
Chengappa C D
  • 1,841
  • 1
  • 12
  • 19
1

For Swift 3.0 and iOS 10:

UNUserNotificationCenter.current().removeAllDeliveredNotifications()
-4

Hard to tell without seeing the implementation of the code.

so you have an

- IBAction methodName{

//scheduleNotification
//cancelNotification
}

Is that about right? If so add a bool so

Bool didCancel;

- IBAction methodName{

if didCancel == No{
//scheduleNotification
didCancel = YES;
}else if didCancel == YES{
//cancelNotifications
didCancel = NO;
}
mKane
  • 932
  • 13
  • 30