-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
60 lines (52 loc) · 1.88 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const getOptions = (body, data, timestamp = Date.now()) => {
return {
body,
data,
tag: timestamp, // a unique ID
showTrigger: timestamp, // set the time for the push notification
badge: './screenshot.png',
icon: './logo-192x192.png',
actions: [
{action: 'open', title: 'Open'},
{action: 'close', title: 'Close'}
]
}
}
const defaultNotification = {
title: 'Checkout updates on Github!',
options: getOptions('Frontend mentor solutions, freeCodeCamp and more on Github.', {url: 'https://github.com/mfrank37'})
}
const sendNotification = async (notificationConfig = defaultNotification) => {
const {title, options} = notificationConfig;
const reg = await navigator.serviceWorker.getRegistration();
const permission = await Notification.requestPermission();
if (permission !== 'granted') {
alert('You need to allow push notifications');
} else {
reg.showNotification(title, options);
}
};
const interval = setInterval(() => sendNotification(), 10 * 60 * 1000); // 10 minutes
document.querySelector('h1').onclick = (e) => {
sendNotification();
};
document.querySelector('h1').ondblclick = async () => {
const reg = await navigator.serviceWorker.getRegistration();
const notifications = await reg.getNotifications({
includeTriggered: true
});
notifications.forEach(notification => notification.close());
alert(`${notifications.length} notification(s) cancelled`);
};
document.querySelector('footer').ondblclick = async () => {
clearInterval(interval);
}
const aside = document.querySelector('aside');
const iconBulb = document.querySelector('.icon.bulb');
const iconXMark = document.querySelector('.icon:not(.bulb)');
iconBulb.addEventListener('click', () => {
aside.classList.add('open');
});
iconXMark.addEventListener('click', () => {
aside.classList.remove('open');
})