Push Notification Tokens: FCM and APNs Device Token Management
Push notification tokens (also called device tokens or registration tokens) are unique identifiers assigned by push notification services to each device-app combination. Understanding how these tokens work is crucial for building reliable push notification systems.
What are Push Notification Tokens?
When a mobile app registers for push notifications, the operating system's push notification service (APNs for iOS, FCM for Android) assigns a unique token to that device. This token is specific to both the device and the app - no two apps on the same device share the same token, and the same app on different devices gets different tokens.
FCM (Firebase Cloud Messaging) Tokens
FCM tokens are registration tokens issued by Google's Firebase Cloud Messaging service. They identify an app instance on a device. FCM supports two types of tokens:
- Registration Token: Identifies a specific app instance on a device
- Instance ID: A unique identifier for the app instance that maps to the registration token
FCM tokens can be up to 4KB in length and should be treated as opaque strings. They may change due to app reinstalls, OS updates, or when the user clears app data.
APNs (Apple Push Notification service) Tokens
APNs device tokens are 32-byte (64-character hex string) identifiers assigned by Apple's servers. They're unique per device-app pair and are formatted as a hex string without colons or other delimiters. APNs uses token-based authentication (p8 certificate or p12 certificate) for server-to-service communication.
Token Lifecycle Management
Registration
On app launch, request notification permissions and register for remote notifications. Send the resulting token to your backend server. Handle the case where registration fails or the user denies permissions gracefully.
Storage
Store device tokens in your backend database associated with user accounts. Handle multiple tokens per user (multiple devices). Index tokens for efficient lookup.
Refresh and Invalidation
Monitor the FCM canonical registration ID (when a token is refreshed) and APNs feedback service (when a token is no longer valid). Implement webhooks or periodic cleanup to remove stale tokens from your database.
Conclusion
Proper device token management ensures reliable push notification delivery. Handle token registration, storage, refresh, and cleanup carefully to maintain a healthy notification system.
