const publicKey='BBfEJfOKDgKRvDnJHqGDdFElThShHKZfRYmch-HY2nlsvz6qpIKzc7KAARsKID6zdE_bC25orr6j_6MLtlFxw74'; let isSubscribed = false; let swRegistration = null; function urlB64ToUint8Array(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; } function updateSubscriptionOnServer(subscription) { if(subscription !=null){ const key = subscription.getKey('p256dh'); const token = subscription.getKey('auth'); const contentEncoding = (PushManager.supportedContentEncodings || ['aesgcm'])[0]; if('fetch' in window){ return fetch('ajax/webpush_register.php', { method: 'POST', body: JSON.stringify({ endpoint: subscription.endpoint, publicKey: key ? btoa(String.fromCharCode.apply(null, new Uint8Array(key))) : null, authToken: token ? btoa(String.fromCharCode.apply(null, new Uint8Array(token))) : null, contentEncoding, }), }).then(() => subscription); }else{ var http = new XMLHttpRequest(); var url = 'ajax/webpush_register.php'; http.open('POST', url, true); //Send the proper header information along with the request http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); http.onreadystatechange = function() {//Call a function when the state changes. if(http.readyState == 4 && http.status == 200) { alert(http.responseText); } } http.send(JSON.stringify({ endpoint: subscription.endpoint, publicKey: key ? btoa(String.fromCharCode.apply(null, new Uint8Array(key))) : null, authToken: token ? btoa(String.fromCharCode.apply(null, new Uint8Array(token))) : null, contentEncoding: contentEncoding, })); } }else{ if('fetch' in window){ return fetch('ajax/webpush_register.php', { method: 'POST', body: JSON.stringify({ endpoint: null }), }).then(() => subscription); }else{ var http = new XMLHttpRequest(); var url = 'ajax/webpush_register.php'; http.open('POST', url, true); //Send the proper header information along with the request http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); http.onreadystatechange = function() {//Call a function when the state changes. if(http.readyState == 4 && http.status == 200) { alert(http.responseText); } } http.send(JSON.stringify({ endpoint: subscription.endpoint, publicKey: key ? btoa(String.fromCharCode.apply(null, new Uint8Array(key))) : null, authToken: token ? btoa(String.fromCharCode.apply(null, new Uint8Array(token))) : null, contentEncoding, })); } } } function subscribeUser() { const applicationServerKey = urlB64ToUint8Array(publicKey); swRegistration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: applicationServerKey }) .then(function(subscription) { updateSubscriptionOnServer(subscription); isSubscribed = true; }) .catch(function(err) { console.log('Failed to subscribe the user: ', err.message); unsubscribeUser(); }); } function unsubscribeUser() { swRegistration.pushManager.getSubscription() .then(function(subscription) { if (subscription) { return subscription.unsubscribe(); } }) .catch(function(error) { console.log('Error unsubscribing', error); }) .then(function() { updateSubscriptionOnServer(null); isSubscribed = false; }); }if ('serviceWorker' in navigator && 'PushManager' in window) { navigator.serviceWorker.register('/serviceworker.js') .then(function(swReg) { swRegistration = swReg; }) }