I would like to set a variable messagingSenderId value in my service worker, not an hardcoded one. Is it possible?
I register my service worker like this:
navigator.serviceWorker.register( 'firebase-messaging-sw.js' )
.then( function( registration ) {
messaging.useServiceWorker( registration );
});
And in my firebase-messaging-sw.js, I initialize firebase like this
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-app.js' );
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-messaging.js' );
firebase.initializeApp({
'messagingSenderId': 'my-id' // <- I want this to be variable
});
The problem is I can't find how to pass data to my service worker file. Any idea?
Thanks
As mentionned, Passing state info into a service worker before 'install' answered the question. Thanks.
Here is the answer for this use case:
You need to pass the variable in the URL like so:
And then, in firebase service worker (firebase-messaging-sw.js), you can get this variable like so:
This works. But URL.searchParams is a very new tool. It is less compatible than Firebase itself.
So instead of:
I suggest using: