|
1 | | -var PushNotifications = (function () { |
| 1 | +const PushNotifications = (function () { |
2 | 2 | let applicationServerPublicKey; |
3 | 3 |
|
4 | 4 | let consoleOutput; |
5 | 5 | let pushServiceWorkerRegistration; |
6 | 6 | let subscribeButton, unsubscribeButton; |
7 | 7 | let topicInput, urgencySelect, notificationInput; |
8 | 8 |
|
9 | | - function urlB64ToUint8Array(base64String) { |
10 | | - const padding = '='.repeat((4 - base64String.length % 4) % 4); |
11 | | - const base64 = (base64String + padding).replace(/\-/g, '+').replace(/_/g, '/'); |
12 | | - |
13 | | - const rawData = window.atob(base64); |
14 | | - const outputArray = new Uint8Array(rawData.length); |
15 | | - |
16 | | - for (let i = 0; i < rawData.length; ++i) { |
17 | | - outputArray[i] = rawData.charCodeAt(i); |
18 | | - } |
19 | | - |
20 | | - return outputArray; |
21 | | - }; |
22 | | - |
23 | 9 | function initializeConsole() { |
24 | 10 | consoleOutput = document.getElementById('output'); |
25 | 11 | document.getElementById('clear').addEventListener('click', clearConsole); |
26 | | - }; |
| 12 | + } |
27 | 13 |
|
28 | 14 | function clearConsole() { |
29 | 15 | while (consoleOutput.childNodes.length > 0) { |
30 | 16 | consoleOutput.removeChild(consoleOutput.lastChild); |
31 | 17 | } |
32 | | - }; |
| 18 | + } |
33 | 19 |
|
34 | 20 | function writeToConsole(text) { |
35 | 21 | var paragraph = document.createElement('p'); |
36 | 22 | paragraph.style.wordWrap = 'break-word'; |
37 | 23 | paragraph.appendChild(document.createTextNode(text)); |
38 | 24 |
|
39 | 25 | consoleOutput.appendChild(paragraph); |
40 | | - }; |
| 26 | + } |
41 | 27 |
|
42 | 28 | function registerPushServiceWorker() { |
43 | 29 | navigator.serviceWorker.register('/scripts/service-workers/push-service-worker.js', { scope: '/scripts/service-workers/push-service-worker/' }) |
|
50 | 36 | }).catch(function (error) { |
51 | 37 | writeToConsole('Push Service Worker registration has failed: ' + error); |
52 | 38 | }); |
53 | | - }; |
| 39 | + } |
54 | 40 |
|
55 | 41 | function initializeUIState() { |
56 | 42 | subscribeButton = document.getElementById('subscribe'); |
|
68 | 54 | .then(function (subscription) { |
69 | 55 | changeUIState(Notification.permission === 'denied', subscription !== null); |
70 | 56 | }); |
71 | | - }; |
| 57 | + } |
72 | 58 |
|
73 | 59 | function changeUIState(notificationsBlocked, isSubscibed) { |
74 | 60 | subscribeButton.disabled = notificationsBlocked || isSubscibed; |
|
77 | 63 | if (notificationsBlocked) { |
78 | 64 | writeToConsole('Permission for Push Notifications has been denied'); |
79 | 65 | } |
80 | | - }; |
| 66 | + } |
81 | 67 |
|
82 | 68 | function subscribeForPushNotifications() { |
83 | 69 | if (applicationServerPublicKey) { |
84 | 70 | subscribeForPushNotificationsInternal(); |
85 | 71 | } else { |
86 | | - fetch('push-notifications-api/public-key') |
87 | | - .then(function (response) { |
88 | | - if (response.ok) { |
89 | | - return response.text(); |
90 | | - } else { |
91 | | - writeToConsole('Failed to retrieve Public Key'); |
92 | | - } |
93 | | - }).then(function (applicationServerPublicKeyBase64) { |
94 | | - applicationServerPublicKey = urlB64ToUint8Array(applicationServerPublicKeyBase64); |
| 72 | + PushNotificationsController.retrievePublicKey() |
| 73 | + .then(function (retrievedPublicKey) { |
| 74 | + applicationServerPublicKey = retrievedPublicKey; |
95 | 75 | writeToConsole('Successfully retrieved Public Key'); |
96 | 76 |
|
97 | 77 | subscribeForPushNotificationsInternal(); |
98 | 78 | }).catch(function (error) { |
99 | 79 | writeToConsole('Failed to retrieve Public Key: ' + error); |
100 | 80 | }); |
101 | 81 | } |
102 | | - }; |
| 82 | + } |
103 | 83 |
|
104 | 84 | function subscribeForPushNotificationsInternal() { |
105 | 85 | pushServiceWorkerRegistration.pushManager.subscribe({ |
106 | 86 | userVisibleOnly: true, |
107 | 87 | applicationServerKey: applicationServerPublicKey |
108 | 88 | }) |
109 | 89 | .then(function (pushSubscription) { |
110 | | - fetch('push-notifications-api/subscriptions', { |
111 | | - method: 'POST', |
112 | | - headers: { 'Content-Type': 'application/json' }, |
113 | | - body: JSON.stringify(pushSubscription) |
114 | | - }) |
| 90 | + PushNotificationsController.storePushSubscription(pushSubscription) |
115 | 91 | .then(function (response) { |
116 | 92 | if (response.ok) { |
117 | 93 | writeToConsole('Successfully subscribed for Push Notifications'); |
|
130 | 106 | writeToConsole('Failed to subscribe for Push Notifications: ' + error); |
131 | 107 | } |
132 | 108 | }); |
133 | | - }; |
| 109 | + } |
134 | 110 |
|
135 | 111 | function unsubscribeFromPushNotifications() { |
136 | 112 | pushServiceWorkerRegistration.pushManager.getSubscription() |
137 | 113 | .then(function (pushSubscription) { |
138 | 114 | if (pushSubscription) { |
139 | 115 | pushSubscription.unsubscribe() |
140 | 116 | .then(function () { |
141 | | - fetch('push-notifications-api/subscriptions?endpoint=' + encodeURIComponent(pushSubscription.endpoint), { |
142 | | - method: 'DELETE', |
143 | | - }) |
| 117 | + PushNotificationsController.discardPushSubscription(pushSubscription) |
144 | 118 | .then(function (response) { |
145 | 119 | if (response.ok) { |
146 | 120 | writeToConsole('Successfully unsubscribed from Push Notifications'); |
|
157 | 131 | }); |
158 | 132 | } |
159 | 133 | }); |
160 | | - }; |
| 134 | + } |
161 | 135 |
|
162 | 136 | function sendPushNotification() { |
163 | 137 | let payload = { topic: topicInput.value, notification: notificationInput.value, urgency: urgencySelect.value }; |
|
176 | 150 | }).catch(function (error) { |
177 | 151 | writeToConsole('Failed to send Push Notification: ' + error); |
178 | 152 | }); |
179 | | - }; |
| 153 | + } |
180 | 154 |
|
181 | 155 | return { |
182 | 156 | initialize: function () { |
183 | 157 | initializeConsole(); |
184 | 158 |
|
185 | | - if (!'serviceWork' in navigator) { |
| 159 | + if (!('serviceWorker' in navigator)) { |
186 | 160 | writeToConsole('Service Workers are not supported'); |
187 | 161 | return; |
188 | 162 | } |
189 | 163 |
|
190 | | - if (!'PushManager' in window) { |
| 164 | + if (!('PushManager' in window)) { |
191 | 165 | writeToConsole('Push API not supported'); |
192 | 166 | return; |
193 | 167 | } |
|
0 commit comments