-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
108 lines (97 loc) · 3.85 KB
/
index.html
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Football</title>
<meta name="description" content="My first PWA" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="css/materialize.min.css" />
<link rel="manifest" href="/manifest.json" />
<link rel="apple-touch-icon" href="/icon.png">
<meta name="theme-color" content="#00897B" />
</head>
<body>
<!-- Navigasi -->
<nav class="teal lighten-1" role="navigation">
<div class="nav-wrapper container">
<a href="#" class="brand-logo" id="logo-container">Football News</a>
<a href="#" class="sidenav-trigger" data-target="nav-mobile">☰</a>
<ul class="topnav right hide-on-med-and-down"></ul>
<ul class="sidenav" id="nav-mobile"></ul>
</div>
</nav>
<!-- Akhir Navigasi -->
<div class="container" id="body-content"></div>
<script src="js/materialize.min.js"></script>
<script src="js/nav.js"></script>
<script src="js/api.js"></script>
<script src="js/idb.js"></script>
<script src="js/db.js"></script>
<script>
// REGISTER SERVICE WORKER
if ("serviceWorker" in navigator) {
window.addEventListener("load", function() {
navigator.serviceWorker
.register("service-worker.js")
.then(function() {
console.log("Pendaftaran ServiceWorker berhasil");
})
.catch(function() {
console.log("Pendaftaran ServiceWorker gagal");
});
});
} else {
console.log("ServiceWorker belum didukung browser ini.");
}
requestPermission();
function requestPermission() {
if ('Notification' in window) {
Notification.requestPermission().then(function (result) {
if (result === "denied") {
console.log("Fitur notifikasi tidak diijinkan.");
return;
} else if (result === "default") {
console.error("Pengguna menutup kotak dialog permintaan ijin.");
return;
}
navigator.serviceWorker.getRegistration().then(function(reg) {
reg.showNotification('Notifikasi diijinkan!');
});
if (('PushManager' in window)) {
navigator.serviceWorker.getRegistration().then(function(registration) {
registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array('BL1xFcXW3nV7tba06VIp_ou_8P7GMmnYRNujZ6qVzAFFmgLodAzIOggeCE5fh9RSZM2VQ_h9q1DtpzSCxsJ_nOo')
}).then(function(subscribe) {
console.log('Berhasil melakukan subscribe dengan endpoint: ', subscribe.endpoint);
console.log('Berhasil melakukan subscribe dengan p256dh key: ', btoa(String.fromCharCode.apply(
null, new Uint8Array(subscribe.getKey('p256dh')))));
console.log('Berhasil melakukan subscribe dengan auth key: ', btoa(String.fromCharCode.apply(
null, new Uint8Array(subscribe.getKey('auth')))));
}).catch(function(e) {
console.error('Tidak dapat melakukan subscribe ', e.message);
});
});
}
});
}
}
function urlBase64ToUint8Array(base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding)
.replace(/-/g, '+')
.replace(/_/g, '/');
const rawData = window.atob(base64);
const outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}
// REQUEST API UNTUK PERTAMA KALI
document.addEventListener("DOMContentLoaded", function() {
var page = window.location.hash.substr(1);
});
</script>
</body>
</html>