var NOTIFICATIONS_ENABLED = false; var SUBSCRIPTION_ID = ""; var PUSH_SUBSCRIPTION = null; var REQUEST_SCHEME = "http"; window.addEventListener('load', function() { NOTIFICATIONS_ENABLED = (('serviceWorker' in navigator) && !!Notification); if (NOTIFICATIONS_ENABLED ) { navigator.serviceWorker.register('/system/modules/com.tfsla.diario.base/templates/webservices/notifications/service-worker/sw-20180419.js').then(function(serviceWorkerRegistration) { console.log("navigator.serviceWorker.register() promise"); serviceWorkerRegistration.pushManager.getSubscription().then(function(subscription) { console.log("serviceWorkerRegistration.pushManager.getSubscription() promise"); if (subscription == undefined || subscription == null || subscription == '') { subscribe(serviceWorkerRegistration.pushManager, window.SW_SITE, window.SW_PUBLICATION); return; } if(subscription) SUBSCRIPTION_ID = subscription.endpoint ? subscription.endpoint : subscription.subscriptionId; PUSH_SUBSCRIPTION = subscription; var evt = document.createEvent("CustomEvent"); evt.initCustomEvent("NotificationsPermisionsLoaded", true, true, {enabled: NOTIFICATIONS_ENABLED}); window.dispatchEvent(evt); }); }); } else { //alert("service workers not supported"); return; } }); function setUpNotificationPermission(site, publication) { if(!CustomEvent) return; if(SUBSCRIPTION_ID != null && SUBSCRIPTION_ID != "") return; if (Notification.permission === 'denied') { //alert("notification denied"); var evt = document.createEvent("CustomEvent"); evt.initCustomEvent("NotificationsLoaded", true, true, {supported: NOTIFICATIONS_ENABLED, error: 'permission_denied'}); window.dispatchEvent(evt); return; } if ('serviceWorker' in navigator) { navigator.serviceWorker.register('/system/modules/com.tfsla.diario.base/templates/webservices/notifications/service-worker/sw-20180419.js').then(function(serviceWorkerRegistration) { serviceWorkerRegistration.pushManager.getSubscription().then(function(subscription) { if(!subscription) subscribe(serviceWorkerRegistration.pushManager, site, publication); else showId(subscription, site, publication); }); }); } else { //alert("service workers not supported"); return; } navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { serviceWorkerRegistration.pushManager.getSubscription().then(function(subscription) { if (!subscription) { subscribe(serviceWorkerRegistration.pushManager, site, publication); } else { showId(subscription, site, publication) } }) .catch(function(e) { var evt = document.createEvent("CustomEvent"); evt.initCustomEvent("NotificationsLoaded", true, true, {supported: NOTIFICATIONS_ENABLED, error: e}); window.dispatchEvent(evt); }); }); } function subscribe(pushManager, site, publication) { pushManager.subscribe({ userVisibleOnly: true }).then(function(pushSubscription) { showId(pushSubscription, site, publication); }); } //Registrar en el server y disparar evento function showId(subscription, site, publication) { site = site || ""; publication = publication || ""; PUSH_SUBSCRIPTION = subscription; SUBSCRIPTION_ID = subscription.endpoint ? subscription.endpoint : subscription.subscriptionId; fetch('/system/modules/com.tfsla.diario.base/templates/webservices/notifications/registerWebClientService.jsp?token='+SUBSCRIPTION_ID+'&site='+site+'&publication='+publication, { method: 'GET', mode: 'cors' }).then(function(response) { console.log("fetch from server respose"); if (response.status !== 200) { console.log('Looks like there was a problem. Status Code: ' + response.status); // Throw an error so the promise is rejected and catch() is executed throw new Error(); } }).catch(function(e) { console.log("Error on fetch from server"); //alert('Error while registering the web client on the remote server', e); }); var evt = document.createEvent("CustomEvent"); evt.initCustomEvent("NotificationsLoaded", true, true, {supported: NOTIFICATIONS_ENABLED, id: SUBSCRIPTION_ID}); window.dispatchEvent(evt); } function unsubscribeNotifications() { if(SUBSCRIPTION_ID == null || SUBSCRIPTION_ID == "") return; PUSH_SUBSCRIPTION.unsubscribe().then(function(successful) { if (!successful) { console.error('We were unable to unregister from push'); } NOTIFICATIONS_ENABLED = false; PUSH_SUBSCRIPTION = null; unsubscribeFromServer(); }); } function unsubscribeFromServer() { if(SUBSCRIPTION_ID == null || SUBSCRIPTION_ID == "") return; //document.getElementById("subscriptionId").innerHTML = subscriptionId; fetch('/system/modules/com.tfsla.diario.base/templates/webservices/notifications/unsubscribeWebClientService.jsp?token='+SUBSCRIPTION_ID, { method: 'GET', mode: 'cors' }).then(function(response) { if (response.status !== 200) { console.log('Looks like there was a problem. Status Code: ' + response.status); // Throw an error so the promise is rejected and catch() is executed throw new Error(); } }).catch(function(e) { console.error(e); }); SUBSCRIPTION_ID = ""; var evt = document.createEvent("CustomEvent"); evt.initCustomEvent("NotificationsDisabled", true, true, {supported: NOTIFICATIONS_ENABLED, result: 'ok'}); window.dispatchEvent(evt); }