diff --git a/src/Helpers/Browser.ts b/src/Helpers/Browser.ts index 5b44d5a..f437489 100644 --- a/src/Helpers/Browser.ts +++ b/src/Helpers/Browser.ts @@ -9,8 +9,28 @@ export function parseLocation(location?: string | undefined) { } export function notify(msg: string): void { - const n = new Notification(msg); - setTimeout(() => n && n.close(), 4000); + // Let's check if the browser supports notifications + if (!('Notification' in window)) { + alert('This browser does not support desktop notification'); + } + + // Let's check whether notification permissions have already been granted + else if (Notification.permission === 'granted') { + // If it's okay let's create a notification + const n = new Notification(msg); + setTimeout(() => n && n.close(), 4000); + } + + // Otherwise, we need to ask the user for permission + else if (Notification.permission !== 'denied') { + Notification.requestPermission().then(function (permission) { + // If the user accepts, let's create a notification + if (permission === 'granted') { + const n = new Notification(msg); + setTimeout(() => n && n.close(), 4000); + } + }); + } } export function markPage(text: string, version: string) {