-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
57 lines (52 loc) · 1.68 KB
/
main.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
57
Notification.requestPermission(function(status) {
console.log('Notification permission status:', status);
});
function displayNotification() {
if (Notification.permission == 'granted') {
navigator.serviceWorker.getRegistration().then(function(reg) {
var options = {
body: 'Here is a notification body!',
icon: 'images/example.png',
vibrate: [100, 50, 100],
data: {
dateOfArrival: Date.now(),
primaryKey: 1
},
actions: [
{action: 'explore', title: 'Explore this new world',
icon: 'images/checkmark.png'},
{action: 'close', title: 'Close notification',
icon: 'images/xmark.png'},
]
};
reg.showNotification('Hello world!', options);
});
}
}
if ('Notification' in window && navigator.serviceWorker) {
// Display the UI to let the user toggle notifications
}
if (Notification.permission === "granted") {
/* do our magic */
} else if (Notification.permission === "blocked") {
/* the user has previously denied push. Can't reprompt. */
} else {
/* show a prompt to the user */
}
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js').then(function(reg) {
console.log('Service Worker Registered!', reg);
reg.pushManager.getSubscription().then(function(sub) {
if (sub === null) {
// Update UI to ask user to register for Push
console.log('Not subscribed to push service!');
} else {
// We have a subscription, update the database
console.log('Subscription object: ', sub);
}
});
})
.catch(function(err) {
console.log('Service Worker registration failed: ', err);
});
}