1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-10-28 07:32:40 +01:00
ntfy/web/src/app/NotificationManager.js

46 lines
1.4 KiB
JavaScript
Raw Normal View History

import {formatMessage, formatTitleWithFallback, topicShortUrl} from "./utils";
import repository from "./Repository";
2022-02-26 16:14:43 +01:00
class NotificationManager {
notify(subscription, notification, onClickFallback) {
if (!this.shouldNotify(subscription, notification)) {
return;
}
2022-02-26 16:14:43 +01:00
const message = formatMessage(notification);
const title = formatTitleWithFallback(notification, topicShortUrl(subscription.baseUrl, subscription.topic));
2022-02-26 16:14:43 +01: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);
});
}
}
shouldNotify(subscription, notification) {
const priority = (notification.priority) ? notification.priority : 3;
if (priority < repository.getMinPriority()) {
return false;
}
return true;
}
2022-02-26 16:14:43 +01:00
}
const notificationManager = new NotificationManager();
export default notificationManager;