Last active
August 1, 2018 13:15
-
-
Save Mahabali/5ff321a8e4090d995403d8794c2d2a6b to your computer and use it in GitHub Desktop.
Apple Push notification registration
This file contains 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
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