Skip to content

Commit 1210997

Browse files
feat: add a dialog if User denies notfication permission (#737)
Co-authored-by: Dunsin <78784850+Dun-sin@users.noreply.github.com>
1 parent f5a1d0b commit 1210997

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

client/src/lib/browserNotification.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import BadWordsNext from 'bad-words-next';
22
import en from 'bad-words-next/data/en.json';
33

4-
export const requestBrowserNotificationPermissions = () => {
4+
export const requestBrowserNotificationPermissions = async () => {
55
if (!('Notification' in window)) {
66
console.log('Browser does not support desktop notification');
77
} else {
8-
Notification.requestPermission();
8+
const res = await Notification.requestPermission();
9+
if (res === 'granted') {
10+
return true;
11+
} else {
12+
return false;
13+
}
914
}
1015
};
1116

client/src/pages/Start.jsx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,50 @@ import { requestBrowserNotificationPermissions } from 'src/lib/browserNotificati
66
import { useApp } from 'src/context/AppContext';
77
import useCloseChat from 'src/hooks/useCloseChat';
88
import { useEffect } from 'react';
9+
import { useDialog } from 'src/context/DialogContext';
910

1011
const centerElement = 'flex flex-col items-center justify-center';
1112

1213
const Start = () => {
1314
const { app } = useApp();
1415
const navigate = useNavigate();
1516
const { handleClose } = useCloseChat();
17+
const { setDialog } = useDialog();
1618

19+
const requestPermission = async () => {
20+
try {
21+
const isGranted = await requestBrowserNotificationPermissions();
22+
if (!isGranted) {
23+
setDialog({
24+
isOpen: true,
25+
text: "You've blocked Whisper notifications. Enable them to stay updated, keep track of conversations, and never miss important messages.",
26+
handler: async (response) => {
27+
if (response) {
28+
await requestBrowserNotificationPermissions();
29+
setDialog({ isOpen: false });
30+
} else {
31+
setDialog({ isOpen: false });
32+
}
33+
},
34+
noBtnText: 'No',
35+
yesBtnText: 'Try Again',
36+
});
37+
}
38+
39+
return isGranted;
40+
} catch (error) {
41+
console.error('Error requesting notification permission:', error);
42+
return false;
43+
}
44+
};
1745
useEffect(() => {
1846
if (app.isSearching) {
1947
navigate('/searching');
2048
}
21-
22-
requestBrowserNotificationPermissions();
49+
const checkPermission = async () => {
50+
await requestPermission();
51+
};
52+
checkPermission();
2353
}, []);
2454

2555
return (

0 commit comments

Comments
 (0)