From 9ae499f18c331276fa8f8768589e8a631bcd4c00 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sun, 18 Oct 2020 10:47:09 +0300 Subject: [PATCH] Add notification permission request --- src/Helpers/Browser.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) 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) {