Skip to content

Instantly share code, notes, and snippets.

@Mahabali
Last active August 1, 2018 13:15
Show Gist options
  • Save Mahabali/5ff321a8e4090d995403d8794c2d2a6b to your computer and use it in GitHub Desktop.
Save Mahabali/5ff321a8e4090d995403d8794c2d2a6b to your computer and use it in GitHub Desktop.
Apple Push notification registration
import UserNotifications
if #available(iOS 10.0, *) {
//iOS 10.0 and greater
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: { granted, error in
DispatchQueue.main.async {
if granted {
UIApplication.shared.registerForRemoteNotifications()
}
else {
//Do stuff if unsuccessful...
}
}
})
}
else {
//iOS 9
let type: UIUserNotificationType = [UIUserNotificationType.badge, UIUserNotificationType.alert, UIUserNotificationType.sound]
let setting = UIUserNotificationSettings(types: type, categories: nil)
UIApplication.shared.registerUserNotificationSettings(setting)
UIApplication.shared.registerForRemoteNotifications()
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenParts = deviceToken.map { data -> String in
return String(format: "%02.2hhx", data)
}
let token = tokenParts.joined()
print(token)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment