tutorial react developer apple push-notification notifications swift3 ios10

push-notification - react - push notifications ios swift 4



Obtenga el token de notificación push-iOS 10, Swift 3 (8)

¿Cómo obtener el token del dispositivo del nuevo xCode 8, Swift 3 en iOS 10?

Aquí el código para registrar la notificación:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { DispatchQueue.main.async { let settings = UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil) UIApplication.shared.registerUserNotificationSettings(settings) } return true } func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) { if notificationSettings.types != UIUserNotificationType() { application.registerForRemoteNotifications() } }

Aquí para obtener el token, pero aparece el error "No se puede invocar el inicializador para el tipo ''UnsafePointer'' con una lista de argumentos de tipo ''(UnsafeRawPointer)''":

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { let chars = UnsafePointer<CChar>((deviceToken as NSData).bytes) var token = "" for i in 0..<deviceToken.count { token += String(format: "%02.2hhx", arguments: [chars[i]]) } print("Registration succeeded!") print("Token: ", token) }

¿Alguien puede ayudar a resolverlo?

Gracias


Ante el mismo problema, esto es lo único que me ayudó:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { let token = String(format: "%@", deviceToken as CVarArg) .trimmingCharacters(in: CharacterSet(charactersIn: "<>")) .replacingOccurrences(of: " ", with: "") }


El siguiente snnipet está trabajando con la solución de Eric Aya:

let token = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})

Gracias por toda la ayuda :)


Este método puede resolver su problema en iOS 10 y superior:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { var token = "" for i in 0..<deviceToken.count { token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]]) } print(token) }


Podría funcionar:

let chars = UnsafePointer<CChar>((deviceToken as NSData).bytes.assumingMemoryBound(to:CChar.self))

¡Gracias!


Swift 3 ejemplo tomado de raywenderlich.com .

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) }


Una línea:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { let apnsDeviceToken = deviceToken.map {String(format: "%02.2hhx", $0)}.joined() }

De este tutorial.


Código de trabajo para obtener el token de notificación push - iOS 11 o superior, Swift 4

Solicitar permiso del usuario

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { let notificationTypes: UIUserNotificationType = [UIUserNotificationType.alert,UIUserNotificationType.badge, UIUserNotificationType.sound] let pushNotificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: nil) application.registerUserNotificationSettings(pushNotificationSettings) application.registerForRemoteNotifications() return true }

Obtención de token de dispositivo

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined() print(token) }

En caso de error

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { print("i am not available in simulator /(error)") }


var pushToken = String(format: "%@", deviceToken as CVarArg) pushToken = pushToken.trimmingCharacters(in: CharacterSet(charactersIn: "<>")) pushToken = pushToken.replacingOccurrences(of: " ", with: "")