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

53 lines
1.8 KiB
JavaScript
Raw Normal View History

import {formatMessage, formatTitleWithFallback, topicShortUrl} from "./utils";
2022-03-02 22:16:30 +01:00
import prefs from "./Prefs";
import subscriptionManager from "./SubscriptionManager";
2022-02-26 16:14:43 +01:00
class NotificationManager {
async notify(subscriptionId, notification, onClickFallback) {
const subscription = await subscriptionManager.get(subscriptionId);
const shouldNotify = await this.shouldNotify(subscription, notification);
if (!shouldNotify) {
return;
}
2022-03-02 22:16:30 +01:00
const shortUrl = topicShortUrl(subscription.baseUrl, subscription.topic);
2022-02-26 16:14:43 +01:00
const message = formatMessage(notification);
2022-03-02 22:16:30 +01:00
const title = formatTitleWithFallback(notification, shortUrl);
console.log(`[NotificationManager, ${shortUrl}] Displaying notification ${notification.id}: ${message}`);
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);
});
}
}
async shouldNotify(subscription, notification) {
const priority = (notification.priority) ? notification.priority : 3;
2022-03-02 22:16:30 +01:00
const minPriority = await prefs.minPriority();
if (priority < minPriority) {
return false;
}
return true;
}
2022-02-26 16:14:43 +01:00
}
const notificationManager = new NotificationManager();
export default notificationManager;