Firebase JS - getToken () does not return a token

I have the following code based on google documentation :

var config = {
    apiKey: "XX",
    authDomain: "XX",
    databaseURL: "XX",
    storageBucket: "XX",
    messagingSenderId: "XX"
};
firebase.initializeApp(config);

const messaging = firebase.messaging();

messaging.requestPermission()
.then(function() {
    console.log('Notification permission granted.');
    messaging.getToken()
    .then(function(currentToken) {
        if (currentToken) {
            console.log(currentToken);
        } else {
            console.log('No Instance ID token available. Request permission to generate one.');
        }
    })
    .catch(function(err) {
        console.log('An error occurred while retrieving token. ', err);
    });
})
.catch(function(err) {
    console.log('Unable to get permission to notify. ', err);
});

Oddly enough, the thenfunction is messaging.getToken()never called. The console displays Notification permission granted., but after that it remains silent (i.e. I also do not get any errors).

Am I doing something wrong?

EDIT: It should be noted that I'm trying to implement this in a Chrome extension.

+7
source share
5 answers

, 6 , . "firebase-messaging-sw.js" ( ) .

html- " http://localhost:56050/Notification/index.html"
firebase-messaging-sw.js - http://localhost:56050/firebase-messaging-sw.js"

+8

. , nginx apache, firebase-messaging-sw.js . index.html firebase-messaging-sw.js .

0

firebase-messaging-sw.js , useServiceworker. . .:

messaging.useServiceWorker('pathToYourSW');
0

, API HTTPS SSL-. getToken() .

:

FCM SDK , HTTPS. , HTTPS.

, getToken() , .

0

"firebase-messaging-sw.js" , "messaging.getToken(). Then (function (currentToken) {" .

I went into Chrome "Developer Tools" β†’ "Applications" β†’ "Service Workers" (as shown in the figure below) and unregistered the old service worker. After that, "messaging.getToken (). Then (function (currentToken) {" returned normally.

Another problem that I encountered was that I initially had two Firebase projects for the same domain name, which is prohibited due to restrictions with SSL certificates.

Stop and Unregister an existing Firebase Messaging Service Worker in Chrome's Developer Tools

0
source

Source: https://habr.com/ru/post/1658821/


All Articles