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?

Kof
  • 23,893
  • 9
  • 56
  • 81

1 Answers1

5

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');
Kof
  • 23,893
  • 9
  • 56
  • 81
  • same thing here @AjitejKaushik , could you figure it out? i've seen https://github.com/node-apn/node-apn/issues/506 but with no lucky – mariomol Mar 24 '17 at 20:50
  • 4
    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. – Ajitej Kaushik Apr 04 '17 at 11:50
  • 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. – Kof Apr 05 '17 at 11:28
  • 1
    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. – Kof Apr 05 '17 at 11:30
  • this didnt worked can someone who has solved how he got it working? i am still getting the same error @Kof – Ammad Jun 30 '17 at 14:53
  • 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. – Derek Pankaew May 05 '21 at 16:09