/* * * Push Notifications Iranotif * https://iranotif.com * */ /* eslint-env browser, es6 */ 'use strict'; const applicationServerPublicKey = 'BEKiWeXvp79LaGueyFKhBz7XvyRLGlNt7lF6G1ScOkzYXLuLq4_DOrK4hh_LjiBQtydkVblxyYM-eqCcjHKah_U'; const pushButton = document.querySelector('.js-push-class'); let isSubscribed = false; let swRegistration = null; var csrftoken = '29642938811'; var webtokenid = '20668bb65acb7a7'; 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 updateBtn() { if (Notification.permission === 'denied') { notif_status('denied'); // pushButton.textContent = 'ارسال نوتیفیکیشن بلاک شده است.'; pushButton.disabled = true; pushButton.classList.remove('subscribe-disabled'); pushButton.classList.remove('subscribe-enabled'); pushButton.classList.add('subscribe-blocked'); updateSubscriptionOnServer(null); return; } if (isSubscribed) { notif_status('isSubscribed'); // pushButton.textContent = 'غیر فعال سازی نوتیفیکیشن'; pushButton.classList.remove('subscribe-disabled'); pushButton.classList.remove('subscribe-blocked'); pushButton.classList.add('subscribe-enabled'); } else { notif_status('notSubscribed'); // pushButton.textContent = 'فعال سازی نوتیفیکیشن'; pushButton.classList.remove('subscribe-enabled'); pushButton.classList.remove('subscribe-blocked'); pushButton.classList.add('subscribe-disabled'); } pushButton.disabled = false; } function getCookie(cname) { let name = cname + "="; let decodedCookie = decodeURIComponent(document.cookie); let ca = decodedCookie.split(';'); for (let i = 0; i < ca.length; i++) { let c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ""; } function updateSubscriptionOnServer(subscription) { if (subscription) { let oldendpoint = getCookie("endpoint"); if (oldendpoint == "" || oldendpoint != JSON.stringify(subscription.endpoint)) { const d = new Date(); d.setTime(d.getTime() + (157680000000)); document.cookie = "endpoint=" + JSON.stringify(subscription.endpoint) + "; expires=" + d.toGMTString(); var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { } }; xhttp.open("POST", "https://notif.nilisoft.com/webpush/subscribe", true); xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); var uid = pushButton.dataset.userid; var gpid = pushButton.dataset.grouptoken; var datas = "_token=" + csrftoken + "&webtokenid=" + webtokenid + "&sbdata=" + JSON.stringify(subscription) + "&userid=" + uid + "&grouptoken=" + gpid+"&webview=0"; xhttp.send(datas); } } } function subscribeUser() { const applicationServerKey = urlB64ToUint8Array(applicationServerPublicKey); swRegistration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: applicationServerKey }) .then(function (subscription) { updateSubscriptionOnServer(subscription); isSubscribed = true; updateBtn(); }) .catch(function (err) { console.log('Failed to subscribe the user: ', err); updateBtn(); }); } function unsubscribeUser() { swRegistration.pushManager.getSubscription() .then(function (subscription) { if (subscription) { return subscription.unsubscribe(); } }) .catch(function (error) { }) .then(function () { updateSubscriptionOnServer(null); isSubscribed = false; updateBtn(); }); } function initializeUI() { pushButton.addEventListener('click', function () { pushButton.disabled = true; if (isSubscribed) { unsubscribeUser(); } else { subscribeUser(); } }); swRegistration.pushManager.getSubscription() .then(function (subscription) { isSubscribed = !(subscription === null); updateSubscriptionOnServer(subscription); if (isSubscribed) { } else { } updateBtn(); }); } if ('serviceWorker' in navigator && 'PushManager' in window) { navigator.serviceWorker.register('/sw.js') .then(function (swReg) { swRegistration = swReg; initializeUI(); // subscribeonload() }) .catch(function (error) { }); } else { // pushButton.textContent = 'مرورگر شما از این امکان پشتیبانی نمی کند.'; pushButton.classList.add('subscribe-notsupported'); notif_status('notsupported'); if ("Android" in window) { var uid = pushButton.dataset.userid; Android.subscribe_user(uid); } } function subscribeonload() { if (!isSubscribed) { subscribeUser(); } } function webview_subscribe(userid,firbaseid){ var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { } }; xhttp.open("POST", "https://notif.nilisoft.com/webpush/subscribe", true); xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); var uid = userid; var gpid = pushButton.dataset.grouptoken; var datas = "_token=" + csrftoken + "&webtokenid=" + webtokenid + "&sbdata=" + firbaseid + "&userid=" + uid + "&grouptoken=" + gpid+"&webview=1"; xhttp.send(datas); } function notif_status(status){ if (status=="notSubscribed"){ if( $('.push-notsubscribed').length ) { $('.push-notsubscribed').show(); } if( $('.push-issubscribed').length ) { $('.push-issubscribed').hide(); } if( $('.push-denied').length ) { $('.push-denied').hide(); } if( $('.push-notsupported').length ) { $('.push-notsupported').hide(); } } if (status=="isSubscribed"){ if( $('.push-notsubscribed').length ) { $('.push-notsubscribed').hide(); } if( $('.push-issubscribed').length ) { $('.push-issubscribed').show(); } if( $('.push-denied').length ) { $('.push-denied').hide(); } if( $('.push-notsupported').length ) { $('.push-notsupported').hide(); } } if (status=="denied"){ if( $('.push-notsubscribed').length ) { $('.push-notsubscribed').hide(); } if( $('.push-issubscribed').length ) { $('.push-issubscribed').hide(); } if( $('.push-denied').length ) { $('.push-denied').show(); } if( $('.push-notsupported').length ) { $('.push-notsupported').hide(); } } if (status=="notsupported"){ if( $('.push-notsubscribed').length ) { $('.push-notsubscribed').hide(); } if( $('.push-issubscribed').length ) { $('.push-issubscribed').hide(); } if( $('.push-denied').length ) { $('.push-denied').hide(); } if( $('.push-notsupported').length ) { $('.push-notsupported').show(); } } }