const onesignalAppID = "3b063b5e-1717-48ec-9156-56006fe3c0c2"; const thisAppID = 119; const defaultNotificationIcon = "https://mobile.bizeff.online"; var useragentid = null; var OneSignal = window.OneSignal || []; const appBaseURL = "https://proficos.bizeff.online"; let saveDeviceIdFlag = false; OneSignal.push(function() { OneSignal.SERVICE_WORKER_PARAM = { scope: "/" }; OneSignal.init({ appId: onesignalAppID, autoResubscribe: true, notifyButton: { enable: false, }, welcomeNotification: { disable: true }, // path: "https://mobile.bizeff.online/api/v1/widget/view/pwa/119/3305/" }); OneSignal.setDefaultNotificationUrl(appBaseURL); OneSignal.showNativePrompt(); }); setTimeout(resendSubscriberDetails, 500); OneSignal.push(function() { OneSignal.getUserId().then(function(userId) { if (userId) { OneSignal.push(["getNotificationPermission", function(permission) { if (permission == 'granted') { OneSignal.isPushNotificationsEnabled(function(isEnabled) { if (!isEnabled) { OneSignal.setSubscription(true).then(function() { saveDeviceId(userId); }); } else { saveDeviceId(userId); } }); } } ]); } }); }); OneSignal.push(function() { OneSignal.on('subscriptionChange', function(isSubscribed) { if (isSubscribed == true) { OneSignal.getUserId().then(function(userId) { return userId; }).then(function(userId) { saveDeviceId(userId); }); } }); }); var getPNDeviceID; getPNDeviceID = (apk=false)=>{ console.log('getPNDeviceID', apk); return new Promise((resolve,reject)=>{ if (!isIOS) { OneSignal.push(function() { OneSignal.getUserId().then(function(userId) { if (userId) { resolve(userId); } else { reject(null); } }); }); } else { let userId = localStorage.getItem('app_119_ios_notification_id'); if (userId) resolve(userId); else reject(null); } } ); } console.log('OneSignal blade test 1', "1"); function saveDeviceId(device_id) { console.log('saveDeviceId', device_id); localStorage.setItem('onesignalDeviceID', device_id); if (!saveDeviceIdFlag) { saveDeviceIdFlag = true; let data = { device_id: device_id, useragent: navigator.userAgent, "__ossg0080ckwsksco84owwck0kckkgowgg088ows8": "0c1ee06b531b0bf07e65d6cb2a4072ce" }; $.ajax({ type: 'POST', url: `https://proficos.bizeff.online/administrator/notification_device/save_device`, async: true, timeout: 600000, data: data, success: function(ret) { console.log('saveDeviceId', 'user device saved for notifications', ret); }, error: function(jqXHR, textStatus, errorThrown) { console.log('saveDeviceId', 'error', errorThrown); } }); } } function unsubscribeDevice() { console.log('unsubscribeDevice'); getPNDeviceID().then(deviceID=>{ console.log('unsubscribeDevice', deviceID); // $.ajax({ // type: 'POST', // url: `${admin_url}/api/notification_device/unsubscribe_device`, // beforeSend: function(xhr) { // xhr.setRequestHeader("X-Api-Key", admin_api_key); // }, // async: true, // timeout: 600000, // data: { // device_id: deviceID // }, // success: function(dataret) { // console.log('unsubscribeDevice', 'user unsubscribed'); // console.log(dataret); // /*localStorage.setItem('app_'+ thisAppID +'_notfications_tag', tag);*/ // }, // error: function(jqXHR, textStatus, errorThrown) { // console.log('unsubscribeDevice', 'error', errorThrown); // } // }); } ).catch(e=>{ console.log('unsubscribe', e); } ); } function resubscribeDevice() { console.log('resubscribeDevice'); getPNDeviceID().then(deviceID=>{ console.log('resubscribeDevice', deviceID); // $.ajax({ // type: 'POST', // url: `${admin_url}/api/notification_device/resubscribe_device`, // beforeSend: function(xhr) { // xhr.setRequestHeader("X-Api-Key", admin_api_key); // }, // async: true, // timeout: 600000, // data: { // device_id: deviceID // }, // success: function(dataret) { // console.log('resubscribeDevice', 'user unsubscribed'); // console.log(dataret); // }, // error: function(jqXHR, textStatus, errorThrown) { // console.log('resubscribeDevice', 'error', errorThrown); // } // }); } ).catch(e=>{ console.log('re-subscribed', e); } ) } function checkSubscribe() { return getPNDeviceID().then(deviceID=>{ console.log('checkSubscribe', deviceID); // return new Promise((resolve,reject)=>{ // $.ajax({ // type: 'GET', // url: `${admin_url}/api/notification_device/check_if_device_subscribed`, // async: true, // timeout: 600000, // data: { // device_id: deviceID, // "X-Api-Key": admin_api_key // }, // success: function(dataret) { // console.log('is User subscribed'); // console.log(dataret); // /*localStorage.setItem('app_'+ thisAppID +'_notfications_tag', tag);*/ // if (dataret.subscribed == "subscribed" || dataret.subscribed == "not_subscribed") // resolve(dataret.subscribed); // else // reject(); // }, // error: function(jqXHR, textStatus, errorThrown) { // reject(); // } // }); // } // ) } ) } function sendTag(tag) { getPNDeviceID().then(deviceID=>{ console.log('sendTag', tag, deviceID); // $.ajax({ // url: `${admin_url}/api/notification_device/update_user`, // type: 'POST', // beforeSend: function(xhr) { // xhr.setRequestHeader("X-Api-Key", admin_api_key); // }, // async: true, // timeout: 600000, // data: { // device_id: deviceID, // user: tag // }, // success: function(ret) { // console.log('sendTag success', ret); // if (ret.status) { // localStorage.setItem('app_' + thisAppID + '_notfications_tag', tag); // } // }, // error: function(jqXHR, textStatus, errorThrown) {} // }); } ).catch(e=>{ console.log('sendTag', e); } ) } function unSendTag() { getPNDeviceID().then(deviceID=>{ console.log('unSendTag', deviceID); // $.ajax({ // url: `${admin_url}/api/notification_device/update_user`, // type: 'POST', // beforeSend: function(xhr) { // xhr.setRequestHeader("X-Api-Key", admin_api_key); // }, // async: true, // timeout: 600000, // data: { // device_id: deviceID, // user: '' // }, // success: function(dataret) { // localStorage.removeItem('app_' + thisAppID + '_notfications_tag'); // localStorage.removeItem('app_' + thisAppID + '_notfications_role'); // }, // error: function(jqXHR, textStatus, errorThrown) {} // }); } ).catch(e=>{ console.log('sendTag', e); } ) } function sendRole(role) { getPNDeviceID().then(deviceID=>{ console.log('sendRole', deviceID); // let postURL = "https://mobile.bizeff.online/api/onesignal/pn_subscribe.php"; // $.ajax({ // type: 'POST', // url: postURL, // timeout: 120000, // data: { // appID: thisAppID, // device_id: deviceID, // method: 'sendRole', // role: role // }, // success: function(dataret) { // console.log('role updated'); // localStorage.setItem('app_119_notfications_role', role); // }, // error: function(jqXHR, textStatus, errorThrown) {} // }); } ).catch(e=>{ console.log('sendTag', e); } ); } function sendIOSemail(iosEmail) { console.log('sendIOSemail', iosEmail); // let device_id = localStorage.getItem('app_' + thisAppID + '_ios_notification_id'); // if (!device_id || device_id == '' || device_id == null || device_id.split('|')[0] != 'ios_device') { // device_id = 'ios_device|' + ApC.uuidv4(); // console.log('sendIOSemail', 'new device_id', device_id); // } else { // console.log('sendIOSemail', 'existing device_id', device_id); // } // if (!saveDeviceIdFlag && isIOS) { // saveDeviceIdFlag = true; // $.ajax({ // url: `${admin_url}/api/notification_device/set_ios_email`, // type: 'POST', // beforeSend: function(xhr) { // xhr.setRequestHeader("X-Api-Key", admin_api_key); // }, // async: true, // timeout: 600000, // data: { // device_id: device_id, // ios_email: iosEmail, // useragent: navigator.userAgent // }, // success: function(dataret) { // localStorage.setItem('app_' + thisAppID + '_ios_notification_id', device_id); // console.log('ios user saved for notifications'); // }, // error: function(jqXHR, textStatus, errorThrown) { // console.log("ios subscribe error"); // } // }); // } } function resendSubscriberDetails() { OneSignal.setSubscription(true); let userTag = localStorage.getItem('app_119_notfications_tag'); let userRole = localStorage.getItem('app_119_notfications_role'); if (userTag) sendTag(userTag); if (userRole) sendRole(userRole); } function pushNotification(config) { if (!config.icon) { config.icon = defaultNotificationIcon; } if (!config.image) { config.image = ''; } if (!config.navURL) { config.navURL = appBaseURL; } if (!config.data) { config.data = ''; } if (!config.datetime) { config.datetime = false; } // let postURL = "https://mobile.bizeff.online/api/onesignal/pn_push.php"; // $.ajax({ // type: 'POST', // url: postURL, // timeout: 120000, // data: { // appID: thisAppID, // oneSignalAppID: config.onesignalAppID ? config.onesignalAppID : onesignalAppID, // tags: config.tags, // title: config.title, // text: config.body, // icon: config.icon, // source: localStorage.getItem('onesignalDeviceID'), // image: config.image, // navURL: config.navURL, // datetime: config.datetime, // data: config.data, // appName: "Proficos" // } // }); } function localNotification(conf) { if (conf.tag) { conf.tag = "localNotif|" + conf.tag; } else { conf.tag = "localNotif|" + uuidv4(); } const localTag = conf.tag; navigator.serviceWorker.controller.postMessage({ notification: conf }); return new Promise(function(resolve, reject) { navigator.serviceWorker.addEventListener('message', event=>{ if (event.data.command == 'localNotif' && event.data.notification == localTag.split('|')[1]) { resolve(localTag.split('|')[1]); } } ); } ) } function listenForPush(config) { console.log('listenForPush', config); // return new Promise((resolve,reject)=>{ // navigator.serviceWorker.addEventListener('message', event=>{ // if (event.data.command == 'newNotification') { // if (config) { // if (config.dataField) { // try { // if (event.data.notification.data[config.dataField] == config.dataValue) { // resolve(event.data.notification); // } // } catch (e) {} // } // if (config.title && event.data.notification.title == config.title) { // resolve(event.data.notification); // } // } else { // resolve(event.data.notification); // } // } // } // ); // } // ) }