import UIKit | |
enum Notifications { | |
enum Categories: String { | |
case Message | |
} | |
enum Actions: String { | |
case RemindMeLater | |
case Reply | |
} | |
} | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func registerForNotifications(application: UIApplication) { | |
let snoozeAction = UIMutableUserNotificationAction() | |
snoozeAction.identifier = Notifications.Actions.RemindMeLater.rawValue | |
snoozeAction.title = "Remind Me Later" | |
snoozeAction.activationMode = .Background // Silently handle this action, .Foreground launches the app | |
let replyAction = UIMutableUserNotificationAction() | |
replyAction.identifier = Notifications.Actions.Reply.rawValue | |
replyAction.title = "Reply" | |
replyAction.activationMode = .Background | |
replyAction.behavior = .TextInput | |
let category = UIMutableUserNotificationCategory() | |
category.identifier = Notifications.Categories.Message.rawValue | |
/** | |
Contexts outlined here: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIUserNotificationCategory_class/index.html#//apple_ref/c/tdef/UIUserNotificationActionContext | |
.Default allows for up to four actions, whereas .Minimal allows for two | |
*/ | |
category.setActions([snoozeAction, replyAction], forContext: .Default) | |
category.setActions([snoozeAction, replyAction], forContext: .Minimal) | |
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: [category]) | |
application.registerUserNotificationSettings(settings) | |
} | |
// MARK: - UIApplicationDelegate | |
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
registerForNotifications(application) | |
return true | |
} | |
// ... | |
} |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
// ... | |
// MARK: - UIApplicationDelegate | |
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
if let notification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification { | |
// Handle launch from local notification | |
} | |
// ... | |
return true | |
} | |
// ... | |
} |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
// ... | |
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) { | |
defer { completionHandler() } | |
guard let identifier = identifier, let action = Notifications.Actions(rawValue: identifier) else { return } | |
switch action { | |
case .RemindMeLater: | |
print("Handle remind me later case!") | |
case .Reply: | |
if let replyText = responseInfo[UIUserNotificationActionResponseTypedTextKey] as? String { | |
// Use replyText for desired action | |
} | |
} | |
} | |
// ... | |
} |
import UIKit | |
import Timepiece | |
class NotificationScheduler { | |
// ... | |
static func scheduleSampleNotification() { | |
let notification = UILocalNotification() | |
notification.fireDate = NSDate() + 5.seconds | |
notification.timeZone = .localTimeZone() | |
notification.alertBody = "Test message!" | |
notification.category = Notifications.Categories.Message.rawValue | |
UIApplication.sharedApplication().scheduleLocalNotification(notification) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment