M-Swift All articles
Testing & Tooling

Push Notification Debt: The Device Tokens Your App Forgot to Retire

M-Swift
Push Notification Debt: The Device Tokens Your App Forgot to Retire

Photo: 茅野ふたば, Public domain, via Wikimedia Commons

Push notifications are a weird kind of infrastructure: when they work, nobody says a word. When they fail, the app looks fine. The lock screen is just quiet. And quiet is easy to blame on the user, the campaign, or iOS being iOS.

Most push problems I see aren't copy problems. They're token problems. A device token is not a user ID, not an install ID, and definitely not permanent. It's an opaque receipt for one app on one device in one APNs environment. Treat it like a subscription that expires without telling you.

Start with a census, not a dashboard

Before touching code, pull a token census from your backend. For each user, count active tokens by environment, bundle ID, and APNs topic. Add last registration time, last successful send, last APNs error, app version, and OS version. You're looking for weird shapes: one user with 14 production tokens, a token that has been active for 19 months with no app opens, or a production token whose first-seen date matches a TestFlight build.

That last one is the classic sandbox leak. A debug build registers against the sandbox APNs environment, your server labels it production, and every send returns BadDeviceToken. The token never gets removed because nobody reads the response. It just sits there, inflating your audience and making re-engagement numbers look better than they are.

Where tokens go to die

There are four usual suspects. Device restores and transfers can produce a new token while the old one lingers. Reinstalls create fresh tokens, but your server may keep the old one if dedupe is based on user ID alone. Environment mix-ups tag sandbox tokens as production. And token refresh races can write an older token after a newer one, especially if registration and login happen in parallel.

None of these are dramatic. That's the problem. A stale token doesn't crash. It just turns into a polite 410 from APNs, which your server logs at info level and forgets.

Tag tokens at the door

Make the client send context with every registration: environment, bundle ID, APNs topic, and a stable app-install ID. On the server, store tokens with a composite key of token plus environment plus topic. Reject mismatches early. If a registration says sandbox and your production endpoint receives it, drop it and log a metric. Don't normalize it into production because the payload looked close enough.

Also dedupe by token, not by user. The same user can have an iPhone, an iPad, and a replacement phone. The same physical device can have your app installed twice under different Apple IDs. Those are separate delivery targets, and your database should say so.

Leave a breadcrumb trail on the client

You don't need a full analytics pipeline to debug registration. A few OSLog events will do. Log when registration starts, when it succeeds or fails, whether notification authorization is granted, and a hashed prefix of the token. Never log the full token. It's a delivery credential, and logs have a way of ending up in places you didn't plan.

Sample those events. If registration success drops after an iOS update, you'll see it. If a background refresh loop is calling registerForRemoteNotifications every few minutes, you'll see that too. That loop is worth catching. Silent pushes have a budget, and so does user patience.

Sweep dead tokens on the server

APNs is your source of truth. Every send returns a response, and every response should update token state. A 410 Unregistered is a retirement notice. BadDeviceToken usually means environment or topic mismatch. Treat repeated failures as a signal, not noise.

Run a weekly sweep: group tokens by last APNs response, prune tokens that have been unregistered for more than a few days, and quarantine tokens with repeated BadDeviceToken errors for manual review. Then compare your active token count with actual app opens. If the gap is widening, you have ghost tokens.

Collapse IDs are not cleanup

Collapse IDs group notifications, but they don't invalidate tokens. A stale collapse ID can make a thread look current while the underlying token is dead. Audit your collapse IDs like you audit everything else: stable per conversation, not per send, and never reused across unrelated features. If your lock screen shows one old notification for a thread that has moved on, your collapse logic is lying.

The 30-minute monthly audit

Once a month, check four things: token growth by environment, registration failure rate, APNs error mix, and silent-push volume per active token. If any of those look off, you'll know where to look. Push doesn't fail loudly. But your token table will tell you the truth if you ask it.

All Articles

Related Articles

The Automation Nobody Finds: Auditing App Intents Before They Gather Dust

Your App Gained 12 MB and Nobody Blinked: Making Binary Size a Budget Item

Your App Gained 12 MB and Nobody Blinked: Making Binary Size a Budget Item

Canceled but Still Charged? A StoreKit 2 Entitlement Audit That Saves Support Tickets

Canceled but Still Charged? A StoreKit 2 Entitlement Audit That Saves Support Tickets