Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
88 lines (81 loc) · 3.04 KB
/
Copy pathindex.html
File metadata and controls
88 lines (81 loc) · 3.04 KB
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Webpush Golang Example</title>
</head>
<body style="display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh;">
<button id="send-notification-btn" disabled style="padding: 10px; font-size: 16px;">
Send Test Notification
</button>
<script>
const sendNotificationBtn = document.getElementById('send-notification-btn');
let currentSubscription;
function subscribe() {
console.log('Subscribing to push notifications...');
navigator.serviceWorker.ready
.then(function(registration) {
console.log('Fetching VAPID public key...');
return fetch('/vapid_public_key').then(response => response.text()).then(vapidPublicKey => {
console.log('Got VAPID public key.');
return registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(vapidPublicKey),
});
})
})
.then(function(subscription) {
currentSubscription = subscription;
console.log('Subscription registered.');
sendNotificationBtn.disabled = false;
})
.catch(err => console.error(err));
}
function urlBase64ToUint8Array(base64String) {
const padding = '='.repeat((4 - (base64String.length % 4)) % 4);
const base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/');
const rawData = window.atob(base64);
return Uint8Array.from([...rawData].map(char => char.charCodeAt(0)));
}
if ('serviceWorker' in navigator) {
console.log('Registering service worker...');
navigator.serviceWorker.register('service-worker.js');
navigator.serviceWorker.ready
.then(function(registration) {
console.log('Service worker ready, checking for subscription...');
return registration.pushManager.getSubscription();
})
.then(function(subscription) {
if (!subscription) {
console.log('No subscription found, starting subscription process.');
subscribe();
} else {
console.log('Existing subscription found.');
currentSubscription = subscription;
sendNotificationBtn.disabled = false;
}
});
}
sendNotificationBtn.addEventListener('click', () => {
console.log('Sending notification...');
let authScheme = 'vapid';
const userAgent = navigator.userAgent;
if (userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Edg") === -1) {
authScheme = 'webpush';
}
fetch('/send_notification', {
method: 'POST',
body: JSON.stringify({
subscription: currentSubscription,
authScheme: authScheme,
})
})
.then(response => response.text())
.then(text => console.log('Server response:', text))
.catch(err => console.error('Error sending notification:', err));
});
</script>
</body>
</html>