ntfy/web/src/app/NotificationManager.js

34 lines
1 KiB
JavaScript
Raw Normal View History

import {formatMessage, formatTitleWithFallback, topicShortUrl} from "./utils";
2022-02-27 04:14:43 +13:00
class NotificationManager {
notify(subscription, notification, onClickFallback) {
const message = formatMessage(notification);
const title = formatTitleWithFallback(notification, topicShortUrl(subscription.baseUrl, subscription.topic));
2022-02-27 04:14:43 +13:00
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;