1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-09-29 03:41:59 +02:00
ntfy/web/src/app/NotificationManager.js

34 lines
963 B
JavaScript
Raw Normal View History

2022-02-26 16:14:43 +01:00
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;