
If you’re working with Firebase Cloud Messaging (FCM) and encounter the error:
FirebaseMessagingError: Requested entity was not found.
with the error code:
messaging/registration-token-not-registered
this means that the FCM registration token is invalid, expired, or unregistered. This issue can prevent push notifications from being delivered to users.
๐ Possible Causes & Solutions
1️⃣ Invalid or Expired FCM Token
FCM tokens are not permanent and may expire over time. If you’re storing tokens in your database, some might be outdated.
✅ Solution:
- Remove invalid tokens from your database when sending push notifications.
- Refresh and store the latest FCM token when the app starts.
Example: Automatically Refresh Token
firebase.messaging().onTokenRefresh((newToken) => {
// Send newToken to your backend and update the stored token
});
2️⃣ Token Unregistered on Client Device
A token might become unregistered if:
- The app is uninstalled on the user’s device.
- The app is reinstalled, generating a new token.
- The user logs out, and the app deletes the token.
- The token was explicitly deleted from the client.
✅ Solution:
- Implement token refreshing and remove outdated tokens from your backend.
- If a token fails with this error, delete it from your database.
Example: Remove Expired Token from Database
async function sendPushNotification(token: string, message: any) {
try {
await admin.messaging().send({ token, ...message });
} catch (error) {
if (error.code === 'messaging/registration-token-not-registered') {
console.log('Invalid token detected. Removing from DB:', token);
await deleteTokenFromDB(token); // Implement a function to remove the token
}
}
}
3️⃣ Incorrect Token Format
If a token is truncated or modified when saved in the database, Firebase will reject it.
✅ Solution:
- Ensure tokens are stored correctly in the database.
- Log tokens before sending push notifications to verify their integrity.
Example: Debugging Token Storage
console.log('FCM Token:', token); // Check if it’s a valid string
4️⃣ Incorrect Firebase Configuration
If your Firebase Admin SDK is misconfigured, FCM might reject valid tokens.
✅ Solution:
- Ensure you are using the correct Firebase Admin SDK JSON credentials.
- Re-authenticate your Firebase service with the correct credentials.
Example: Proper Firebase Admin SDK Configuration
import * as admin from 'firebase-admin';
admin.initializeApp({
credential: admin.credential.cert(require('./firebase-service-account.json')),
});
5️⃣ App Permissions Disabled
If a user disables notifications in their device settings, FCM might invalidate the token.
✅ Solution:
- Check if users have granted notification permissions.
Example: Checking Notification Permissions
const status = await messaging().requestPermission();
if (status !== 'granted') {
console.log('Notifications are disabled');
}
๐ Final Debugging Steps
- Check if the token is still valid by testing it manually in Firebase Cloud Messaging.
- Log token values before sending notifications to ensure they are formatted correctly.
- Handle expired tokens in your backend by removing them after a failed notification attempt.
- Ensure Firebase Admin SDK is correctly configured with the right credentials.
✅ Best Practices for Managing FCM Tokens
- Store only valid FCM tokens in your database.
- Refresh and update tokens regularly.
- Handle errors gracefully by removing expired tokens.
- Monitor push notification logs to detect failed deliveries early.
By implementing these best practices, you can ensure reliable push notification delivery and prevent common FCM token errors.
Need more help? Let me know in the comments! ๐