-
-
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 9 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,52 @@ | ||
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 { | ||
// Get VAPID keys from settings | ||
const publicVapidKey = await setting("webpushPublicVapidKey"); | ||
const privateVapidKey = await setting("webpushPrivateVapidKey"); | ||
// Set Vapid keys in web-push helper lib | ||
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 | ||
}); | ||
//send push notification using web-push lib | ||
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}` | ||
}); | ||
//send push notification using web-push lib | ||
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,106 @@ | ||
<template> | ||
DoidoYo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<button | ||
class="mb-3" | ||
type="button" | ||
:class="[ | ||
'btn', | ||
canRegister ? '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="mb-3 form-text"> | ||
<a href="TODO" target="_blank">{{ $t("documentationOf", ["Webpush"]) }}</a> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
data() { | ||
return { | ||
//store subscription info | ||
btnEnabled: false, | ||
btnText: "", | ||
processing: false, | ||
//determines if browser supports service worker | ||
canRegister: false, | ||
DoidoYo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
//store public vapid key | ||
publicVapidKey: null, | ||
}; | ||
}, | ||
mounted() { | ||
// if already subscribed | ||
DoidoYo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (this.$parent.notification.subscription) { | ||
this.btnEnabled = false; | ||
this.canRegister = true; | ||
this.btnText = "Notifications Enabled"; | ||
} else { //not subscribed | ||
//check if browser supports service worker | ||
if (("serviceWorker" in navigator)) { | ||
this.btnText = "Allow Notifications"; | ||
this.canRegister = true; | ||
this.btnEnabled = true; | ||
} else { //browser does not support service worker | ||
this.btnText = "Browser not supported"; | ||
this.canRegister = false; | ||
this.btnEnabled = false; | ||
} | ||
} | ||
}, | ||
methods: { | ||
async registerWebpush() { | ||
this.processing = true; | ||
try { | ||
// Get the VAPID public key from the server | ||
const publicKey = await new Promise((resolve, reject) => { | ||
this.$root.getSocket().emit("getWebpushVapidPublicKey", (resp) => { | ||
if (!resp.ok) { | ||
reject(new Error(resp.msg)); | ||
} | ||
resolve(resp.msg); | ||
}); | ||
}); | ||
//request permission to send notifications | ||
const permission = await Notification.requestPermission(); | ||
if (permission !== "granted") { | ||
this.$root.toastRes({ | ||
ok: false, | ||
msg: "Unable to get permission to notify.", | ||
DoidoYo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}); | ||
this.processing = false; | ||
return; | ||
} | ||
//get service worker registration | ||
const registration = await navigator.serviceWorker.ready; | ||
//subscribe to push notifications | ||
const subscription = await registration.pushManager.subscribe({ | ||
userVisibleOnly: true, | ||
applicationServerKey: publicKey, | ||
}); | ||
//store subscription info and update button | ||
this.$parent.notification.subscription = subscription; | ||
this.btnEnabled = false; | ||
this.canRegister = true; | ||
this.btnText = "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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// 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 && | ||
self.Notification.permission === 'granted' | ||
)) { | ||
//notifications aren't supported or permission not granted! | ||
DoidoYo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
console.log("Notifications aren't supported or permission not granted!"); | ||
return; | ||
} | ||
|
||
if (event.data) { | ||
console.log(event); | ||
DoidoYo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
let message = event.data.json(); | ||
self.registration.showNotification(message.title, { | ||
DoidoYo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
body: message.body, | ||
DoidoYo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}); | ||
} | ||
}); |
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.