Firebase Messaging WIP to clean up service worker and implement proper notifications.

Missed in last commit
This commit is contained in:
Patrick Fic
2020-07-22 11:34:46 -07:00
parent 8f8c26af54
commit 4abef35822
4 changed files with 107 additions and 53 deletions

View File

@@ -52,20 +52,6 @@ export function register(config) {
}
});
}
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("/firebase-messaging-sw.js")
.then(function (registration) {
console.log(
"FCM Registration successful, scope is:",
registration.scope
);
})
.catch(function (err) {
console.log("FCM Service worker registration failed, error:", err);
});
}
}
function registerValidSW(swUrl, config) {
@@ -114,6 +100,7 @@ function registerValidSW(swUrl, config) {
function checkValidServiceWorker(swUrl, config) {
// Check if the service worker can be found. If it can't reload the page.
console.log("Seeing if service worker cna be found.");
fetch(swUrl)
.then((response) => {
// Ensure service worker exists, and that we really are getting a JS file.
@@ -123,6 +110,7 @@ function checkValidServiceWorker(swUrl, config) {
(contentType != null && contentType.indexOf("javascript") === -1)
) {
// No service worker found. Probably a different app. Reload the page.
console.log("No service worker found. Unregistering.");
navigator.serviceWorker.ready.then((registration) => {
registration.unregister().then(() => {
window.location.reload();
@@ -130,6 +118,8 @@ function checkValidServiceWorker(swUrl, config) {
});
} else {
// Service worker found. Proceed as normal.
console.log("Service worker found. Registering.");
registerValidSW(swUrl, config);
}
})