Prompt
How do I diagnose APNs errors from my notification backend?
Latest observation
To diagnose APNs errors from your notification backend, focus on both the HTTP response from APNs and the behavior of your token/device lifecycle. APNs errors usually fall into a few categories: authentication, payload, token validity, topic/environment mismatch, and delivery throttling/expiration.
1) Log the full APNs response
When you send a notification, capture:
- HTTP status code
- APNs error reason from the JSON body
apns-idresponse header- Request metadata:
- device token
- topic/bundle ID
- push type
- environment (
api.push.apple.comvsapi.development.push.apple.com) - expiration
- priority
- collapse ID
- timestamp
APNs commonly returns JSON like:
{
"reason": "BadDeviceToken"
}
or
{
"reason": "Unregistered"
}
The reason is the key diagnostic signal.
2) Interpret common APNs errors
Here are the most common ones and what they usually mean:
-
BadDeviceToken- Token is malformed, expired, from the wrong environment, or tied to a different app/team.
- Check that you’re sending to the correct APNs environment and using the latest token from the device.
-
Unregistered- The app was uninstalled or APNs no longer considers the token valid.
- Stop sending to this token. Remove it from your database.
-
DeviceTokenNotForTopic- The device token doesn’t match the
apns-topicyou used. - Verify bundle ID / topic, especially with app extensions or multiple targets.
- The device token doesn’t match the
-
TopicDisallowed- Your certificate/auth key isn’t allowed for that topic.
- Check Apple Developer settings and entitlements.
-
BadTopic- Missing or incorrect
apns-topic. - Ensure it exactly matches the app’s bundle identifier unless using a special topic.
- Missing or incorrect
-
PayloadTooLarge- Payload exceeds APNs size limits.
- Reduce payload size; keep custom data minimal.
-
BadPriority- Invalid
apns-priorityfor the given push type. - Common values are
5and10; background pushes typically use5.
- Invalid
-
BadPushType- Missing or incorrect
apns-push-type. - Must match the notification type (
alert,background,voip, etc.).
- Missing or incorrect
-
ExpiredProviderToken- If using JWT auth, your provider token is too old.
- Refresh your signing token regularly.
-
InvalidProviderToken- JWT is malformed, signed with wrong key, or claims are incorrect.
- Check team ID, key ID, and signature generation.
-
TooManyRequests- You’re being rate limited.
- Back off and retry with exponential delay.
-
Shutdown- APNs is temporarily unavailable.
- Retry later.
3) Verify environment and token source
A very common issue is mixing sandbox and production.
- Tokens from development builds generally go to
api.development.push.apple.com - Tokens from App Store/TestFlight/production builds go to
api.push.apple.com
If you send a sandbox token to production APNs or vice versa, you’ll often see BadDeviceToken.
Also confirm:
- the token was generated for the same app bundle ID/topic
- you’re not sending stale cached tokens
- the user has not restored to a different device/account state
4) Check headers and push type
APNs is strict about headers.
Typical required headers:
apns-topicapns-push-type- authentication header (
authorization: bearer <jwt>) or certificate-based auth
For example:
- visible notification:
apns-push-type: alert
- silent/background notification:
apns-push-type: background- usually
apns-priority: 5 - payload must include
content-available: 1
If headers don’t match the payload, APNs may reject the request.
5) Validate payload format
Make sure the payload is valid JSON and conforms to APNs rules:
- top-level
apsdictionary exists - alert fields are structured correctly
- badge is numeric
- sound is valid
- background pushes contain
content-available: 1
Example alert payload:
{
"aps": {
"alert": {
"title": "Hello",
"body": "Message text"
},
"badge": 1,
"sound": "default"
}
}
Example background payload:
{
"aps": {
"content-available": 1
}
}
6) Handle token lifecycle correctly
On the backend, maintain a token registry:
- store tokens by user/device
- update tokens whenever the app reports a new one
- delete tokens on
Unregistered - mark suspect tokens after repeated
BadDeviceToken
Also note:
- tokens can change after reinstall, restore, or sometimes OS/device changes
- don’t assume a token is permanent
7) Use apns-id for tracing
APNs returns an apns-id header on successful requests and often on failures. Log it and correlate it with:
- your request logs
- device-side logs
- Apple system logs if available
This helps you track a single push through your system.
8) Reproduce with curl or a minimal client
If your backend is complex, test the same request manually.
This helps isolate whether the problem is:
- your backend code
- token/topic/header formation
- APNs credentials
- payload content
9) Check for certificate/JWT auth issues
If using certificates:
- verify the certificate is valid and not expired
- ensure it matches the app’s APNs environment
If using JWT:
- confirm:
iss= Apple Team IDkid= Key ID- ES256 signing
- token is refreshed within Apple’s allowed window
- ensure your server clock is correct
10) Use APNs status codes as a guide
Common HTTP statuses:
- 200: accepted by APNs
- 400: bad request (payload/header/token issue)
- 403: authentication/authorization issue
- 405: wrong HTTP method
- 410: token no longer valid (
Unregistered) - 413: payload too large
- 429: too many requests
- 500/503: APNs temporary server issue
Practical debugging checklist
- Log APNs status, reason, and
apns-id - Verify production vs sandbox environment
- Verify
apns-topicmatches your bundle ID - Verify
apns-push-typematches payload - Check JWT/certificate validity
- Test token freshness and user uninstall/reinstall cases
- Retry only on transient errors like 500/503/429
- Remove tokens on
Unregistered
If you want, I can also give you:
- a table of APNs error reasons and fixes
- a Node/Python sample for logging APNs failures
- a backend troubleshooting flowchart
Brands