APNs BadDeviceToken: Causes and Fixes

Résumé

Pixel shares a guide to diagnosing APNs HTTP 400 BadDeviceToken errors, with special focus on matching device tokens to sandbox or production endpoints. It also covers malformed token strings and sending FCM tokens to APNs, helping developers troubleshoot failed iOS push notifications.

DEV Community

APNs BadDeviceToken (400): Causes and Fix, Including Sandbox vs Production

Your iOS push fails with 400 and "reason": "BadDeviceToken". Before you delete that token, check one thing: is it going to the right APNs environment? Most of the time it's a sandbox token sent to production, or the other way round. What the error looks like APNs replies with HTTP 400 and a JSON body with the reason: HTTP/2 400
apns-id: 5E3F9C1A-7B2D-4C8E-9A61-2F0B3D4E5A6C

{
"reason": "BadDeviceToken"
}
Apple's description is simply that the device token is invalid. The response doesn't tell you why, so check the likely causes in order. Cause 1: sandbox and production are mixed up APNs has two environments, and a device token only works in the one it came from: # Development builds (run from Xcode with a development profile)
https://api.sandbox.push.apple.com/3/device/

TestFlight, Ad Hoc and App Store builds

https://api.push.apple.com/3/device/
A build run from Xcode with a development provisioning profile has aps-environment set to development and gets sandbox tokens. So does the iOS Simulator. TestFlight, Ad Hoc and App Store builds are signed for distribution and get production tokens. A typical story: push works perfectly while you test from Xcode, then every TestFlight tester gets BadDeviceToken, because the server still sends to the sandbox host. Or the reverse, after someone switches the server to production and keeps testing from Xcode. Fix: store the environment with each token when the app uploads it (for example a sandbox flag set from your build configuration), and send each token to its matching host. Other causes The token string was changed. APNs device tokens arrive as binary data. Convert them to a hexadecimal string once and store it exactly. Using description on the token, adding spaces or angle brackets, or cutting the string short all produce an invalid token. You sent an FCM token to APNs. If your app uses Firebase, the token from Messaging.messaging().fcmToken is an FCM token. It goes to FCM, not to APNs directly. The APNs token is the one from didRegisterForRemoteNotificationsWithDeviceToken. The token belongs to another app. Tokens are tied to the app that requested them. If you have several apps or bundle IDs, check that you send each token with the right one. This often shows up as DeviceTokenNotForTopic instead. A quick way to find the cause Get a fresh token from a test device and log it on the device. Compare it with what your server stored, character by character. Note how that build was installed: from Xcode, or through TestFlight or the App Store. Send to the matching host. If it works, the problem was the environment. If it still fails on the right host with an exact token, check the bundle ID and your APNs key. How it differs from other APNs errors Only delete a token when the error says the token itself is dead: Reason HTTP What it means What to do BadDeviceToken 400 The token is invalid for this request, often the wrong environment Check the environment and token string first, then delete Unregistered 410 The token is no longer active for this topic Delete the token DeviceTokenNotForTopic 400 The token doesn't belong to the apns-topic you sent Set apns-topic to the app's bundle ID ExpiredProviderToken, InvalidProviderToken 403 Your signing JWT is too old or can't be verified Fix the key ID, team ID or signing. Don't delete device tokens TooManyProviderTokenUpdates 429 You create a new JWT too often Reuse one JWT. Apple asks you to refresh it no more than every 20 minutes and at least every 60 TooManyRequests 429 Too many pushes to the same device in a short time Slow down and retry later InternalServerError, ServiceUnavailable 500, 503 A temporary problem at APNs Retry with backoff In code, the branching can look like this: // reason comes from the JSON body of a non-200 APNs response
switch (reason) {
case "Unregistered": // 410: the token is no longer active
await removeToken(token);
break;
case "BadDeviceToken": // 400...
APNs BadDeviceToken: Causes and Fixes
Powered by Readybot.io

Les dernières actus de Pixel

Incident Response: Look Beyond the Red Alert

Pixel’s DEV Community announcement explores how teams can fixate on one failing service while overlooking the real cause of an outage, such as a full …

Sach: AI Fact-Checks WhatsApp Forwards in India

Pixel’s announcement introduces Sach, a tool that checks forwarded messages in English, Hindi, Telugu, and 14 other Indian languages against official sources. It gives cited …

Java 25 Removes SecurityManager: What to Fix

Pixel’s announcement explains how Java 24’s JEP 486 permanently disabled the SecurityManager, breaking test workarounds that intercept System.exit. It recommends returning exit codes or throwing …