Skip to content

Instantly share code, notes, and snippets.

@youvalv
Last active April 5, 2017 14:25
Show Gist options
  • Save youvalv/3392360ce8aaddefa652af55b79b7527 to your computer and use it in GitHub Desktop.
Save youvalv/3392360ce8aaddefa652af55b79b7527 to your computer and use it in GitHub Desktop.
Register for remote notifications
private func registerForRemoteNotifications () {
if #available(iOS 10.0, *) {
let userNotificationType: UNAuthorizationOptions = [UNAuthorizationOptions.alert, UNAuthorizationOptions.badge, UNAuthorizationOptions.sound]
UNUserNotificationCenter.current().setNotificationCategories([self.getNewCategoryByIdentifier(identifier: kMorningEventIdentifier),
self.getNewCategoryByIdentifier(identifier: kEveningEventIdentifier),
self.getNewCategoryByIdentifier(identifier: kTakePillboxEventIdentifier)])
UNUserNotificationCenter.current().requestAuthorization(options: userNotificationType, completionHandler: { (success:Bool, error:Error?) in
})
} else {
let userNotificationTypes: UIUserNotificationType = [UIUserNotificationType.alert, UIUserNotificationType.badge, UIUserNotificationType.sound]
let userNotificationSettings: UIUserNotificationSettings = UIUserNotificationSettings(types: userNotificationTypes,
categories: [self.getCategoryByIdentifier(identifier: kMorningEventIdentifier),
self.getCategoryByIdentifier(identifier: kEveningEventIdentifier),
self.getCategoryByIdentifier(identifier: kTakePillboxEventIdentifier)])
UIApplication.shared.registerUserNotificationSettings(userNotificationSettings)
UIApplication.shared.registerForRemoteNotifications()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment