Get device token for push notification
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A device token is a unique identifier assigned by a push notification service (APNs for iOS, FCM for Android) to a specific app installation on a specific device. Your app must request this token, send it to your backend server, and your server uses it to target push notifications to that device. Tokens can change, so your app must handle token refresh.
How Push Notification Tokens Work
- Your app registers with the OS push notification service
- The OS contacts Apple (APNs) or Google (FCM) servers
- The service returns a unique device token
- Your app sends this token to your backend server
- Your server stores the token and uses it to send notifications via APNs/FCM
iOS: Getting the APNs Device Token
Request Permission and Register
Token Format
APNs tokens are binary data, typically 32 bytes. Convert to a hex string before sending to your server:
Android: Getting the FCM Token
Using Firebase Cloud Messaging
Handling Token Refresh
Tokens can change when the app is reinstalled, data is cleared, or the OS rotates tokens. Override onNewToken to catch updates:
Register the service in AndroidManifest.xml:
Sending the Token to Your Server
Server-Side: Storing and Using Tokens
Common Pitfalls
- Tokens change: Device tokens are not permanent. They can change when the app is reinstalled, the device is restored from backup, or the OS rotates them. Always handle
onNewToken(Android) anddidRegisterForRemoteNotificationsWithDeviceToken(iOS) to update your server. - Simulator tokens: iOS simulators do not receive real APNs tokens. You must test on a physical device. Android emulators with Google Play Services can receive FCM tokens.
- Stale tokens: If you send to an expired or unregistered token, APNs returns a 410 response and FCM returns
NOT_REGISTERED. Remove these tokens from your database to avoid wasted requests. - Security: Always transmit tokens over HTTPS. Tokens themselves are not secret (they cannot be used to read notifications), but they can be used to send unwanted notifications if your server endpoint is not authenticated.
- Permission denied: On iOS, if the user denies notification permission,
registerForRemoteNotificationsis never called and you never receive a token. Check authorization status before attempting to register.
Summary
- Request notification permission, then register for remote notifications to receive a device token
- On iOS, implement
didRegisterForRemoteNotificationsWithDeviceTokento capture the APNs token - On Android, use
FirebaseMessaging.getInstance().tokenand overrideonNewTokenfor FCM - Send the token to your backend over HTTPS and update it whenever it changes
- Handle invalid/expired tokens by removing them from your database when the push service reports them as unregistered
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.