FirebaseError: Messaging: A problem occurred while unsubscribing the user from FCM

3,450 views
Skip to first unread message

Frédéric Jost

unread,
Jun 22, 2023, 8:23:33 AM6/22/23
to Firebase Google Group
Good morning,

I have a problem in my application when I try to delete the FCM token.

I'm using the package in a Nextjs application
"firebase": "^9.22.2",

The scenario is as follows:

The user logs in to the application then is registered with FCM which provides him with an FCM token

```
// Firebase.ts
import {FirebaseApp, initializeApp} from 'firebase/app';
import {getMessaging as getFirebaseMessaging, getToken, Messaging} from 'firebase/messaging';

const firebaseConfig = {
apiKey: 'xxx',
authDomain: 'xxx',
projectId: 'xxx',
storageBucket: 'xxx',
messagingSenderId: 'xxx',
appId: 'xxx',
};

export function getFirebaseApp(): FirebaseApp {
return initializeApp(firebaseConfig);
}

export function getMessaging(firebaseApp: FirebaseApp): Messaging {
return getFirebaseMessaging(firebaseApp);
}
export async function getFCMToken(messaging: Messaging): Promise<string> {
return await getToken(messaging, {
vapidKey: 'xxx',
});
}
// Notification.ts
import {deleteApp} from '@firebase/app';
import {deleteToken} from '@firebase/messaging';
const registerFCMToken = async (firebaseApp: FirebaseApp, messaging: Messaging): Promise<void> => {
...

const firebaseToken = await getFCMToken(messaging).catch((e) =>
console.error('An error occurred while retrieving token. ', e)
);

if (!firebaseToken) {
throw new Error('No registration token available.');
}


// => persist token in db ...
};
const deleteFCMToken = (firebaseApp: FirebaseApp, messaging: Messaging) => {
...

deleteToken(messaging)
.
then(() => {
deleteApp(firebaseApp)
.
then(() => {
... // => supress the FCM token from DB
})
.
catch(function (error) {
console.error('Error deleting FCM app:', error);
});
})
.
catch(function (error) {
console.error('Error deleting FCM token:', error);
});
};
... const firebaseApp = getFirebaseApp();
const messaging = getMessaging(firebaseApp);

// If permission for notifications is denied we must reset the FCM token and remove data from innoDB
if (isAuthorized === false) { // => isAuthorized became false if user denied notification permissions deleteFCMToken(firebaseApp, messaging); // => 404 !?

return;
}

await registerFCMToken(firebaseApp, messaging).catch((e) => console.error('getFCMToken error', e)); // => here the token is generated and some firebase data are created in IndexedDB ```

Here everything works fine, when registerFCMToken is called, the token is either created from FCM endpoints and inserted into the IndexedDB tables firebase generated, or retrieved from IndexedDB data.

The user after some time, decides not to receive notifications anymore. Then the code executes the function deleteFCMToken, which calls deleteToken(messaging) which calls the endpoint
https://fcmregistrations.googleapis.com/v1/projects/{project_id}/registrations/{fcm_token}
which returns me a 404:
```
{
  "error": {
    "code": 404,
    "message": "Requested entity was not found.",
    "status": "NOT_FOUND"
  }
}
```
and data from IndexedDB is still present with the FCM token.

My question is, is the token deleted from FCM, in this case FCM chooses to return a 404 rather than a 20x, but the javascript sdk does not delete the data from IndexedDB? Or is there actually a problem on FCM and it should return a 20x then delete the data from IndexedDB?

I've seen several threads on stackoverflow and github but not really responding. Sometimes it looks like it's the sdk version, sometimes it looks like it's from FCM.

The documentation for deleteToken:
```
/**
* Deletes the registration token associated with this {@link Messaging} instance and unsubscribes
* the {@link Messaging} instance from the push subscription.
*
* @param messaging - The {@link Messaging} instance.
*
* @returns The promise resolves when the token has been successfully deleted.
*
* @public
*/
export declare function deleteToken(messaging: Messaging): Promise<boolean>; ```

Some resources found:
- in the last comments: https://github.com/firebase/firebase-js-sdk/issues/2364
https://github.com/firebase/firebase-js-sdk/issues/2469
https://stackoverflow.com/questions/64509791/firebase-messaging-deletetoken-failed-when-deleting-token

Thanks for your help!


Grzegorz Miszewski

unread,
Jul 7, 2023, 3:37:25 PM7/7/23
to Firebase Google Group
Hi,

I think I have the similar issue.
In my case it's even worse. 

I'm using Flutter integration, but this part is done by javascript library.
I've been using old Flutter libs that were using Firebase libraries v8, and after migration to v9 notifications stopped working.

Now in browser (chrome) i get floods of tries to register/remove tokens. Everytime i refresh page it creates new token, and previous one is added to that list.
But I can't see any token that successfully registers. But after that - flutter method: firebaseMessaging.getToken(vapidKey: Config.publicVapidKey)
returns token. But when I try to send push to this from backend it fails saying that token is not registered.
(This is no surprise, as non of register call works) 

I never use deleteToken when that happens.
errors.png
Reply all
Reply to author
Forward
0 new messages