Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Created January 18, 2015 16:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kristopherjohnson/06bab43acbd9cebe5fd7 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/06bab43acbd9cebe5fd7 to your computer and use it in GitHub Desktop.
Swift snippets for local and remote notifications
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
registerNotificationTypes()
return true
}
func registerNotificationTypes() {
let types: UIUserNotificationType = .Badge | .Sound | .Alert;
let one = UIMutableUserNotificationAction()
one.identifier = "one"
one.title = "One"
one.activationMode = .Foreground
one.destructive = false
one.authenticationRequired = false
let two = UIMutableUserNotificationAction()
two.identifier = "two"
two.title = "Two"
two.activationMode = .Foreground
two.destructive = false
two.authenticationRequired = false
let three = UIMutableUserNotificationAction()
three.identifier = "three"
three.title = "Three"
three.activationMode = .Foreground
three.destructive = false
three.authenticationRequired = false
let category = UIMutableUserNotificationCategory()
category.identifier = "SAMPLE_CATEGORY"
category.setActions([one, two, three], forContext: .Default)
category.setActions([two, three], forContext: .Minimal)
let categories = NSSet(object: category)
let settings = UIUserNotificationSettings(forTypes: types, categories: categories)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
}
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
println("Received local notification")
}
func application(application: UIApplication,
handleActionWithIdentifier identifier: String?,
forLocalNotification notification: UILocalNotification,
completionHandler: () -> Void)
{
println("Handle action \(identifier)")
completionHandler()
}
func scheduleLocalNotification() {
let notification = UILocalNotification()
notification.fireDate = NSDate().dateByAddingTimeInterval(10)
notification.alertBody = "This is a notification alert"
notification.alertAction = "View Notification"
notification.soundName = UILocalNotificationDefaultSoundName
notification.applicationIconBadgeNumber = 1
notification.category = "SAMPLE_CATEGORY"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment