-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Add Webpush notification provider #5444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DoidoYo
wants to merge
13
commits into
louislam:master
Choose a base branch
from
DoidoYo:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
486a129
Add WebPush Notification Provider
DoidoYo 961368b
Webpush with VAPID generation
DoidoYo 1041812
Lint cleanup and comments
DoidoYo e66c178
Update package-lock.json
DoidoYo d9cf5d2
Update package.json
DoidoYo ca33835
Update package-lock.json
DoidoYo fbebad8
Update package-lock.json
DoidoYo feb6700
Update Webpush.vue
DoidoYo 0b3e10e
ESLint update
DoidoYo af301cd
Addressing CommanderStorm code comments
DoidoYo d179a46
Merge branch 'master' into master
CommanderStorm 43c3fda
Update server/server.js
CommanderStorm 2a1f0ce
Running NPM INSTALL as requested
DoidoYo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const NotificationProvider = require("./notification-provider"); | ||
const { UP } = require("../../src/util"); | ||
const webpush = require("web-push"); | ||
const { setting } = require("../util-server"); | ||
|
||
class Webpush extends NotificationProvider { | ||
name = "Webpush"; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { | ||
const okMsg = "Sent Successfully."; | ||
|
||
try { | ||
const publicVapidKey = await setting("webpushPublicVapidKey"); | ||
const privateVapidKey = await setting("webpushPrivateVapidKey"); | ||
|
||
webpush.setVapidDetails("https://github.com/louislam/uptime-kuma", publicVapidKey, privateVapidKey); | ||
|
||
if (heartbeatJSON === null && monitorJSON === null) { | ||
// Test message | ||
const data = JSON.stringify({ | ||
title: "TEST", | ||
body: "Test Alert - " + msg | ||
}); | ||
|
||
await webpush.sendNotification(notification.subscription, data); | ||
|
||
return okMsg; | ||
} | ||
|
||
const title = `Monitor ${heartbeatJSON["status"] === UP ? "UP" : "DOWN"}`; | ||
const down = "❌ " + monitorJSON["name"] + " is DOWN ❌"; | ||
const up = "✅ " + monitorJSON["name"] + " is UP ✅"; | ||
|
||
const data = JSON.stringify({ | ||
title: title, | ||
body: `${heartbeatJSON["status"] === UP ? up : down}` | ||
}); | ||
|
||
await webpush.sendNotification(notification.subscription, data); | ||
|
||
return okMsg; | ||
} catch (error) { | ||
this.throwGeneralAxiosError(error); | ||
} | ||
} | ||
} | ||
|
||
module.exports = Webpush; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<template> | ||
DoidoYo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<button | ||
class="mb-3" | ||
type="button" | ||
:class="[ | ||
'btn', | ||
browserSupportsServiceWorkers ? 'btn-primary' : 'btn-danger' | ||
]" | ||
:disabled="!btnEnabled" | ||
DoidoYo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@click="registerWebpush" | ||
> | ||
<div v-if="processing" class="spinner-border spinner-border-sm me-1"></div> | ||
<span v-else-if="$parent.notification.subscription" class="me-1">✓</span> | ||
{{ btnText }} | ||
</button> | ||
|
||
<div class="form-text"> | ||
{{ $t("Webpush Helptext") }} | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
data() { | ||
return { | ||
btnEnabled: false, | ||
btnText: "", | ||
processing: false, | ||
browserSupportsServiceWorkers: false, | ||
publicVapidKey: null, | ||
}; | ||
}, | ||
mounted() { | ||
if (this.$parent.notification.subscription) { | ||
this.btnEnabled = false; | ||
this.browserSupportsServiceWorkers = true; | ||
this.btnText = this.$t("Notifications Enabled"); | ||
} else { | ||
if (("serviceWorker" in navigator)) { | ||
this.btnText = this.$t("Allow Notifications"); | ||
this.browserSupportsServiceWorkers = true; | ||
this.btnEnabled = true; | ||
} else { | ||
this.btnText = this.$t("Browser not supported"); | ||
this.browserSupportsServiceWorkers = false; | ||
this.btnEnabled = false; | ||
} | ||
} | ||
}, | ||
methods: { | ||
async registerWebpush() { | ||
this.processing = true; | ||
|
||
try { | ||
const publicKey = await new Promise((resolve, reject) => { | ||
this.$root.getSocket().emit("getWebpushVapidPublicKey", (resp) => { | ||
if (!resp.ok) { | ||
reject(new Error(resp.msg)); | ||
} | ||
console.log(resp.msg); | ||
resolve(resp.msg); | ||
}); | ||
}); | ||
|
||
const permission = await Notification.requestPermission(); | ||
if (permission !== "granted") { | ||
this.$root.toastRes({ | ||
ok: false, | ||
msg: this.$t("Unable to get permission to notify"), | ||
}); | ||
this.processing = false; | ||
return; | ||
} | ||
|
||
const registration = await navigator.serviceWorker.ready; | ||
|
||
const subscription = await registration.pushManager.subscribe({ | ||
userVisibleOnly: true, | ||
applicationServerKey: publicKey, | ||
}); | ||
|
||
this.$parent.notification.subscription = subscription; | ||
this.btnEnabled = false; | ||
this.browserSupportsServiceWorkers = true; | ||
this.btnText = this.$t("Notifications Enabled"); | ||
} catch (error) { | ||
console.error("Subscription failed:", error); | ||
this.$root.toastRes({ | ||
ok: false, | ||
msg: error | ||
}); | ||
} finally { | ||
this.processing = false; | ||
} | ||
} | ||
}, | ||
}; | ||
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Needed per Vite PWA docs | ||
import { precacheAndRoute } from 'workbox-precaching' | ||
declare let self: ServiceWorkerGlobalScope | ||
precacheAndRoute(self.__WB_MANIFEST) | ||
|
||
// Receive push notifications | ||
self.addEventListener('push', function (event) { | ||
if (self.Notification?.permission !== 'granted') { | ||
console.error("Notifications aren't supported or permission not granted!"); | ||
return; | ||
} | ||
|
||
if (event.data) { | ||
let message = event.data.json(); | ||
try { | ||
self.registration.showNotification(message.title, { | ||
body: message.body, | ||
}); | ||
} catch (error) { | ||
console.error('Failed to show notification:', error); | ||
} | ||
} | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.