Support sounds

This commit is contained in:
Philipp Heckel 2022-03-06 00:02:27 -05:00
parent 09b128f27a
commit dc7ca6e405
13 changed files with 73 additions and 16 deletions

View file

@ -1,52 +0,0 @@
import {formatMessage, formatTitleWithDefault, openUrl, topicShortUrl} from "./utils";
import prefs from "./Prefs";
import subscriptionManager from "./SubscriptionManager";
class NotificationManager {
async notify(subscriptionId, notification, onClickFallback) {
const subscription = await subscriptionManager.get(subscriptionId);
const shouldNotify = await this.shouldNotify(subscription, notification);
if (!shouldNotify) {
return;
}
const shortUrl = topicShortUrl(subscription.baseUrl, subscription.topic);
const message = formatMessage(notification);
const title = formatTitleWithDefault(notification, shortUrl);
console.log(`[NotificationManager, ${shortUrl}] Displaying notification ${notification.id}: ${message}`);
const n = new Notification(title, {
body: message,
icon: '/static/img/favicon.png'
});
if (notification.click) {
n.onclick = (e) => openUrl(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;
const minPriority = await prefs.minPriority();
if (priority < minPriority) {
return false;
}
return true;
}
}
const notificationManager = new NotificationManager();
export default notificationManager;