skip to content

In a parcel-tracking app, how should collapse identifiers and time-to-live be set so a 'driver is two stops away' alert never arrives hours late?

level: middleimportance: must knowfreq 55%

answer

  1. the service stores what it cannot deliver
  2. a lifetime as long as the news is true
  3. newest replaces older, per shipment
  4. only a few collapse slots kept
  5. carry an id and a version, not a record

basics

~20 s

Give each alert a time-to-live as long as it stays true (minutes for 'driver nearby') and each shipment one collapse identifier, so newer news replaces older. The push carries an id and version; the app fetches the record.

solid answer

~50 s

When the phone is off or out of coverage, APNs and FCM store the message and deliver it later, for days or weeks (one service defaults to four weeks), so a 'driver is two stops away' alert can land long after the van has gone. The fix is a **time-to-live** equal to the minutes the alert stays true, so a late copy is dropped instead of delivered. A **collapse identifier** per shipment makes a newer update replace an older one still waiting, so a returning phone gets the latest news rather than a backlog. Neither is a guarantee: one service keeps only four collapse identifiers per device, the other stores one notification per app, and neither keeps order. So the push carries the shipment id and a status version, the app ignores anything older than what it shows, and it fetches the record itself.

code

json · 15 lines
json
{
  "to": "<device token>",
  "collapseId": "parcel-shp_8f3k2",
  "ttlSeconds": 900,
  "priority": "high",
  "alert": {
    "title": "Your parcel is two stops away",
    "body": "Arriving in about 10 minutes"
  },
  "data": {
    "shipmentId": "shp_8f3k2",
    "event": "driver_nearby",
    "statusVersion": 41
  }
}

go deeper

for a junior

Recall that undelivered pushes are stored and delivered later, that a lifetime stops stale ones, and that a collapse identifier lets a newer push replace an older one.

for a middle

Explain how lifetime and collapse work on APNs and FCM, pick a lifetime per message from how long it stays true, and use one identifier per shipment.

for a senior

Show the limits: four stored identifiers on one service, one stored notification on the other, no ordering, so the push carries an id and version and the app fetches.

for a principal

Set a house rule that every push type is declared with its lifetime, collapse scope and priority, and that none of them is the only source of a fact.

## Why a late alert happens at all A push service does not drop a message just because the phone is unreachable. When the device is off, out of coverage or asleep, the service **stores** the message and tries again when the device reconnects. How long it keeps trying is the message's **time-to-live** (TTL), also called its expiration: - One service accepts a lifetime from 0 seconds to 28 days and uses four weeks when the sender sets none. - The other stores an undeliverable notification for up to 30 days, depending on the expiration the sender sets. - On one service, a lifetime of zero means "deliver now or discard"; the open standard for browser push defines the same idea. So with defaults, a customer who loses signal in a lift at 14:02 and gets it back at 17:30 can be told at 17:30 that their driver is two stops away. That alert is worse than no alert: it sends them running to the door for a van that left hours ago. ## Time-to-live: as long as the news is true Set each message's lifetime from the question "after what moment would delivering this be wrong?" | Message | Shown to user | Collapse identifier | Lifetime (scenario assumption) | |---|---|---|---| | Shipments list changed | no (data-only) | one for the whole list | a few hours; the on-open fetch covers the rest | | Status changed for one parcel | yes | one per shipment | about a day | | Delivery window updated | yes | one per shipment | until the window starts | | Driver is two stops away | yes | one per shipment | about 15 minutes | | Signature required | yes | a separate one per shipment | until the delivery attempt | The figures are this app's assumptions, not standard values; the point is that each lifetime comes from the meaning of the message. ## Collapse: newest replaces older A **collapse identifier** is a label the sender attaches; a newer message with the same label replaces an older one that is still waiting. With one identifier per shipment, a phone that was offline all afternoon receives "delivered" instead of "at depot", "out for delivery" and "two stops away" in a burst. Some limits matter: - One service stores at most **four** different collapse identifiers per device, with no rule about which survive when there are more. - The other stores only **one** undelivered notification per app, usually but not always the latest. - On one platform, at most 100 messages without a collapse identifier are stored per device; beyond that all are discarded and the app is told to do a full sync. So do not mint an identifier per event. Use one per shipment for its routine news, one for the list nudge, and a separate one only for a notice a routine update must not overwrite, such as a signature request. A customer with six active parcels will still lose some stored pushes, which is acceptable only because the app fetches the truth when it opens. ## Order and payload: carry an id and a version Both services may deliver out of order. A "two stops away" push sent after "out for delivery" can arrive first, or after "delivered". Therefore: 1. Every push carries the **shipment id** and a **status version** the server increments on each change. 2. The app ignores a push whose version is not newer than what it already shows. 3. The app fetches the shipment record itself rather than trusting a record inside the payload. Payloads are small anyway (one service caps them at 4 KB), and an id-sized payload also gives a tapped notification exactly what it needs to open the right parcel. ## What the user sees - After a long time offline: one current notification per parcel, not a stack of stale ones. - A "driver nearby" alert only while it is still useful; if it expired, the opened app shows the current status instead. - A signature notice kept apart from routine updates by its own identifier; because neither service promises to keep it, the parcel's card in the app shows the same need when opened. ## Where this question stops How the server queues, prioritises and caps these sends, and when it falls back to email or SMS, is the notification pipeline's design. Each service's exact field names and headers belong to the vendor's own documentation. What happens when the tap opens the app is the navigation design's. This question keeps the two promises the client can design around: a message that is no longer true should die, and a newer message should replace an older one.

  • What should a 'signature required' notification show on a locked screen?
    Assume anyone holding the phone can read it. Say that a parcel needs a signature and roughly when, and leave out the sender, contents, full address and any collection code. The detail appears in the app after unlock, fetched by the shipment id the push carried.
  • The user has more active parcels than one service keeps collapse identifiers for; what breaks?
    On that service only four identifiers per device are stored, and which survive is undetermined, so some stored updates for an offline phone are lost. The design accepts that: each push is a hint, and opening the app fetches every shipment's current status. The mistake would be minting an identifier per event, which loses more.
  • Why not send 'driver nearby' with a lifetime of zero?
    Zero means deliver now or never, which is right only if a few seconds of unreachability should cancel the alert. A phone on a train between masts would miss it. A short lifetime of some minutes survives a brief gap and still dies before it becomes misleading; the exact figure is this app's assumption.

A courier's note through the letterbox saying 'back in 10 minutes' is useful for ten minutes; one found after a weekend away should go straight in the bin, and a fresh note should replace it rather than pile up beside it.

  • A late 'driver nearby' alert is harmless, so the default lifetime is fine.
  • A collapse identifier guarantees only the newest status is ever delivered.
  • Pushes for one shipment arrive in the order they were sent.
  • The push service keeps every undelivered message until the phone returns.
  • Putting the whole shipment record in the push removes the need to fetch it.
  • A fresh collapse identifier per event keeps every update and still collapses.