I have created a firebase account, though I skipped the npm installation in wix. I added this to my custom code with all the firebaseConfig values
<script src="https://www.gstatic.com/firebasejs/8.3.2/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.3.2/firebase-analytics.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.3.2/firebase-messaging.js"></script>
<script>
const firebaseConfig = {
apiKey: "",
authDomain: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: ""
};
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
firebase.initializeApp(firebaseConfig);
firebase.analytics();
console.log("firebase ", firebase);
const messaging = firebase.messaging();
window.addEventListener('message', function(event) {
console.log("Data received :",event.data);
if( event.data !== null){
if('serviceWorker' in navigator){ // browser supports service workers
navigator.serviceWorker.register('_functions/firebase_messaging_sw_js').then((sw_registration) => {
messaging.requestPermission().then(()=>{
console.log("Permission granted");
messaging.getToken({serviceWorkerRegistration: sw_registration}).then((token)=>{
window.location.assign('https://www.mywebsite.com/?fcm_token='+token);
}).catch((tokenError)=>{
console.log("Token Error ", tokenError);
})
}).catch((permissionError)=>{
console.log("permission Error ", permissionError);
})
}).catch((registrationError)=>{
console.log("registration Error ", registrationError);
})
}
else{ // browser does not support service workers
window.location.assign('https://www.mywebsite.com/?sw=notsupported');
}
}
})
messaging.onMessage((payload)=>{console.log("On Message - payload received ", payload)});
</script>
And added this to my to the http-functions.js backend with all the values
import { ok, notFound, created, serverError } from 'wix-http-functions';
let firebase_messaging_sw_js = `
importScripts('https://www.gstatic.com/firebasejs/8.3.2/firebase-app.js') importScripts('https://www.gstatic.com/firebasejs/8.3.2/firebase-messaging.js')
firebase.initializeApp({
apiKey: "",
authDomain: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: "" })
const messaging = firebase.messaging();
messaging.onBackgroundMessage((payload) => {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body };
self.registration.showNotification(notificationTitle,
notificationOptions); });`
export function get_firebase_messaging_sw_js(request) {
console.log("get firebase messaging sw js with request ", request);
let options = {
"headers": {
"Content-Type": "application/javascript"},
"body": firebase_messaging_sw_js };
return ok(options);
}
This does not trigger an subscription request to a user, please what am I missing here