In a parcel-tracking app, how should the push device token be managed so that after one user signs out, the next person on the phone does not get their alerts?
answer
- an address for the app, not the person
- the binding lives on the server
- ask for the token on every launch
- a restore or new phone means a new token
- detach before the session is gone
basics
~20 sA device token addresses this app on this device, not a person; the server's token-to-account binding decides who gets alerts. Fetch the token each launch, register it with the account and an install id, and detach it at sign-out.
solid answer
~50 sThe token APNs or FCM issues is the address of this app on this device. It says nothing about who is signed in; the server maps it to an account. If sign-out only clears the local session, the server still maps the token to the old account, and because the system shows visible notifications without running the app's code, the next person sees the old user's parcels. So the client: asks the system for the token on every launch, since one service issues a new one after a restore, a new device or an OS reinstall; registers it with the account and a stable install id whenever it changes; and at sign-out calls the server to **detach** the install while the session is still valid, queueing the call if offline. Sign-in rebinds by install id as a backstop.
code
pseudocode · 16 lineson app_launch:
token = push_service.request_token() # never read from local storage
if token is missing:
retry_registration_later()
else if session.signed_in:
register(token, session.account_id, install_id, permission_state())
on token_changed(new_token):
if session.signed_in:
register(new_token, session.account_id, install_id, permission_state())
on sign_out:
detached = api.detach_install(install_id) # authenticated, so before clearing
if not detached:
durable_queue.add(detach_install(install_id))
session.clear()go deeper
Recall that the push service issues a device token per app and device, that the server needs it to send, and that the app asks for it each launch.
Explain why the token changes after a restore, reinstall or new phone, and how the app registers it with the account, an install id and a timestamp.
Trace the shared-phone bug: detach before clearing the session, queue it when offline, rebind by install id at sign-in, since visible alerts bypass the app.
Decide where the source of truth for who receives a device's alerts lives, and make sign-out, account deletion and token rotation one audited path.
## What a device token is, and is not To send a push, the server needs an address. The platform push service, APNs or FCM, issues one to the app: the **device token** (also called a registration token). It identifies **this app on this device**: - It is unique per app and device; two apps on one phone get different tokens. - It is not tied to a user. The push service has no idea who is signed in to the app. - It can change. One service says never to cache it, because a new token is issued after a restore from backup, on a new device and after an OS reinstall; the other's client library delivers the current one on routine syncs at app start. So "who gets this phone's alerts" is not a property of the token. It is a row on the app's server: token, install, account. The client's job is to keep that row true. ## Why the partner sees the wrong parcels The bug in the question has a simple chain: 1. Alex signs in; the app registers its token against Alex's account. 2. Alex signs out. The app clears its local session and caches, and nothing else. 3. Sam signs in on the same phone. Perhaps the app registers the token for Sam too, so it is now attached to both accounts. 4. The server sends Alex's "driver is two stops away" to every token on Alex's account, including this phone. 5. A visible notification is usually shown by the system without the app's code running, so the app cannot quietly drop it. Sam sees Alex's parcel on the lock screen. The fix cannot live only on the device. It has to change the server's binding, and the client has to ask for that at the right moments. ## The lifecycle, step by step 1. **On every launch**, ask the system for the current token rather than reading a stored copy. Registration can fail without a network; set a flag and retry later. 2. **Register** the token with the signed-in account, a stable per-install id the app generates once, the notification permission state and a timestamp, whenever any of them changes, and at least periodically. One service suggests a monthly refresh is enough and that more often than weekly brings no benefit. 3. **On sign-in**, the server binds the install id to the new account and **replaces** any previous binding for that install. One install, one account at a time. 4. **On sign-out**, call the server to **detach** the install **before** clearing the session, because the call must be authenticated. If offline, keep the detach in a durable queue and send it as soon as the network returns. 5. **On reinstall, restore or a new phone**, the app gets a new token and registers it as on any launch. The old token stops working on its own; on one service an uninstalled app's token is invalidated the first time a send reaches it. 6. **One account, many devices**: the account holds a set of installs, each with its own token. The install id is what makes this robust. A token can change under the app, but the install id stays put, so the server replaces the old token instead of accumulating dead ones, and a later sign-in on the same install overwrites a binding that a failed detach left behind. ## The edges a senior answer covers - **Offline sign-out.** Between sign-out and the queued detach reaching the server, visible alerts for the old account can still appear. Say so, keep that window short, and let the next sign-in's rebind close it. - **Data-only pushes after sign-out.** These do run app code, so the app checks the account id in the payload against the signed-in one and ignores a mismatch. - **Account deletion.** Detach every install on the account at once, server side. - **Tokens the service rejects.** When a send response says a token is no longer valid, the server deletes it; one service also treats a token unused for over a month as stale and, on one platform, expires it after 270 days of inactivity. Pruning and sending are the notification pipeline's work. | Event | Token | Server binding | |---|---|---| | Launch | fetched fresh | refreshed with timestamp | | Sign-in | unchanged | install bound to new account | | Sign-out | unchanged | install detached | | Restore or reinstall | new | new token registered, old one dies | ## Where this question stops Storing, pruning and sending to tokens on the server belong to the notification pipeline. A framework's token callbacks and helper calls belong to the framework trees, and each service's registration calls to the vendor documentation. Keeping the payload and other stored secrets safe on a shared phone is the device-security design's. This question keeps the client's half: ask for the token, report it, and detach it before the next person picks up the phone.
- What if the user signs out with no connection?Put the detach in a durable queue and send it when the network returns, then clear the session. Until it lands, visible alerts for the old account can still appear, because the system shows them without the app. The next sign-in on the same install rebinds it on the server, closing the window even if the queued detach was lost.
- Why register an install id alongside the token?Because the token changes under the app after a restore or reinstall, and a token alone cannot say which old row it replaces. With a stable install id, the server updates one row per install, never piles up dead tokens for the same phone, and can rebind the install at the next sign-in even if a detach never arrived.
- How does the app learn that its token changed while it was running?It asks the system for the token on each launch, and one service's client library also reports a changed token to the running app. Either path leads to the same register call with the account, the install id and the permission state, so the server's row is replaced rather than duplicated.
- The device token belongs to the user and follows them to a new phone.
- A token can be cached once at install and reused forever.
- Clearing the local session at sign-out is enough to stop the old user's alerts.
- The app can detect its own uninstall and unregister its token then.
- A restore from backup keeps the same token, so nothing needs re-registering.
- The app can always hide pushes meant for another account when they arrive.