Sending an APN returns BadDeviceToken, what should DeviceToken format be?
Asked Answered
R

1

3

My backend uses Node.js with Express.js, handling APN using the npm apn package, which uses Apple's new Provider Authentication Tokens. The iOS app got permission and sent the received 32 bytes device token received to the backend, encoded as a base64 string.

But when trying to send a push notification with the device token (encoded as ascii, utf8 or base64), the APN server returns -

{"sent":[],"failed":[{"device":"....token....","status":"400","response":{"reason":"BadDeviceToken"}}]}

What should be the token format when sent to APN servers?

Recusant answered 28/10, 2016 at 11:7 Comment(0)
R
6

After hours searching the internet, camp across APNs Provider API, under APNs Notification API it was mentioned that -

For the device-token parameter, specify the hexadecimal bytes of the device token for the target device.

Which worked. Device token should be encoded as hexadecimal bytes.

// Node.js snippet
let deviceToken = Buffer.from(base64Token, 'base64').toString('hex');
Recusant answered 28/10, 2016 at 11:13 Comment(7)
same thing here @AjitejKaushik , could you figure it out? i've seen github.com/node-apn/node-apn/issues/506 but with no luckyEstus
I found out that the issue was with the environment i was setting, the certificates were of production while the token i was trying to use was of staging.Elga
Down vote is meant mainly for unrelated or really bad answer, if it didn't work for you, just keep searching. If you find an answer, post it here below, for example @AjitejKaushik should paste the last comment as a separate answer to this question. This way, each viewer votes for the answer that worked for him. No need to reduce other answers, it's not a contest.Recusant
Read the original question - "What should be the token format when sent to APN servers?", question is "what's the format", not "how to solve a bad device response". The answer above answers this directly - it's hexadecimal bytes. Sigh.Recusant
this didnt worked can someone who has solved how he got it working? i am still getting the same error @RecusantAsuncion
This is also true of Python's PyAPNS2. The token must be in hexidecimal form, not base64. This also caused us several hours of headache to figure out, as it wasn't really documented anywhere.Milne
priceless tip !Toothwort

© 2022 - 2024 — McMap. All rights reserved.