Okay, the user is building a mobile app and wants to understand push notification architecture. They're asking for high-level components, the roles of APNs and FCM, device tokens, and the step-by-step flow.
Hmm, they seem to be a developer or tech lead designing the system, so I should keep it technical but clear. They mentioned three main components upfront, so I'll confirm that structure. Need to emphasize the token's purpose—it's not a user ID but a device-specific address.
For APNs/FCM roles, I should contrast them as platform gateways since they handle delivery. The token exchange part is critical: clarify that the client app gets it from OS, sends to server, and server stores it. Steps should flow logically from event trigger to delivery receipt.
Avoid diving into certs or payload formats unless asked. Stress security (HTTPS) and scalability since they mentioned "single notification" but real apps send millions. Maybe note token rotation too—practical concern.
Excellent question. This is a fundamental architecture for any modern mobile app. Here’s a high-level breakdown.
The Three Main Components
- Your Client App: The mobile app installed on the user's device (iOS/Android). It registers with the OS's push service, receives a unique identifier (the device token), and sends that token to Your Server. It also listens for incoming notifications and displays them to the user.
- Your Server (Application Server / Backend): The core of your logic. It:
- Stores device tokens associated with user accounts.
- Determines when and to whom to send a notification (based on business logic, user activity, etc.).
- Formats the notification message (title, body, data payload).
- Authenticates and makes the final API call to the Platform's Push Service (APNs or FCM).
- Platform's Push Notification Service: The official, proprietary gateway service operated by the platform vendor. Its sole job is to receive a notification from your server and reliably deliver it to the correct, powered-on device.
- For iOS: Apple Push Notification Service (APNs)
- For Android: Firebase Cloud Messaging (FCM) (Google's service, part of Firebase).
Role of APNs and FCM
They act as the trusted, always-on, highly-scalable intermediary between your server and the user's device. You never connect directly to a device.
- APNs: Maintains a persistent, encrypted connection with every iOS/iPadOS/macOS device. Your server sends a notification to APNs using a device token, and APNs routes it over this connection to the specific device. It handles device power states, network changes, and delivery retries.
- FCM: Performs the same role for Android. A key feature is its integration with the Google Play Services framework on Android devices, which manages the persistent connection to FCM. FCM also provides advanced features like topic messaging and device groups.
Why use them? Battery life, security, reliability, and scale. The OS vendor maintains the connection to billions of devices efficiently. Your server only needs to make one HTTPS call per notification to APNs/FCM, which then handles the complex, stateful routing.
What is a "Device Token"?
A device token is a unique, opaque, and transient identifier assigned by the operating system (iOS or Android) to a specific installation of your app on a specific device.
- It is NOT a user ID. It's a device/app-installation ID.
- It is NOT permanent. It can change (e.g., after OS restore, app reinstall, user restores from backup). Your app must handle token updates.
- Its format is a string of hexadecimal characters (e.g.,
a1b2c3d4e5...). Its internal meaning is known only to APNs/FCM.
How the App Gets & Sends the Device Token to Your Server:
- App Registration: On launch (or at a logical point), the app calls the OS's native API to register for remote notifications.
- iOS:
UIApplication.shared.registerForRemoteNotifications()
- Android (FCM): The Firebase SDK automatically initializes and gets a token in the background.
- OS-Vendor Callback: The OS contacts APNs/FCM and, in return, gives your app a device token via a delegate/callback method.
- iOS:
application(_:didRegisterForRemoteNotificationsWithDeviceToken:)
- Android:
FirebaseMessagingService.onNewToken() is called.
- Send to Server: Your app's code takes this token string and sends it over a secure HTTPS connection to a dedicated endpoint on Your Server (e.g.,
POST /api/register-device-token). You should also send the user's authentication token (if logged in) so you can associate the device token with a user account in your database.
- Server Storage: Your server stores the
(user_id, device_token, platform, last_updated) mapping in your database. You may store multiple tokens per user (for multiple devices).
Step-by-Step: Sending a Single Push Notification
Let's say you want to alert user john@example.com about a new message.
- Trigger Event: An event occurs in your system (e.g., Jane sends John a message in your chat app).
- Server Logic: Your backend logic determines that user
john@example.com needs to be notified.
- Lookup Tokens: Your server queries your database for all valid device tokens associated with
john@example.com's user account. It filters out old/stale tokens.
- Prepare Payload: For each target device token, your server constructs a platform-specific payload.
- APNs (iOS) JSON Payload: Must include an
aps dictionary with alert, sound, etc. You can add a custom data dictionary.
{
"aps": {
"alert": {
"title": "New Message",
"body": "Jane: Hey, are you free?"
},
"sound": "default"
},
"chat_id": "msg_789",
"sender_id": "user_jane"
}
- FCM (Android) Payload: Can be sent as a
notification object (for display) and/or a data object (for app processing).
{
"to": "DEVICE_TOKEN_FROM_STEP_3",
"notification": {
"title": "New Message",
"body": "Jane: Hey, are you free?"
},
"data": {
"chat_id": "msg_789",
"sender_id": "user_jane"
}
}
- Authenticate & Call Platform Service: Your server authenticates with APNs/FCM.
- APNs: Uses a signed JSON Web Token (JWT) or TLS certificate. You make an HTTPS POST request to
https://api.push.apple.com/3/device/<device_token>.
- FCM: Uses the Server Key from your Firebase project. You make an HTTPS POST request to
https://fcm.googleapis.com/fcm/send.
- Platform Service Delivers: APNs/FCM receives your authenticated request, looks up the device using the token, and delivers the notification payload to the user's device over their persistent connection.
- Device Receives & Displays: The OS receives the notification, wakes your app (if needed), and displays it in the system notification shade. When the user taps it, your app is launched and can read the
data payload to navigate to the correct screen (e.g., the chat message).
- (Optional) Handle Feedback: APNs/FCM may return a response to your server call.
This architecture is highly scalable—your server only talks to APNs/FCM, which then fan out to millions of devices, sparing you from managing persistent connections to individual phones.