Why APNs returns BadDeviceToken, and how to fix it
APNs returns 400 BadDeviceToken when it does not recognise the device token for the environment you sent it to. Most of the time the token is fine: a sandbox token from a development build went to production, or a production token from TestFlight or the App Store went to the sandbox. Otherwise the token was damaged between the app and your server. In both cases the device still exists, so fix the configuration before you delete anything.
What the response looks like
Apple answers with HTTP status 400 and a JSON body that carries only the reason:
HTTP/2 400
apns-id: 6f2e9a1c-0b4d-4c8e-9f3a-2d1b7e5c8a90
{"reason":"BadDeviceToken"}
There is no timestamp field. Apple adds one only to a 410 Unregistered, the response that says a token stopped being valid. Apple's list of APNs responses asks you to check that the token "matches the environment", and lists BadDeviceToken among the codes not to retry.
Apple's page on sending notification requests describes the order: APNs validates your authentication token first, and only then uses the device token. A BadDeviceToken therefore means your key, Team ID and Key ID were accepted. The problem is the token, or where you sent it.
Cause 1: the token belongs to the other environment
APNs has two environments, development (also called sandbox) and production, and a token is issued for one of them. The app's aps-environment entitlement decides which, and Xcode sets it from the provisioning profile the build is signed with.
| Build | aps-environment |
APNs host that accepts its tokens |
|---|---|---|
| Signed with a development profile, as when you run from Xcode | development |
api.sandbox.push.apple.com |
| Signed for distribution: TestFlight, App Store | production |
api.push.apple.com |
A token sent to the other host gets BadDeviceToken. The usual ways this happens:
- One backend for every build. Production sends everything to
api.push.apple.com, while your team runs Xcode builds that register sandbox tokens against the same backend. - A token copied by hand. You copy a token from a TestFlight build into a curl test against the sandbox. An Apple engineer diagnosed exactly this in a 2021 forum thread, and later replies in that thread report the same mistake up to 2024.
- Imported tokens without their environment. An export from another push provider may not say which build issued each token. If your import treats them all as production, every device that registered from a development build fails.
To confirm an environment mismatch, send the same notification to the other host. If it returns 200, the token is valid and the stored environment was wrong. This sends a real notification to the device:
curl -v --http2 \
-H "authorization: bearer $PROVIDER_TOKEN" \
-H "apns-topic: com.example.app" \
-H "apns-push-type: alert" \
-d '{"aps":{"alert":"Environment check"}}' \
"https://api.sandbox.push.apple.com/3/device/$DEVICE_TOKEN"
$PROVIDER_TOKEN is the JWT you sign with your .p8 key, and apns-topic is the app's bundle ID.
Cause 2: the token was damaged on the way to your server
The app receives the token as raw bytes, in a Data value. Your server needs it as a hexadecimal string. Encoding mistakes produce a string APNs cannot parse:
- Using
deviceToken.description. Code that stripped<,>and spaces from it broke when apps were built with Xcode 11 and the iOS 13 SDK: it now returns something like{length = 32, bytes = 0x...}. - Base64 instead of hex. Swift's
JSONEncoderencodesDataas base64 by default, so aDatafield in your registration request reaches the server in the wrong format. - Truncation or extra characters. A database column too short, a trailing newline, a space added when copying.
Encode it explicitly in the app:
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.map { String(format: "%02x", $0) }.joined()
// send `token` to your server
}
On the server, check that the token contains only hexadecimal characters. Do not check for a fixed length: Apple's documentation of the registration callback says device tokens vary in length. In a June 2025 forum thread, every push to production returned BadDeviceToken, and the token in question contained characters that are not hexadecimal. Apple's answer was that the token was simply invalid.
BadDeviceToken, DeviceTokenNotForTopic and Unregistered
Three APNs reasons concern the token itself.
| Reason | Status | Timestamp | What it says | What to do |
|---|---|---|---|---|
BadDeviceToken |
400 | no | APNs does not accept this token in this environment | Find the environment or encoding error |
DeviceTokenNotForTopic |
400 | no | The token does not match apns-topic, usually the app's bundle ID |
Check the topic and the credential's bundle ID |
Unregistered |
410 | yes | The token is no longer active for the topic | Stop sending to it |
Only Unregistered says the token is no longer active. Even then, compare its timestamp with the device's last registration: Apple notes that you can send to the token again if your app retrieves the same token later.
Should you delete the token?
Not on BadDeviceToken. In a November 2025 forum answer on which errors justify removing a token, an Apple engineer left BadDeviceToken to the developer's judgement, because the token can be valid in the other environment. The same answer says Unregistered can be removed the first time you see it.
One wrong environment variable on a backend turns every device of that environment into BadDeviceToken at once. Delete on that error and you lose those devices until each app registers its token again. Keep the device, mark the notification as failed, fix the configuration, and send again.
How Carillon handles it
- Each iOS device carries its APNs environment. The SDK reads
aps-environmentfrom the embedded provisioning profile at every launch. With no profile, as on TestFlight and the App Store, it reportsproduction. A rebuild with different signing corrects the environment at the next launch. See devices and environments. - Each notification goes to the host that matches the device. You send one request to the Carillon API; the sandbox and production devices in its audience are sent to their own APNs environment.
BadDeviceTokenstays on the delivery, and the device stays. The delivery is marked failed with the error code, and the dashboard shows what it means and what to check. The device is not invalidated, because the error describes a configuration, not a device that stopped existing.Unregisteredinvalidates the device only when its timestamp is later than the device's last registration.- Imports carry the environment. The CSV import accepts an
environmentcolumn for iOS tokens. Without it, a token is imported asproduction, so setsandboxfor tokens from development builds. - Credential checks use this error on purpose. When you upload an APNs key, Carillon sends a request to a device token that no device holds. Apple answers
BadDeviceTokenonly after accepting the key, which confirms the key, Team ID and Key ID without a notification reaching anyone.
The troubleshooting page lists every APNs and FCM error code with the fix in one line.