-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
56 lines (43 loc) · 1.47 KB
/
script.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
const checkPermissions = () => {
return new Promise((resolve, reject) => {
if (!('serviceWorker' in navigator)) {
reject(new Error("No support for the Service Worker API!"));
}
if (!('Notification' in window)) {
reject(new Error("No support for the Notification API"));
}
if (!('PushManager' in window)) {
reject(new Error("No support for the Push API"));
}
resolve();
});
};
const askForNotificationPermission = async () => {
const permission = await Notification.requestPermission();
if (permission !== 'granted') {
throw new Error("Notification permission not granted")
}
return permission;
}
const registerServiceWorker = async () => {
const registration = await navigator.serviceWorker.register('sw.js');
if (registration.installing) {
console.log("Service worker installing");
} else if (registration.waiting) {
console.log("Service worker installed");
} else if (registration.active) {
console.log("Service worker active");
}
console.log("Service Worker registered successfully!");
// registration.showNotification('Hello world'); // show notification
return registration;
};
const main = async () => {
try {
await checkPermissions()
await askForNotificationPermission()
await registerServiceWorker()
} catch (error) {
console.error("Error:", error.toString());
}
}