it-swarm.com.de

Holen Sie sich das Push-Benachrichtigungstoken - iOS 10, Swift 3

Wie erhalte ich das Gerätetoken vom neuen xCode 8, Swift 3 in iOS 10?

Hier der Code zur Registrierung der Benachrichtigung:

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

Um das Token abzurufen, erhalte ich die Fehlermeldung "Der Initialisierer für den Typ 'UnsafePointer' kann nicht mit einer Argumentliste des Typs '(UnsafeRawPointer)' aufgerufen werden" ":

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

Kann jemand helfen, das Problem zu lösen?

Vielen Dank

19
user831098

Diese Methode löst möglicherweise Ihr Problem in iOS 10 und höher:

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)
}
34
chirag shah
var pushToken = String(format: "%@", deviceToken as CVarArg)
pushToken = pushToken.trimmingCharacters(in: CharacterSet(charactersIn: "<>"))
pushToken = pushToken.replacingOccurrences(of: " ", with: "")
11
Emre

Swift 3-Beispiel aus 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)
}
5
Etienne Martin

Angesichts des gleichen Problems half mir dies nur:

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

Working Code zum Abrufen von Push-Benachrichtigungstoken - iOS 11 oder höher, Swift 4

Benutzerberechtigung anfordern

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        
}

Geräte-Token abrufen

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
    print(token) 
}

Im Fehlerfall

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {

    print("i am not available in simulator \(error)")
}
1
Nikunj Kumbhani

Das folgende Snnipet arbeitet mit der Lösung von Eric Aya: 

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

Danke für alle Hilfe :)

1
user831098

Eine Linie: 

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

Aus diesem Tutorial.

0
ScottyBlades

Es könnte klappen: 

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

Vielen Dank!

0
user6880304

Code aus der Mixpanel-Bibliothek

    let tokenChars = (deviceToken as NSData).bytes.assumingMemoryBound(to: CChar.self)
    var tokenString = ""
    for i in 0..<deviceToken.count {
        tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
    }
0
comonitos