1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2025-09-10 22:01:12 +02:00

Desktop notifications

This commit is contained in:
Philipp Heckel 2022-02-26 10:14:43 -05:00
parent 530f55c234
commit aa79fe2861
5 changed files with 101 additions and 42 deletions

View file

@ -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.

View 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;