2022-07-01 11:16:13 -07:00
|
|
|
import $ from "jquery";
|
2021-09-02 23:18:43 -07:00
|
|
|
|
2021-12-29 11:32:33 -08:00
|
|
|
const noteCloseTime = 4*1000; // 4 seconds
|
2022-07-01 11:16:13 -07:00
|
|
|
const noteIcon = webroot + "/favicon.png";
|
2021-09-02 23:18:43 -07:00
|
|
|
|
|
|
|
function canNotify() {
|
2023-06-15 14:38:57 -07:00
|
|
|
return (location.protocol === "https:")
|
2021-09-02 23:18:43 -07:00
|
|
|
&& (typeof Notification !== "undefined");
|
|
|
|
}
|
|
|
|
|
2023-05-13 23:46:41 -07:00
|
|
|
export function notify(title: string, body: string, img = noteIcon) {
|
2023-05-23 12:32:46 -07:00
|
|
|
const n = new Notification(title, {
|
2021-09-02 23:18:43 -07:00
|
|
|
body: body,
|
|
|
|
image: img,
|
2022-07-01 11:16:13 -07:00
|
|
|
icon: noteIcon
|
2021-09-02 23:18:43 -07:00
|
|
|
});
|
|
|
|
setTimeout(() => {
|
|
|
|
n.close();
|
|
|
|
}, noteCloseTime);
|
|
|
|
}
|
|
|
|
|
2023-05-23 12:32:46 -07:00
|
|
|
$(() => {
|
2021-09-02 23:18:43 -07:00
|
|
|
if(!canNotify())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Notification.requestPermission().then(granted => {
|
2023-06-15 14:38:57 -07:00
|
|
|
if(granted !== "granted")
|
2021-09-02 23:18:43 -07:00
|
|
|
return Promise.reject("denied");
|
|
|
|
}).catch(err => {
|
2023-06-15 14:38:57 -07:00
|
|
|
if(err !== "denied")
|
2021-09-02 23:18:43 -07:00
|
|
|
console.log(`Error starting notifications: ${err}`);
|
|
|
|
});
|
|
|
|
});
|