/
index.js
53 lines (45 loc) · 1.22 KB
/
index.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
'use strict';
var scope = {
scope: './'
};
var endpoint;
var baseURL = 'https://localhost:3012/';
navigator.serviceWorker.register('service-worker.js')
.then(function(registration) {
return registration.pushManager.getSubscription()
.then(function(subscription) {
if (subscription) {
return subscription;
}
return registration.pushManager.subscribe({ userVisibleOnly: true });
});
}).then(function(subscription) {
endpoint = subscription.endpoint;
fetch(baseURL + 'register', {
method: 'post',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify({
endpoint: subscription.endpoint,
}),
});
});
function printStatus(status) {
document.querySelector('#status').innerHTML = status;
}
document.querySelector('#resetButton').addEventListener('click',
function() {
navigator.serviceWorker.getRegistration().then(function(registration) {
registration.unregister();
window.location.reload();
});
}
);
document.querySelector('#send').onclick = function() {
var delay = document.querySelector('#notification-delay').value;
fetch(baseURL + 'sendNotification?endpoint=' + endpoint + '&delay=' + delay, {
method: 'post'
}
);
};