Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 339
walid Avatar asked Oct 10 '19 14:10

walid


People also ask

What is FCM in Java?

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably send messages at no cost.

What are browser requirements for receiving and displaying FCM?

Stay organized with collections Save and categorize content based on your preferences. The FCM JavaScript API lets you receive notification messages in web apps running in browsers that support the Push API. This includes the browser versions listed in this support matrix and Chrome extensions via the Push API.


2 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');
like image 142
Thomas Burke Avatar answered Sep 19 '22 11:09

Thomas Burke


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...
like image 29
Edouard Yonga Avatar answered Sep 17 '22 11:09

Edouard Yonga