Skip to content

Instantly share code, notes, and snippets.

@ryantxr
Created March 19, 2016 22:44
Show Gist options
  • Save ryantxr/50d10c2b7aedfef11696 to your computer and use it in GitHub Desktop.
Save ryantxr/50d10c2b7aedfef11696 to your computer and use it in GitHub Desktop.
Create and cancel a local notification on iOS / Swift
class LookAtMeNotification {
func createNotification(key: String) {
let notification = UILocalNotification()
notification.fireDate = NSDate(timeIntervalSinceNow: 5)
notification.repeatInterval = NSCalendarUnit.Minute
notification.alertBody = "Please Look at me"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["ID": key]
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
func cancelNotification(key: String) {
if let localNotifications = UIApplication.sharedApplication().scheduledLocalNotifications {
for localNotification in localNotifications {
if let userInfo = localNotification.userInfo {
if userInfo["ID"] as! String == key {
UIApplication.sharedApplication().cancelLocalNotification(localNotification)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment