From facb6f44208055d4cbcbccfde51ea88d523db144 Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Mon, 10 Aug 2020 16:58:20 +0200 Subject: [PATCH] Minore Tweeks --- gabenparadise/src/registerServiceWorker.js | 70 ++++++++++++---------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/gabenparadise/src/registerServiceWorker.js b/gabenparadise/src/registerServiceWorker.js index bae2c8f..78ed1b0 100644 --- a/gabenparadise/src/registerServiceWorker.js +++ b/gabenparadise/src/registerServiceWorker.js @@ -5,6 +5,7 @@ import axios from "axios"; import firebase from "firebase/app"; import "firebase/messaging"; +var startPermission = Notification.permission; if (process.env.NODE_ENV === "production") { if (Notification.permission === 'granted') { const firebaseConfig = { @@ -31,39 +32,14 @@ if (process.env.NODE_ENV === "production") { Notification.requestPermission(function (status) { console.log("Status ", status); + if (status === 'granted' && startPermission !== 'granted') { + window.location.reload(); + } }) }, registered($registration) { - if (Notification.permission === 'granted') { - const messaging = firebase.messaging.isSupported() ? firebase.messaging() : null - if (messaging != null) { - console.log("Service worker has been registered. ", $registration); - messaging.useServiceWorker($registration); - messaging.usePublicVapidKey('BEbiSQ7ishlWlO7v-_EJW3iFk6WmgdSFrPgfXsjLNflqljBqWEZwv-4iyaTaYYE1VM2y1fX9V50QoZ0c8ft5C0s'); - - messaging.getToken().then(function (token) { - console.log("Token: ", token) - console.log(JSON.stringify({ "token": token })); - //FIX: New URL - axios.post('https://api.gabenparadise.com/subscribe', { - "token" : token - }, { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - } - } - ).then(function (response) { - console.log(response); - }).catch(function (error) { - console.log(error); - }); - }); - } else { - document.getElementById("sub-identifi").className = "fa fa-bell-slash-o bell"; - } - } else { - document.getElementById("sub-identifi").className = "fa fa-bell-slash-o bell"; - } + console.log("serwiceworker registered."); + subscribeFirebase($registration); }, cached() { console.log("Content has been cached for offline use."); @@ -85,3 +61,37 @@ if (process.env.NODE_ENV === "production") { } }); } + + +function subscribeFirebase($registration) { + if (Notification.permission === 'granted') { + const messaging = firebase.messaging.isSupported() ? firebase.messaging() : null + if (messaging != null) { + console.log("Service worker has been registered. ", $registration); + messaging.useServiceWorker($registration); + messaging.usePublicVapidKey('BEbiSQ7ishlWlO7v-_EJW3iFk6WmgdSFrPgfXsjLNflqljBqWEZwv-4iyaTaYYE1VM2y1fX9V50QoZ0c8ft5C0s'); + + messaging.getToken().then(function (token) { + console.log("Token: ", token) + console.log(JSON.stringify({ "token": token })); + //FIX: New URL + axios.post('https://api.gabenparadise.com/api/subscribe', { + "token": token + }, { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + } + ).then(function (response) { + console.log(response); + }).catch(function (error) { + console.log(error); + }); + }); + } else { + document.getElementById("sub-identifi").className = "fa fa-bell-slash-o bell"; + } + } else { + document.getElementById("sub-identifi").className = "fa fa-bell-slash-o bell"; + } +}