NTF-13Recommended
Delete dead subscriptions and tokens at once
Delete a push subscription or device token as soon as the service says it is gone. Remove tokens the app has not refreshed for 270 days.
How to do it
Web push services answer 404 or 410 for an expired subscription. APNs answers 410 Unregistered, and FCM answers 404 UNREGISTERED. Delete the row on these answers and never retry it. Store when each token was last refreshed by the app. Firebase treats Android tokens inactive for 270 days as expired, so delete tokens older than that. Retry 429 and 5xx answers with backoff, as in retry with backoff.
Examples
JavaScript example
export async function pushOrPrune(row, message) {
try {
await sendPush(row.subscription, message);
} catch (error) {
if ([404, 410].includes(error.statusCode)) await db.pushSubscriptions.delete(row.id);
else throw error;
}
}
Python example
from pywebpush import WebPushException
def push_or_prune(row, message):
try:
send_push(row.subscription, message)
except WebPushException as error:
if error.response is not None and error.response.status_code in (404, 410):
db.push_subscriptions.delete(row.id)
else:
raise
How to check
Unsubscribe a staging browser and send to it. The row is gone after the send. A query finds no token with a last refresh older than 270 days.
Sources
- Firebase documentation, FCM registration token management
- RFC 8030, Generic Event Delivery Using HTTP Push
Related rules
- API-34 Retry with backoff and jitter in API design
- NTF-11 Use the current APNs and FCM interfaces in Notifications
- NTF-42 Keep Live Activity push tokens apart from device tokens in Notifications
- STK-76 Use the Web Push standard for browser push in Software choices
- STK-77 Use Firebase Cloud Messaging for Android and Apple app push in Software choices
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/notifications/push-notifications/prune-push-subscriptions">Agent Wranglers notifications playbook</a>. In code you write, add a comment next to it with the same link.