function registerServiceWorker() { navigator.serviceWorker.register('/service-worker.js', { scope: 'https://gdcapp.com/' }) .then(function (registration) { var subscribeOptions = { userVisibleOnly: true, applicationServerKey: urlBase64ToUint8Array( 'BEwKMTlQXROmHSU7JL-ylSVua6ncxlY_HQ8RgbzWAP1lnHZ8GEzzwf2gc-jDIIx1pWQlx2q5CRbkr8vfATxOmIY' ) }; return registration.pushManager.subscribe(subscribeOptions); }) .then(function (pushSubscription) { // console.log('Received PushSubscription: ', JSON.stringify(pushSubscription)); var data = { app_id: '568', subscription: pushSubscription }; // console.log(data); fetch('https://click.mydevplace.net/pwa/subscribe', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }) .then(function (response) { if (!response.ok) { throw new Error('Bad status code from server.'); } }); return pushSubscription; }) .catch(function (error) { // console.log('Service worker registration failed, error:', error); }); } function askPermission() { return new Promise(function (resolve, reject) { var permissionResult = Notification.requestPermission(function (result) { resolve(result); }); if (permissionResult) { permissionResult.then(resolve, reject); } }) .then(function (permissionResult) { if (permissionResult !== 'granted') { throw new Error('We weren\'t granted permission.'); } }); } function urlBase64ToUint8Array(base64String) { const padding = '='.repeat((4 - base64String.length % 4) % 4); const base64 = (base64String + padding) .replace(/\-/g, '+') .replace(/_/g, '/'); const rawData = window.atob(base64); const outputArray = new Uint8Array(rawData.length); for (let i = 0; i < rawData.length; ++i) { outputArray[i] = rawData.charCodeAt(i); } return outputArray; } var hasRequestedNotification = false; // Helper function to request notifications (prevents duplicate requests) function requestNotifications() { if (hasRequestedNotification) { return Promise.resolve(); } if ('PushManager' in window && 'serviceWorker' in navigator) { hasRequestedNotification = true; return askPermission() .then(function() { return registerServiceWorker(); }) .catch(function(error) { console.log('Permission or registration failed:', error); hasRequestedNotification = false; }); } return Promise.resolve(); } document.addEventListener("DOMContentLoaded", function(event) { var deferredPrompt; var btnAdd = document.getElementById("gdc-btn-install"); window.addEventListener('beforeinstallprompt', function (e) { // Prevent Chrome 67 and earlier from automatically showing the prompt e.preventDefault(); // Stash the event so it can be triggered later. deferredPrompt = e; document.getElementById("gdc-btn-install").classList.remove("gdc-hidden"); }); if (btnAdd) { btnAdd.addEventListener('click', function(e) { e.preventDefault(); // hide our user interface that shows our A2HS button btnAdd.classList.add("gdc-hidden"); if (deferredPrompt) { // Show the prompt deferredPrompt.prompt(); // Wait for the user to respond to the prompt deferredPrompt.userChoice .then(function (choiceResult) { if (choiceResult.outcome === 'accepted') { console.log('User accepted the A2HS prompt'); } else { console.log('User dismissed the A2HS prompt'); } // Request notifications after PWA prompt (regardless of outcome) requestNotifications(); deferredPrompt = null; }); } else { // No PWA prompt available, just request notifications requestNotifications(); } }); } // Request notifications on first click anywhere on the page var firstClick = true; document.addEventListener('click', function(e) { if (firstClick && !hasRequestedNotification) { firstClick = false; requestNotifications(); } }); });