Prompt
How do I clean up invalid push notification tokens?
Latest observation
Nov 3, 2026
OpenAI APIWeb search: off
To clean up invalid push notification tokens, the usual pattern is:
-
Keep a token registry in your database
- Store each device token with metadata like:
- user ID
- platform (iOS/Android/Web)
- last seen / last updated
- app version
- enabled/disabled status
- Store each device token with metadata like:
-
Detect invalid tokens when sending
- When your push provider returns an error for a token, mark it as invalid and remove or deactivate it.
- Common provider signals:
- APNs:
410 Unregistered - FCM:
NotRegistered,InvalidRegistration,MismatchSenderId - Web Push:
404 Not Found,410 Gone
- APNs:
-
Delete or disable the token immediately
- If the token is permanently invalid, remove it from your send list.
- If you want auditability, mark it as:
status = invalidinvalidated_at = now()invalid_reason = ...
-
Periodically prune stale tokens
- Some tokens become stale without explicit failure.
- Run a scheduled job to remove tokens that haven’t been seen in a long time, e.g. 90–180 days.
-
Deduplicate tokens
- A device may re-register and create duplicate entries.
- Enforce uniqueness on token value, or on
(user_id, platform, token)depending on your model.
-
Handle token refresh from the client
- Mobile SDKs often rotate tokens.
- Make sure the app sends the new token to your backend whenever it changes.
-
Log send failures carefully
- Track per-token send results so you can identify patterns.
- Avoid retrying permanently invalid tokens.
Example cleanup flow
- Send notification to 1,000 tokens
- 50 fail with permanent “invalid” responses
- Mark those 50 as invalid in the database
- Exclude them from future sends
- Run a weekly cleanup job to purge older invalid tokens
Suggested database fields
iduser_idtokenplatformis_activelast_seen_atlast_success_atinvalidated_atinvalid_reason
If you want, I can also give you:
- a SQL cleanup query
- a Node/Python example
- or provider-specific handling for APNs / FCM / Web Push.