async function check_or_enable_notification_permission() {
if (!("Notification" in window)) {
return false;
}
if (Notification.permission === "granted") {
return true;
}
if (Notification.permission !== "denied") {
// We need to ask the user for permission
const permission = await Notification.requestPermission()
if (permission === "granted") {
return true;
}
}
return false;
}
async function create(title, text) {
const notifications_enabled = await check_or_enable_notification_permission();
if (notifications_enabled) {
return new Notification(title, {
body: text,
icon: 'favicon/android-chrome-192x192.png'
});
}
}
module.exports = {
enabled: check_or_enable_notification_permission,
create: create,
queue: async function(title, text, dispatch_datetime, on_create) {
const enabled = await check_or_enable_notification_permission();
if (!enabled) {
return;
}
const now = new Date();
const next_notification_date = new Date(dispatch_datetime);
const timeout_remaining = next_notification_date.getTime() - now.getTime();
return setTimeout(function() {
create(title, text);
on_create();
}, timeout_remaining);
}
};