Search code examples
iosswiftapple-push-notifications

Error: Device token should be a hex string on Device Token Validator


What I am ultimately trying to do is update a LiveActivity using a push notification, and so to me the very first step is being able to send a push notification from the Push Notification Console to my device.

I am currently using a simulator device, and my app is organized as follows:

  • Main target has bundle id com.nick.main
  • Widget target has bundle id com.nick.main.widget

This is how I am attempting to get my token:

do {
        let Activity = try Activity<Attributes>.request(
            attributes: Attributes2,
            contentState: initialContentState,
            pushType: .token)

        Task {
            for await pushToken in deliveryActivity.pushTokenUpdates {
                let pushTokenString = pushToken.reduce("") { $0 + String(format: "%02x", $1) }

                print("pushTokenString: \(pushTokenString)")
                
            }
        }
    } catch (let error) {
        print("Error requesting Live Activity \(error.localizedDescription)")
        
    }

enter image description here

When it runs it does print out a token from print("pushTokenString: \(pushTokenString)"), and this is what I am attempting to use on the push notification console. I have tried the Device Token Validator on the push notification console and when I put the token to check it I always get the error: Error: Device token should be a hex string. I also tried to use the send feature of the push notification console selecting the different bundle ID's and each produces this error when it is attempted:

{
  "code": 400,
  "message": "bad-request",
  "reason": "Invalid push token",
  "requestUuid": "53f450a0-1160-4be3-aa37-bbd65ba79e58"
}

I would also add that I also tried this:

let pushTokenString = pushToken.reduce("") { $0 + String(format: "%02.2hhx", $1) }

And it produces the same token as

let pushTokenString = pushToken.reduce("") { $0 + String(format: "%02x", $1) }

Here are 2 examples of the tokens that are produced: 80340949ad01bdb0db1bb77332cd7dd9721a8dc9758960d59c112d3af61af6b96b8c6cfe1bedc3cdb8cd240f57b65a6565dc7e17e0e03ee2730bd8745a874912dc284d4d4081e73a8c7a1d5feada388b495734a47f397cea198983fa1629f3db9468bb0e04c351c600c6ccbffba2a7b8e79023b0b0379641cedfe73619b1fc5b

806169d3b506558327fabb72a85fe9f3d840cfa4980dae98dd51574ab1271b8ffa251bdb87175d1b16b1445aa6a8266ac86be4d4223aaaeaa5500f94452074843985a855cd53527f79299480ca2e9b9a3fecf68c7ce472a4c618cdfe148646c46c143f40985c26ad40a435df2808a61d9026d2c8a9def5e36f1b3e36bfe8a3b2

These both have a length of 256, but I believe the length needs to be 64.

Any ideas/suggestions on what I am missing?


Solution

  • The issue ended up being that I was generating the token on an iOS simulator. When I instead tried on a physical device it generated a valid token and worked as expected.