blowater/UI/notification.ts

26 lines
838 B
TypeScript
Raw Normal View History

2023-06-30 14:05:57 +00:00
export async function notify(title: string, msg: string, icon: string, onclick: () => void) {
try {
if (Notification.permission == "denied") {
console.warn(`Notification.permission == "denied"`);
return;
} else if (Notification.permission !== "granted") {
const result = await Notification.requestPermission();
if (result != "granted") {
console.warn(`the user denied the request: ${result}`);
}
return;
}
const notification = new Notification(title, {
body: msg,
icon: icon,
});
notification.onclick = (_) => {
window.parent.parent.focus();
notification.close();
onclick();
};
} catch (e) {
console.error(e);
}
}