Skip to content

Instantly share code, notes, and snippets.

@flaviodsilverio
Last active October 17, 2016 13:01
Show Gist options
  • Save flaviodsilverio/cb8f92ea879c92c5357f7b6079db5e8d to your computer and use it in GitHub Desktop.
Save flaviodsilverio/cb8f92ea879c92c5357f7b6079db5e8d to your computer and use it in GitHub Desktop.
func registerForPushNotifications(application: UIApplication) { // just call this on idFinishLaunchingWith options
let notificationSettings = UIUserNotificationSettings(
types: [.badge, .sound, .alert], categories: nil)
application.registerUserNotificationSettings(notificationSettings)
}
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
if notificationSettings.types != .none {
application.registerForRemoteNotifications()
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.hexString()
print(token)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to Register for remote notifications")
}
extension Data {
func hexString() -> String {
return self.reduce("") { string, byte in
string + String(format: "%02X", byte)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment