diff --git a/src/common/component/Notification/useDeviceToken.js b/src/common/component/Notification/useDeviceToken.js index 71ec92bb..7bc70f0d 100644 --- a/src/common/component/Notification/useDeviceToken.js +++ b/src/common/component/Notification/useDeviceToken.js @@ -4,27 +4,30 @@ import { getToken, getMessaging } from "firebase/messaging"; import { firebaseConfig } from "../../../app/config"; const useDeviceToken = (vapidKey) => { const [token, setToken] = useState(null); - const messaging = getMessaging(initializeApp(firebaseConfig)); + // https only + if (navigator.serviceWorker) { + const messaging = getMessaging(initializeApp(firebaseConfig)); - getToken(messaging, { - vapidKey, - }) - .then((currentToken) => { - if (currentToken) { - console.log("current token for client: ", currentToken); - setToken(currentToken); - // updateDeviceToken(currentToken) - // Perform any other neccessary action with the token - } else { - // Show permission request UI - console.log( - "No registration token available. Request permission to generate one." - ); - } + getToken(messaging, { + vapidKey, }) - .catch((err) => { - console.log("An error occurred while retrieving token. ", err); - }); + .then((currentToken) => { + if (currentToken) { + console.log("current token for client: ", currentToken); + setToken(currentToken); + // updateDeviceToken(currentToken) + // Perform any other neccessary action with the token + } else { + // Show permission request UI + console.log( + "No registration token available. Request permission to generate one." + ); + } + }) + .catch((err) => { + console.log("An error occurred while retrieving token. ", err); + }); + } return token; }; export default useDeviceToken; diff --git a/src/serviceWorkerRegistration.js b/src/serviceWorkerRegistration.js index 2262ecdb..332db576 100644 --- a/src/serviceWorkerRegistration.js +++ b/src/serviceWorkerRegistration.js @@ -11,15 +11,17 @@ // opt-in, read https://cra.link/PWA const isLocalhost = Boolean( - window.location.hostname === 'localhost' || + window.location.hostname === "localhost" || // [::1] is the IPv6 localhost address. - window.location.hostname === '[::1]' || + window.location.hostname === "[::1]" || // 127.0.0.0/8 are considered localhost for IPv4. - window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/) + window.location.hostname.match( + /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ + ) ); export function register(config) { - if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { + if (process.env.NODE_ENV === "production" && "serviceWorker" in navigator) { // The URL constructor is available in all browsers that support SW. const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); if (publicUrl.origin !== window.location.origin) { @@ -29,7 +31,7 @@ export function register(config) { return; } - window.addEventListener('load', () => { + window.addEventListener("load", () => { const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; if (isLocalhost) { @@ -40,8 +42,8 @@ export function register(config) { // service worker/PWA documentation. navigator.serviceWorker.ready.then(() => { console.log( - 'This web app is being served cache-first by a service ' + - 'worker. To learn more, visit https://cra.link/PWA' + "This web app is being served cache-first by a service " + + "worker. To learn more, visit https://cra.link/PWA" ); }); } else { @@ -53,6 +55,7 @@ export function register(config) { } function registerValidSW(swUrl, config) { + if (!navigator.serviceWorker) return; navigator.serviceWorker .register(swUrl) .then((registration) => { @@ -62,14 +65,14 @@ function registerValidSW(swUrl, config) { return; } installingWorker.onstatechange = () => { - if (installingWorker.state === 'installed') { + if (installingWorker.state === "installed") { if (navigator.serviceWorker.controller) { // At this point, the updated precached content has been fetched, // but the previous service worker will still serve the older // content until all client tabs are closed. console.log( - 'New content is available and will be used when all ' + - 'tabs for this page are closed. See https://cra.link/PWA.' + "New content is available and will be used when all " + + "tabs for this page are closed. See https://cra.link/PWA." ); // Execute callback @@ -80,7 +83,7 @@ function registerValidSW(swUrl, config) { // At this point, everything has been precached. // It's the perfect time to display a // "Content is cached for offline use." message. - console.log('Content is cached for offline use.'); + console.log("Content is cached for offline use."); // Execute callback if (config && config.onSuccess) { @@ -92,21 +95,21 @@ function registerValidSW(swUrl, config) { }; }) .catch((error) => { - console.error('Error during service worker registration:', error); + console.error("Error during service worker registration:", error); }); } function checkValidServiceWorker(swUrl, config) { // Check if the service worker can be found. If it can't reload the page. fetch(swUrl, { - headers: { 'Service-Worker': 'script' }, + headers: { "Service-Worker": "script" }, }) .then((response) => { // Ensure service worker exists, and that we really are getting a JS file. - const contentType = response.headers.get('content-type'); + const contentType = response.headers.get("content-type"); if ( response.status === 404 || - (contentType != null && contentType.indexOf('javascript') === -1) + (contentType != null && contentType.indexOf("javascript") === -1) ) { // No service worker found. Probably a different app. Reload the page. navigator.serviceWorker.ready.then((registration) => { @@ -120,12 +123,14 @@ function checkValidServiceWorker(swUrl, config) { } }) .catch(() => { - console.log('No internet connection found. App is running in offline mode.'); + console.log( + "No internet connection found. App is running in offline mode." + ); }); } export function unregister() { - if ('serviceWorker' in navigator) { + if ("serviceWorker" in navigator) { navigator.serviceWorker.ready .then((registration) => { registration.unregister();