how to cancel a localNotification with the press of a button in swift?

You can cancel a notification using its identifier :

let center = UNUserNotificationCenter.current()
center.removeDeliveredNotifications(withIdentifiers: [String])
center.removePendingNotificationRequests(withIdentifiers: [String])

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()