mirror of
https://github.com/binwiederhier/ntfy.git
synced 2025-09-10 22:01:12 +02:00
Desktop notifications
This commit is contained in:
parent
530f55c234
commit
aa79fe2861
5 changed files with 101 additions and 42 deletions
|
@ -32,13 +32,16 @@ class Connection {
|
|||
console.log(`[Connection, ${this.shortUrl}] Message received from server: ${event.data}`);
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
if (data.event === 'open') {
|
||||
return;
|
||||
}
|
||||
const relevantAndValid =
|
||||
data.event === 'message' &&
|
||||
'id' in data &&
|
||||
'time' in data &&
|
||||
'message' in data;
|
||||
if (!relevantAndValid) {
|
||||
console.log(`[Connection, ${this.shortUrl}] Message irrelevant or invalid. Ignoring.`);
|
||||
console.log(`[Connection, ${this.shortUrl}] Unexpected message. Ignoring.`);
|
||||
return;
|
||||
}
|
||||
this.since = data.time + 1; // Sigh. This works because on reconnect, we wait 5+ seconds anyway.
|
||||
|
|
33
web/src/app/NotificationManager.js
Normal file
33
web/src/app/NotificationManager.js
Normal file
|
@ -0,0 +1,33 @@
|
|||
import {formatMessage, formatTitle} from "./utils";
|
||||
|
||||
class NotificationManager {
|
||||
notify(subscription, notification, onClickFallback) {
|
||||
const message = formatMessage(notification);
|
||||
const title = formatTitle(notification);
|
||||
const n = new Notification(title, {
|
||||
body: message,
|
||||
icon: '/static/img/favicon.png'
|
||||
});
|
||||
if (notification.click) {
|
||||
n.onclick = (e) => window.open(notification.click);
|
||||
} else {
|
||||
n.onclick = onClickFallback;
|
||||
}
|
||||
}
|
||||
|
||||
granted() {
|
||||
return Notification.permission === 'granted';
|
||||
}
|
||||
|
||||
maybeRequestPermission(cb) {
|
||||
if (!this.granted()) {
|
||||
Notification.requestPermission().then((permission) => {
|
||||
const granted = permission === 'granted';
|
||||
cb(granted);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const notificationManager = new NotificationManager();
|
||||
export default notificationManager;
|
Loading…
Add table
Add a link
Reference in a new issue