I am getting an error when calling firebase.messaging()
from the html page (not the service worker) that makes no sense.
Messaging: Please ensure that 'messagingSenderId' is set correctly in the options passed into firebase.initializeApp(). (messaging/bad-sender-id).
The app IS initialized with the correct messagingSenderId
in a <script>
further up in the <head>
. The default app is shown in the console log:
function deviceRegistration() {
console.log('in deviceRegistration()');
console.log("firebase.apps:", firebase.apps);
console.log("firebase.apps.length: ", firebase.apps.length);
if (firebase.apps.length == 0) {
// firebase.initializeApp({{ fcm_config.messagingSenderId }});
firebase.initializeApp({"messagingSenderId": "1234567890"});
}
console.log("after initializeApp"); // prints to console
var messaging = firebase.messaging(); // error happens here
console.log("after firebase.messaging()"); // doesn't print to console
The reason I only call firebase.initializeApp
if there are no apps in firebase.apps
is because when I call firebase.initializeApp
without first performing this check I get the following error:
Firebase: Firebase App named '[DEFAULT]' already exists (app/duplicate-app).
I thought I'm supposed to be able to call initializeApp
repeatedly (on every page) and it would register the service worker if necessary...
Can someone please help me figure out what's going on?
Oh silly me...
I populated the config variable with a (django) template tag and context variable that I incorrectly referenced. I did this:
var config = {
apiKey: "{{ fcm_config.apiKey }}",
authDomain: "{{ fcm_config.authDomain }}",
databaseURL: "{{ fcm_config.databaseURL }}",
projectId: "{{ fcm_config.projectId }}",
storageBucket: "{{ fcm_config.storageBucket }}",
messagingSenderId: "{{ fcm_config.messagingSenderId }}"
};
firebase.initializeApp(config);
When I meant to do this:
var config = {
apiKey: "{{ fcm_config.config.apiKey }}",
authDomain: "{{ fcm_config.config.authDomain }}",
databaseURL: "{{ fcm_config.config.databaseURL }}",
projectId: "{{ fcm_config.config.projectId }}",
storageBucket: "{{ fcm_config.config.storageBucket }}",
messagingSenderId: "{{ fcm_config.config.messagingSenderId }}"
};
firebase.initializeApp(config);
So firebase was right to complain about a bad sender ID, since I was passing in an object equivalent to:
firebase.initializeApp({
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: ""
});
Now that part works and I'm on to the next error!
(if you're wondering why I put those variables in fcm_config.config.*
, which seems redundantly redundant, it's because I also store fcm_config.VAPID
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.