mirror of
https://github.com/binwiederhier/ntfy.git
synced 2024-09-30 17:20:41 +13:00
34 lines
963 B
JavaScript
34 lines
963 B
JavaScript
|
import {formatMessage, formatTitle} from "./utils";
|
||
|
|
||
|
class NotificationManager {
|
||
|
notify(subscription, notification, onClickFallback) {
|
||
|
const message = formatMessage(notification);
|
||
|
const title = formatTitle(notification);
|
||
|
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;
|