mobile/push-notifications-how-your-phone-buzzes-for-an-app-that-isn-t-running.md

Push notifications: how your phone buzzes for an app that isn't running

Mobile · 4 min read ·

Your phone buzzes with a message from an app that isn't even running. How? Phones keep one shared connection to a push service (Apple's APNs or Google's FCM), and apps get a device token that works like a mailing address.

Step a message through the trip in a simulator: phone online, phone offline, app uninstalled. Then see a real 121-byte payload, the 4 KB size limit, and why a push should be a small nudge rather than the delivery itself.

Green outline of a phone receiving a notification, with a curly-brace data packet flying toward it

The puzzle

Your phone is in your pocket with the screen off. A friend sends you a chat message, and a second later the phone buzzes and shows it. Yet the chat app was not running. Who woke it up, and how did the message find your phone among billions?

The answer is a push notification: a small message that a company's server pushes to your phone, instead of the phone asking "anything new?" every few seconds. Asking constantly would drain the battery, so phones do something smarter.

One shared connection instead of one per app

If every app kept its own connection open to its own server, a phone with 80 apps would be busy all day. Instead, the phone's operating system (the main software that runs everything else) keeps one connection to a push service run by the platform's maker. On iPhones that is Apple's service, APNs (Apple Push Notification service). On Android phones with Google services it is FCM (Firebase Cloud Messaging). Every app on the phone shares that one connection.

When an app first asks for notifications, the operating system registers with the push service and hands the app a device token: a long string that works like a mailing address for that app on that phone. The app sends the token to its own server, and the server stores it next to your account.

The journey of one message

When your friend hits send, this happens:

1. The chat company's server looks up your device token. 2. It sends the push service a request containing the token and a small payload (the message content, written as JSON, a text format for structured data). 3. The push service finds the phone's open connection and forwards the message. 4. The operating system shows the notification, or wakes the app so it can fetch more.

Try it below. Pick what happens to the phone, then step through the trip.

Notice what each scenario shows. With the phone offline, the push service does not throw the message away; it keeps it for a limited time (you can set how long) and delivers it when the phone reconnects. With the app uninstalled, the push service eventually tells the server that the token is no longer valid, and the server should stop sending to it.

What a payload looks like

Here is a small payload in the shape APNs expects, built and measured with Node.js:

const payload = {
  aps: { alert: { title: "Order shipped", body: "Your parcel leaves the depot today." }, sound: "default" },
  orderId: 4821
};
const text = JSON.stringify(payload);
console.log(text);
console.log(Buffer.byteLength(text, "utf8") + " bytes of 4096 allowed");

Running it prints:

{"aps":{"alert":{"title":"Order shipped","body":"Your parcel leaves the depot today."},"sound":"default"},"orderId":4821}
121 bytes of 4096 allowed

The aps part is what the system shows; orderId is extra data of your own that the app can read. The size matters: Apple documents a limit of 4 KB (4,096 bytes) for standard notifications, and Firebase documents 4096 bytes too. A push is a nudge, not a delivery truck. If the payload is too big, APNs refuses it with a 413 error.

Notification messages and data messages

Firebase describes two kinds of message. A notification message has ready-made visible fields such as a title and a body, and the system can display it for you. A data message carries only your own key-value pairs, and your app's code decides what to do with them. Because a push is small, a common pattern is to send a tiny data message such as "new message in chat 7" and let the app download the full text itself.

Why a push can still go missing

Pushes are best-effort: neither the push services nor the phone promise every message arrives instantly. A few common reasons a notification does not show up: the user turned notifications off for that app in the phone's settings, the token is out of date, the phone was offline longer than the message's lifetime, or the phone's battery-saving rules delayed it. A good app never treats a push as the only way to learn something. It also checks its own server when it opens.

What this means for your own app

If you build a mobile app, you will not talk to phones directly. You will do three things: ask the user for permission to show notifications, save the device token on your server, and send small payloads to APNs or FCM. Handle the "token no longer valid" answer by deleting that token. And keep the payload tiny, with the real data waiting on your server.

$ ls mobile/

see all →
mobile/list-recycling-why-a-phone-can-scroll-10-000-contacts-while-building-o.md List recycling: why a phone can scroll 10,000 contacts while building only 8 rows A phone can scroll through 10,000 contacts without building 10,000 rows. It builds only enough row views to cover the screen, then recycles… 5 min read · 25 Oct mobile/offline-sync-what-happens-when-two-phones-edit-the-same-contact.md Offline sync: what happens when two phones edit the same contact You edit a contact's phone number on a plane. Your partner fixes the city on a tablet. Both devices are offline, then both reconnect. Which… 5 min read · 19 Oct mobile/deep-links-why-one-link-opens-an-app-and-another-opens-a-web-page.md Deep links: why one link opens an app and another opens a web page Tap the same link on two phones and one opens an app while the other opens a web page. Who decides, and how? This article explains deep… 4 min read · 16 Oct mobile/app-lifecycle-why-your-phone-forgets-what-you-typed-when-you-switch-ap.md App lifecycle: why your phone forgets what you typed when you switch apps You switch apps for a minute, and when you come back the message you were typing is gone. Nothing was broken: the phone needed memory… 4 min read · 13 Oct mobile/pixels-dp-and-points-why-a-button-of-48-is-tiny-on-one-phone-and-fine.md Pixels, dp and points: why a button of 48 is tiny on one phone and fine on another A button you size at 48 looks fine on one phone and tiny on another, because a sharper screen packs more pixels into the same space. This… 4 min read · 10 Oct mobile/how-does-a-push-notification-find-your-phone-when-the-app-is-closed.md How does a push notification find your phone when the app is closed? A push notification does not come from your app. Your phone keeps one connection to a service run by Apple or Google, and your app's server… 5 min read · 6 Oct