Prompt
How do I handle push notification token refresh automatically?
Latest observation
Oct 14, 2026
OpenAI APIWeb search: off
To handle push notification token refresh automatically, the key idea is:
- Listen for token changes
- Send the new token to your backend
- Replace the old token in your database
- Do this on app start and whenever the token rotates
General approach
1) Register a token refresh listener
Most push systems rotate tokens occasionally for security or device/app changes.
- Firebase Cloud Messaging (FCM): use the token refresh callback/listener
- APNs (iOS): device token is provided again through registration callbacks
- Expo / other providers: usually have an event or method to re-fetch the token
2) Update your backend immediately
When you get a new token:
- authenticate the user
- send the new token to your API
- mark the old token as invalid or replace it
- associate it with the correct user/device
3) Refresh token on app launch
Even if you listen for refresh events, also fetch the current token when the app starts:
- app reinstall
- app restore
- login/logout changes
- missed refresh event while app was offline
4) Handle invalid tokens from push delivery failures
If your push provider says a token is invalid/unregistered:
- remove it from your database
- stop sending notifications to it
Example: Firebase Cloud Messaging
JavaScript / React Native
import messaging from '@react-native-firebase/messaging';
async function registerToken() {
const token = await messaging().getToken();
await sendTokenToServer(token);
}
messaging().onTokenRefresh(async token => {
await sendTokenToServer(token);
});
// call once on app start / login
registerToken();
Web
messaging.onTokenRefresh(async () => {
const newToken = await messaging.getToken();
await sendTokenToServer(newToken);
});
Best practices
- Store tokens per device, not just per user
- Deduplicate tokens on the backend
- Remove stale tokens when push sending fails
- Update token after login if your app supports multiple accounts
- Secure the endpoint that receives tokens
- Log token update events for debugging
Common pitfalls
- Only saving the token once at install time
- Not updating the backend after token refresh
- Keeping multiple stale tokens for the same device
- Forgetting to fetch token again after reinstall or logout/login
If you tell me your platform/framework (Flutter, React Native, Android, iOS, web, Firebase, Expo, etc.), I can give you the exact code for automatic token refresh.