Created
March 19, 2016 22:44
-
-
Save ryantxr/50d10c2b7aedfef11696 to your computer and use it in GitHub Desktop.
Create and cancel a local notification on iOS / Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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