Firebase FCM (Javascript): Error when request permission (Error status = 500)

Viewed 12262

I'm new to Firebase Cloud Messaging and I need to implement notifications in my webapp. If the browser requests notification for the first time there is no error occurred and the token fetched successfuly. But if I delete the notification from the browser parametre (I use Chrome) and ask for permission again, it shows me an error in the console.

DELETE https://fcmregistrations.googleapis.com/v1/projects/teak-perigee-*****/registrations/dcVW8MdcapIy5CrSqGutkj:APA91bFoslZEsjgIk16CUfol*****************

FirebaseError: Messaging: A problem occured while unsubscribing the user from FCM: FirebaseError: Messaging: A problem occured while unsubscribing the user from FCM: Internal error encountered. (messaging/token-unsubscribe-failed). (messaging/token-unsubscribe-failed).

Actually the token is fetched even this error occurs. but in this situation I handle the new token in the catch block of the promise. This is my code when permission is fired:

askForPermissioToReceiveNotifications = () => {

    const messaging = firebase.messaging();

   Notification.requestPermission().then(async (permission) => {

    if(permission == 'granted') {

        try {

            const token = await messaging.getToken();

            if(token) {

                console.log(token);
                return token;
            }

            else {
                console.log('No Instance ID token available. Request permission to generate one.');
            }
        }

        catch(error) {

            console.log('An error occurred while retrieving token. ', error);


            //BUT THE NEW TOKEN SUCCESSFULY FETCHED
            const token = await messaging.getToken();

            if(token) {

                console.log(token);
                return token;
            }

            else {
                console.log('No Instance ID token available. Request permission to generate one.');
            }
        }
    }

})
.catch(error => console.log(error));

}

I don't know if I miss something and I hope I can find a solution.

3 Answers

I faced a similar issue and believe this may be the explanation.

Issue was introduced in version 7.0.0 of the firebase-js-sdk. A workaround for now is to use version 6.6.2 or lower. I have filed a Github issue here for users effected to track.

So to enable use an older version just update the following in your index.html:

<script src="/__/firebase/6.6.2/firebase-app.js"></script>
<script src="/__/firebase/6.6.2/firebase-messaging.js"></script>

and change the following to your service worker file (typically named: firebase-messaging-sw.js):

importScripts('/__/firebase/6.6.2/firebase-app.js');
importScripts('/__/firebase/6.6.2/firebase-messaging.js');

it turns out that new versions of Firebase SDKs depend on a new internal infrastructure service, called FIS (the Firebase Installations Service) for targeting identifiers ("FIDs" or "Instance-IDs"). If you are using API key restrictions for the API keys you use in your application, you will have to extend those restrictions to allow usage with the new Firebase Installations Service at firebaseinstallations.googleapis.com.

To allow your API key in question to be used with the new Firebase Installations API:

  • go to the Google Cloud Console
  • choose the relevant project (i.e. the project you use for your application)
  • open the menu and go to APIs & Services -> Credentials
  • click Edit API key for the API key in question
  • scroll down to API restrictions
  • from the dropdown, choose FCM Registration API
  • click Save
  • wait a couple of minutes for Google servers to update and retry...

This behavior is due to firebase SDK issue, could check a workaround on this github thread and follow state of resolution.

Related