mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2025-11-08 21:24:53 -05:00
More web push progress.
This commit is contained in:
@@ -354,7 +354,6 @@
|
||||
<p class="mL">
|
||||
<span id="managePhoneNumber2" style="display:none"><a href=# onclick="return account_managePhone()">Manage phone number</a><br /></span>
|
||||
<span id="verifyEmailId" style="display:none"><a href=# onclick="return account_showVerifyEmail()">Verify email</a><br /></span>
|
||||
<span id="accountEnablePushNotificationsSpan" style="display:none"><a href=# onclick="return account_enablePushNotifications()">Enable push notifications</a><br /></span>
|
||||
<span id="accountEnableNotificationsSpan" style="display:none"><a href=# onclick="return account_enableNotifications()">Enable web notifications</a><br /></span>
|
||||
<a href=# onclick="return account_showLocalizationSettings()">Localization Settings</a><br />
|
||||
<a href=# onclick="return account_showAccountNotifySettings()">Notification Settings</a><br />
|
||||
@@ -1360,37 +1359,6 @@
|
||||
// Fix links if a loginKey is used
|
||||
if (urlargs.key) { Q('termsLinkFooter').href += '?key=' + urlargs.key; }
|
||||
|
||||
/*
|
||||
// Register the service worker
|
||||
if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker.addEventListener('message', function(event) {
|
||||
console.log("Client message from service worker: " + event);
|
||||
});
|
||||
|
||||
navigator.serviceWorker.register('serviceworker.js')
|
||||
.then(function(reg) {
|
||||
// Registration worked
|
||||
console.log('Registration succeeded. Scope is ' + reg.scope);
|
||||
|
||||
reg.pushManager.getSubscription().then(function(sub) {
|
||||
if (sub === null) {
|
||||
// Update UI to ask user to register for Push
|
||||
console.log('Not subscribed to push service.');
|
||||
} else {
|
||||
// We have a subscription, update the database
|
||||
console.log('Subscription object: ', sub);
|
||||
|
||||
navigator.serviceWorker.controller.postMessage({type: 'sync', text: 'Hello!'})
|
||||
}
|
||||
});
|
||||
|
||||
}).catch((error) => {
|
||||
// Registration failed
|
||||
console.log('Registration failed with ' + error);
|
||||
});
|
||||
}
|
||||
*/
|
||||
|
||||
// Check if we are in debug mode
|
||||
args = parseUriArgs();
|
||||
if (args.key && (isAlphaNumeric(args.key) == false)) { delete args.key; }
|
||||
@@ -2098,7 +2066,7 @@
|
||||
serverinfo = message.serverinfo;
|
||||
if (serverinfo.timeout) { setInterval(checkIdleSessionTimeout, 10000); checkIdleSessionTimeout(); }
|
||||
if (debugmode == 1) { console.log('Server time: ', printDateTime(new Date(serverinfo.serverTime))); }
|
||||
//console.log(serverinfo);
|
||||
setupServiceWorker();
|
||||
break;
|
||||
}
|
||||
case 'userinfo': {
|
||||
@@ -9692,13 +9660,8 @@
|
||||
}
|
||||
}
|
||||
|
||||
function account_enablePushNotifications() {
|
||||
if ((serverinfo == null) || (serverinfo.vapidpublickey == null)) return;
|
||||
return false;
|
||||
}
|
||||
|
||||
function account_enableNotifications() {
|
||||
if (Notification) { Notification.requestPermission().then(function (permission) { QV('accountEnableNotificationsSpan', permission != 'granted'); }); }
|
||||
if (Notification) { Notification.requestPermission().then(function (permission) { QV('accountEnableNotificationsSpan', permission != 'granted'); setupServiceWorker(); }); }
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -13873,6 +13836,30 @@
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// SERVICE WORKER
|
||||
//
|
||||
|
||||
function setupServiceWorker() {
|
||||
if ((typeof serverinfo.vapidpublickey != 'string') || (Notification == null) || (Notification.permission != 'granted')) return;
|
||||
// Register the service worker
|
||||
if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker.register('serviceworker.js')
|
||||
.then(function(reg) {
|
||||
// Subscribe to push notifications
|
||||
navigator.serviceWorker.ready.then(function(reg) {
|
||||
reg.pushManager.subscribe({ applicationServerKey: urlBase64ToUint8Array(serverinfo.vapidpublickey), userVisibleOnly: true }).then(function(sub) {
|
||||
meshserver.send({ action: 'webpush', sub: sub });
|
||||
}).catch(function(e) { console.error('Unable to subscribe to push', e); });
|
||||
})
|
||||
}).catch(function(error) {
|
||||
// Registration failed
|
||||
console.log('Registration failed', error);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// POPUP DIALOG
|
||||
//
|
||||
@@ -14081,7 +14068,6 @@
|
||||
|
||||
// Setup web notifications
|
||||
if ((x == 2) && Notification) { QV('accountEnableNotificationsSpan', Notification.permission != 'granted'); }
|
||||
//QV('accountEnablePushNotificationsSpan', true);
|
||||
|
||||
// Fetch the server timeline stats if needed
|
||||
if ((x == 40) && (serverTimelineStats == null)) { refreshServerTimelineStats(); }
|
||||
@@ -14416,6 +14402,16 @@
|
||||
if (typeof Symbol!=='undefined')TextEncoder.prototype[Symbol.toStringTag]='TextEncoder';
|
||||
}
|
||||
|
||||
// Used to convert Base64 public VAPID key to bytearray.
|
||||
function urlBase64ToUint8Array(base64String) {
|
||||
const padding = '='.repeat((4 - base64String.length % 4) % 4);
|
||||
const base64 = (base64String + padding).replace(/-/g, '+').replace(/_/g, '/');
|
||||
const rawData = atob(base64);
|
||||
const outputArray = new Uint8Array(rawData.length);
|
||||
for (let i = 0; i < rawData.length; ++i) { outputArray[i] = rawData.charCodeAt(i); }
|
||||
return outputArray;
|
||||
}
|
||||
|
||||
function joinPaths() { var x = []; for (var i in arguments) { var w = arguments[i]; if ((w != null) && (w != '')) { while (w.endsWith('/') || w.endsWith('\\')) { w = w.substring(0, w.length - 1); } while (w.startsWith('/') || w.startsWith('\\')) { w = w.substring(1); } x.push(w); } } return x.join('/'); }
|
||||
function putstore(name, val) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user