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) && REQUEST_SCHEME == 'https'; if (NOTIFICATIONS_ENABLED ) { navigator.serviceWorker.register('/system/modules/com.tfsla.diario.base/templates/webservices/notifications/service-worker.jsp').then(function(serviceWorkerRegistration) { console.log("navigator.serviceWorker.register() promise"); serviceWorkerRegistration.pushManager.getSubscription().then(function(subscription) { console.log("serviceWorkerRegistration.pushManager.getSubscription() promise"); 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() { 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.jsp').then(function(serviceWorkerRegistration) { serviceWorkerRegistration.pushManager.getSubscription().then(function(subscription) { if(!subscription) subscribe(serviceWorkerRegistration.pushManager); else showId(subscription); }); }); } else { //alert("service workers not supported"); return; } navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { serviceWorkerRegistration.pushManager.getSubscription().then(function(subscription) { if (!subscription) { subscribe(serviceWorkerRegistration.pushManager); } else { showId(subscription) } }) .catch(function(e) { var evt = document.createEvent("CustomEvent"); evt.initCustomEvent("NotificationsLoaded", true, true, {supported: NOTIFICATIONS_ENABLED, error: e}); window.dispatchEvent(evt); }); }); } function subscribe(pushManager) { pushManager.subscribe({ userVisibleOnly: true }).then(function(pushSubscription) { showId(pushSubscription); }); } //Registrar en el server y disparar evento function showId(subscription) { 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, { 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); }