-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.js
154 lines (146 loc) · 5.14 KB
/
handler.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Get all elements
*
*
* @type {HTMLElement}
*/
const subscribeButton = document.getElementById('subscribe');
const unsubscribeButton = document.getElementById('unsubscribe');
const subscriptionTextarea = document.getElementById('subscription');
const PUBLIC_KEY = 'BOwAikTNwh3JmmrXz7DHsclNkD8xlviX30LdCdrBFmpVI6bDVncfQFaBuR0J2UKishaQ-XldL_5cwxCYevRbq_I';
/**
* The functionality will work if the browser supports both the service worker and push manager.
*/
if (!'serviceWorker' in navigator || !'PushManager' in window) {
document.getElementById('content').textContent = "Functionality won't work";
} else {
// request for showing browser notification
Notification.requestPermission();
// register service worker
registerServiceWorker();
// add event listener for subscribe button
subscribeButton.addEventListener('click', subscribe);
// add event listener for unsubscribe button
unsubscribeButton.addEventListener('click', unsubscribe);
// initialize the subscription status and buttons
initButtons();
}
/**
* Register the service worker
*/
function registerServiceWorker() {
navigator.serviceWorker.register('sw.js')
.then(
function (registration) {
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}
)
.catch(function (err) {
alert('ServiceWorker registration failed');
console.error('ServiceWorker registration failed: ', err);
});
}
/**
* Initialize the buttons by checking the service worker status and the subscription status
* Hide/show the buttons and subscription data according to the status
*/
function initButtons() {
// if the service worker is ready
navigator.serviceWorker.ready
.then(function (registration) {
// get the subscription from the push manager
registration.pushManager.getSubscription()
.then(function (subscription) {
// if subscribed already
if (subscription) {
// hide the subscribe button
subscribeButton.style.display = "none";
// show the unsubscribe button
unsubscribeButton.style.display = "block";
// show subscription data
subscriptionTextarea.style.display = "block";
subscriptionTextarea.innerText = JSON.stringify(subscription);
} else {
// show subscription button
subscribeButton.style.display = "block";
// hide unsubscribe button
unsubscribeButton.style.display = "none";
// hide subscription data
subscriptionTextarea.style.display = "none";
}
})
});
}
/**
* Trigger when clicking the subscribe option
* Use the subscription public key here
*
* @param event
*/
function subscribe(event) {
// prevent default actions
event.preventDefault();
// subscription processing
navigator.serviceWorker.ready.then((registration) => {
registration.pushManager.subscribe({
userVisibleOnly: true, //Always show notification when received
applicationServerKey: urlBase64ToUint8Array(
PUBLIC_KEY
)
})
.then(function (subscription) {
// change the state of buttons if success
initButtons();
})
.catch(function (error) {
console.error('Error occurred while enabling push ', error);
});
});
}
/**
* Trigger when clicking the unsubscribe button
* Stop showing notifications
*
* @param event
*/
function unsubscribe(event) {
// prevent default actions
event.preventDefault();
// unsubscription process
navigator.serviceWorker.ready
.then(function (registration) {
registration.pushManager.getSubscription()
.then(function (subscription) {
if (!subscription) {
alert('Unable to unregister push notification.');
return;
}
subscription.unsubscribe()
.then(function () {
// set the buttons
initButtons();
})
.catch(function (error) {
console.error(error);
});
})
});
}
/**
* Convert base 64 string to Uint8Array array
*
* @param base64String
* @returns {Uint8Array}
*/
function urlBase64ToUint8Array(base64String) {
let padding = '='.repeat((4 - base64String.length % 4) % 4);
let base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/');
let rawData = window.atob(base64);
let outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}