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