Firebase JS - getToken() does not return a token

firebase-cloud-messaging, javascript

Solution

I have the same problem, and I spend more than 6hours to solve this problem. You need to create dummy "firebase-messaging-sw.js"(empty file) file in home directory. My html page location is "http://localhost:56050/Notification/index.html" and my firebase-messaging-sw.js is "http://localhost:56050/firebase-messaging-sw.js"

Problem

I have the following code, based on Google's 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); }); ``` Strangely enough, the `then` function of `messaging.getToken()` is never called. The console does display `Notification permission granted.`, but after that, it stays quiet (i.e. I don't get any errors either). Am I doing something wrong? EDIT: It should be noted that I'm trying to implement this in a Chrome extension.

Original source