Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Web Push Notification</title>
- </head>
- <body>
- <h3>Ini contoh Web Push Notif Lokal</h3>
- <br>
- <button onclick="checkPermission()">Check Permission</button>
- <br>
- <button onclick="displayNotification()">Display Push Notif</button>
- <script>
- function registerSW() {
- if ('serviceWorker' in navigator) { //cek browser support serviceWorker
- navigator.serviceWorker
- .register('/sw.js')
- .then(function (reg) {
- //Berhasil terdaftar
- console.log('Terdaftar pada scope' + reg.scope)
- })
- .catch(function (error) {
- //Gagal Terdaftar
- console.log('Gagal :' + error)
- });
- } else {
- console.log('Browser tidak support SW')
- }
- }
- function checkPermission() {
- Notification.requestPermission().then(function (permission) {
- alert(permission);
- });
- }
- function displayNotification() {
- if (Notification.permission === 'granted') {
- navigator.serviceWorker.getRegistration()
- .then(reg => {
- console.log(reg)
- const options = {
- body: 'Here is a notification body!',
- icon: 'https://miro.medium.com/max/432/1*gYmM9tPZOxnBr_sjUAgVBw.png',
- data: {url: 'https://google.com'} // allows us to identify notification
- };
- reg.showNotification('Hello world!', options);
- });
- }
- }
- document.addEventListener('DOMContentLoaded', async function () {
- registerSW();
- });
- </script>
- </body>
- </html>
Add Comment
Please, Sign In to add comment