Hey !
I want to add push notification on my project. It’s working perfectly expect when the app is in background.
In order to trigger a banner notification, I added a service worker in /src
(the same location than index.html
.
This is the content :
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js');
firebase.initializeApp({
// get this from Firebase console, Cloud messaging section
'messagingSenderId': 'MESSAGINGID'
});
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(payload => {
console.log('Received background message ', payload);
// here you can override some options describing what's in the message;
// however, the actual content will come from the Webtask
const notificationTitle = "Background message title"
const notificationOptions = {
body: 'Background Message body.',
icon: '/assets/images/logo-128.png'
};
return self.registration.showNotification(notificationTitle, notificationOptions);
});
messaging.onMessage(function(payload) {
console.log('Message received. ', payload);
});
In index.html, I added this :
<script>
/* Service worker for Firebase Cloud Messaging */
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('firebase-messaging-sw.js')
.then(service => console.log('service worker installed :', service))
.catch(err => console.error('Error', err));
}
</script>
I tried to remove all the content, but I’m getting the same error in console : Error TypeError: Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script.
In angular.json
I added foreach configurations : "serviceWorker": true,
I’m using firebase plugin : cordova-plugin-firebase@2.0.5
Do you have an idea of how I could resolve it ?