Created
January 18, 2015 16:42
-
-
Save kristopherjohnson/06bab43acbd9cebe5fd7 to your computer and use it in GitHub Desktop.
Swift snippets for local and remote notifications
This file contains hidden or 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
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