mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-03-12 20:34:46 +00:00
Addressing CommanderStorm code comments
This commit is contained in:
parent
0b3e10e703
commit
af301cd3f2
5 changed files with 37 additions and 43 deletions
|
@ -13,10 +13,9 @@ class Webpush extends NotificationProvider {
|
||||||
const okMsg = "Sent Successfully.";
|
const okMsg = "Sent Successfully.";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Get VAPID keys from settings
|
|
||||||
const publicVapidKey = await setting("webpushPublicVapidKey");
|
const publicVapidKey = await setting("webpushPublicVapidKey");
|
||||||
const privateVapidKey = await setting("webpushPrivateVapidKey");
|
const privateVapidKey = await setting("webpushPrivateVapidKey");
|
||||||
// Set Vapid keys in web-push helper lib
|
|
||||||
webpush.setVapidDetails("https://github.com/louislam/uptime-kuma", publicVapidKey, privateVapidKey);
|
webpush.setVapidDetails("https://github.com/louislam/uptime-kuma", publicVapidKey, privateVapidKey);
|
||||||
|
|
||||||
if (heartbeatJSON === null && monitorJSON === null) {
|
if (heartbeatJSON === null && monitorJSON === null) {
|
||||||
|
@ -25,7 +24,7 @@ class Webpush extends NotificationProvider {
|
||||||
title: "TEST",
|
title: "TEST",
|
||||||
body: "Test Alert - " + msg
|
body: "Test Alert - " + msg
|
||||||
});
|
});
|
||||||
//send push notification using web-push lib
|
|
||||||
await webpush.sendNotification(notification.subscription, data);
|
await webpush.sendNotification(notification.subscription, data);
|
||||||
|
|
||||||
return okMsg;
|
return okMsg;
|
||||||
|
@ -39,7 +38,7 @@ class Webpush extends NotificationProvider {
|
||||||
title: title,
|
title: title,
|
||||||
body: `${heartbeatJSON["status"] === UP ? up : down}`
|
body: `${heartbeatJSON["status"] === UP ? up : down}`
|
||||||
});
|
});
|
||||||
//send push notification using web-push lib
|
|
||||||
await webpush.sendNotification(notification.subscription, data);
|
await webpush.sendNotification(notification.subscription, data);
|
||||||
|
|
||||||
return okMsg;
|
return okMsg;
|
||||||
|
|
|
@ -1495,7 +1495,7 @@ let needSetup = false;
|
||||||
let publicVapidKey = await Settings.get("webpushPublicVapidKey");
|
let publicVapidKey = await Settings.get("webpushPublicVapidKey");
|
||||||
|
|
||||||
if (!publicVapidKey) {
|
if (!publicVapidKey) {
|
||||||
console.debug("Generating new VAPID keys");
|
log.debug("webpush", "Generating new VAPID keys");
|
||||||
const vapidKeys = webpush.generateVAPIDKeys();
|
const vapidKeys = webpush.generateVAPIDKeys();
|
||||||
|
|
||||||
await Settings.set("webpushPublicVapidKey", vapidKeys.publicKey);
|
await Settings.set("webpushPublicVapidKey", vapidKeys.publicKey);
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
type="button"
|
type="button"
|
||||||
:class="[
|
:class="[
|
||||||
'btn',
|
'btn',
|
||||||
canRegister ? 'btn-primary' : 'btn-danger'
|
browserSupportsServiceWorkers ? 'btn-primary' : 'btn-danger'
|
||||||
]"
|
]"
|
||||||
:disabled="!btnEnabled"
|
:disabled="!btnEnabled"
|
||||||
@click="registerWebpush"
|
@click="registerWebpush"
|
||||||
|
@ -14,8 +14,8 @@
|
||||||
{{ btnText }}
|
{{ btnText }}
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
<div class="mb-3 form-text">
|
<div class="form-text">
|
||||||
<a href="TODO" target="_blank">{{ $t("documentationOf", ["Webpush"]) }}</a>
|
{{ $t("Webpush Helptext") }}
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
@ -23,31 +23,26 @@
|
||||||
export default {
|
export default {
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
//store subscription info
|
|
||||||
btnEnabled: false,
|
btnEnabled: false,
|
||||||
btnText: "",
|
btnText: "",
|
||||||
processing: false,
|
processing: false,
|
||||||
//determines if browser supports service worker
|
browserSupportsServiceWorkers: false,
|
||||||
canRegister: false,
|
|
||||||
//store public vapid key
|
|
||||||
publicVapidKey: null,
|
publicVapidKey: null,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
// if already subscribed
|
|
||||||
if (this.$parent.notification.subscription) {
|
if (this.$parent.notification.subscription) {
|
||||||
this.btnEnabled = false;
|
this.btnEnabled = false;
|
||||||
this.canRegister = true;
|
this.browserSupportsServiceWorkers = true;
|
||||||
this.btnText = "Notifications Enabled";
|
this.btnText = this.$t("Notifications Enabled");
|
||||||
} else { //not subscribed
|
} else {
|
||||||
//check if browser supports service worker
|
|
||||||
if (("serviceWorker" in navigator)) {
|
if (("serviceWorker" in navigator)) {
|
||||||
this.btnText = "Allow Notifications";
|
this.btnText = this.$t("Allow Notifications");
|
||||||
this.canRegister = true;
|
this.browserSupportsServiceWorkers = true;
|
||||||
this.btnEnabled = true;
|
this.btnEnabled = true;
|
||||||
} else { //browser does not support service worker
|
} else {
|
||||||
this.btnText = "Browser not supported";
|
this.btnText = this.$t("Browser not supported");
|
||||||
this.canRegister = false;
|
this.browserSupportsServiceWorkers = false;
|
||||||
this.btnEnabled = false;
|
this.btnEnabled = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,40 +52,37 @@ export default {
|
||||||
this.processing = true;
|
this.processing = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Get the VAPID public key from the server
|
|
||||||
const publicKey = await new Promise((resolve, reject) => {
|
const publicKey = await new Promise((resolve, reject) => {
|
||||||
this.$root.getSocket().emit("getWebpushVapidPublicKey", (resp) => {
|
this.$root.getSocket().emit("getWebpushVapidPublicKey", (resp) => {
|
||||||
if (!resp.ok) {
|
if (!resp.ok) {
|
||||||
reject(new Error(resp.msg));
|
reject(new Error(resp.msg));
|
||||||
}
|
}
|
||||||
|
console.log(resp.msg);
|
||||||
resolve(resp.msg);
|
resolve(resp.msg);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
//request permission to send notifications
|
|
||||||
const permission = await Notification.requestPermission();
|
const permission = await Notification.requestPermission();
|
||||||
if (permission !== "granted") {
|
if (permission !== "granted") {
|
||||||
this.$root.toastRes({
|
this.$root.toastRes({
|
||||||
ok: false,
|
ok: false,
|
||||||
msg: "Unable to get permission to notify.",
|
msg: this.$t("Unable to get permission to notify"),
|
||||||
});
|
});
|
||||||
this.processing = false;
|
this.processing = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//get service worker registration
|
|
||||||
const registration = await navigator.serviceWorker.ready;
|
const registration = await navigator.serviceWorker.ready;
|
||||||
//subscribe to push notifications
|
|
||||||
const subscription = await registration.pushManager.subscribe({
|
const subscription = await registration.pushManager.subscribe({
|
||||||
userVisibleOnly: true,
|
userVisibleOnly: true,
|
||||||
applicationServerKey: publicKey,
|
applicationServerKey: publicKey,
|
||||||
});
|
});
|
||||||
|
|
||||||
//store subscription info and update button
|
|
||||||
this.$parent.notification.subscription = subscription;
|
this.$parent.notification.subscription = subscription;
|
||||||
this.btnEnabled = false;
|
this.btnEnabled = false;
|
||||||
this.canRegister = true;
|
this.browserSupportsServiceWorkers = true;
|
||||||
this.btnText = "Notifications Enabled";
|
this.btnText = this.$t("Notifications Enabled");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Subscription failed:", error);
|
console.error("Subscription failed:", error);
|
||||||
this.$root.toastRes({
|
this.$root.toastRes({
|
||||||
|
|
|
@ -1051,5 +1051,10 @@
|
||||||
"RabbitMQ Password": "RabbitMQ Password",
|
"RabbitMQ Password": "RabbitMQ Password",
|
||||||
"rabbitmqHelpText": "To use the monitor, you will need to enable the Management Plugin in your RabbitMQ setup. For more information, please consult the {rabitmq_documentation}.",
|
"rabbitmqHelpText": "To use the monitor, you will need to enable the Management Plugin in your RabbitMQ setup. For more information, please consult the {rabitmq_documentation}.",
|
||||||
"SendGrid API Key": "SendGrid API Key",
|
"SendGrid API Key": "SendGrid API Key",
|
||||||
"Separate multiple email addresses with commas": "Separate multiple email addresses with commas"
|
"Separate multiple email addresses with commas": "Separate multiple email addresses with commas",
|
||||||
|
"Notifications Enabled": "Notifications Enabled",
|
||||||
|
"Allow Notifications": "Allow Notifications",
|
||||||
|
"Browser not supported": "Browser not supported",
|
||||||
|
"Unable to get permission to notify": "Unable to get permission to notify (request either denied or ignored).",
|
||||||
|
"Webpush Helptext": "Web push only works with SSL (HTTPS) connections. For iOS devices, webpage must be added to homescreen beforehand."
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,21 +5,19 @@ precacheAndRoute(self.__WB_MANIFEST)
|
||||||
|
|
||||||
// Receive push notifications
|
// Receive push notifications
|
||||||
self.addEventListener('push', function (event) {
|
self.addEventListener('push', function (event) {
|
||||||
|
if (self.Notification?.permission !== 'granted') {
|
||||||
if (!(
|
console.error("Notifications aren't supported or permission not granted!");
|
||||||
self.Notification &&
|
|
||||||
self.Notification.permission === 'granted'
|
|
||||||
)) {
|
|
||||||
//notifications aren't supported or permission not granted!
|
|
||||||
console.log("Notifications aren't supported or permission not granted!");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.data) {
|
if (event.data) {
|
||||||
console.log(event);
|
|
||||||
let message = event.data.json();
|
let message = event.data.json();
|
||||||
self.registration.showNotification(message.title, {
|
try {
|
||||||
body: message.body,
|
self.registration.showNotification(message.title, {
|
||||||
});
|
body: message.body,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to show notification:', error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue