Commit e94cfa2d2968d32119e34a290b1e6033baff7385
1 parent
eaa6cd5422
Exists in
master
and in
1 other branch
Add more checking for existence of services and checkbox change
Showing 1 changed file with 53 additions and 7 deletions Side-by-side Diff
scripts/SettingsController.js
View file @
e94cfa2
... | ... | @@ -12,6 +12,7 @@ |
12 | 12 | |
13 | 13 | console.log('executing things outside of module'); |
14 | 14 | var PUSH_SERVER_URL = '/api/subscribe/'; |
15 | + var UNPUSH_SERVER_URL = '/api/unsubscribe/'; | |
15 | 16 | |
16 | 17 | function onPushSubscription(pushSubscription) { |
17 | 18 | console.log('pushSubscription = ', pushSubscription.endpoint); |
... | ... | @@ -27,6 +28,16 @@ |
27 | 28 | $http.post(PUSH_SERVER_URL, {'registration_id': subscriptionId}); |
28 | 29 | } |
29 | 30 | |
31 | + function removeSubscription(pushSubscription) { | |
32 | + console.log('removing subscription'); | |
33 | + console.log('pushSubscription endpoint = ', pushSubscription.endpoint); | |
34 | + | |
35 | + var subscriptionId = pushSubscription.subscriptionId; | |
36 | + | |
37 | + console.log('registration_id: ', subscriptionId); | |
38 | + $http.post(UNPUSH_SERVER_URL, {'registration_id': subscriptionId}); | |
39 | + } | |
40 | + | |
30 | 41 | function subscribeDevice() { |
31 | 42 | // We need the service worker registration to access the push manager |
32 | 43 | navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { |
33 | 44 | |
... | ... | @@ -41,12 +52,15 @@ |
41 | 52 | console.log('subscribe() Error: Push permission status = ', |
42 | 53 | permissionStatus); |
43 | 54 | if (permissionStatus.status === 'denied') { |
55 | + pushSwitch.checked = false; | |
56 | + pushSwitch.disabled = true; | |
44 | 57 | // The user blocked the permission prompt |
45 | 58 | console.log('Ooops Notifications are Blocked', |
46 | 59 | 'Unfortunately you just permanently blocked notifications. ' + |
47 | 60 | 'Please unblock / allow them to switch on push ' + |
48 | 61 | 'notifications.'); |
49 | 62 | } else { |
63 | + pushSwitch.checked = false; | |
50 | 64 | console.log('Ooops Push Couldn\'t Register', |
51 | 65 | '<p>When we tried to ' + |
52 | 66 | 'get the subscription ID for GCM, something went wrong,' + |
... | ... | @@ -58,6 +72,7 @@ |
58 | 72 | '</p>'); |
59 | 73 | } |
60 | 74 | }).catch(function(err) { |
75 | + pushSwitch.checked = false; | |
61 | 76 | console.log('Ooops Push Couldn\'t Register', |
62 | 77 | '<p>When we tried to ' + |
63 | 78 | 'get the subscription ID for GCM, something went wrong, not ' + |
64 | 79 | |
... | ... | @@ -71,10 +86,13 @@ |
71 | 86 | } else { |
72 | 87 | // Use notification permission to do something |
73 | 88 | if (Notification.permission === 'denied') { |
89 | + pushSwitch.disabled = true; | |
90 | + pushSwitch.checked = false; | |
74 | 91 | console.log('Ooops Notifications are Blocked', |
75 | 92 | 'Unfortunately you just permanently blocked notifications. ' + |
76 | 93 | 'Please unblock / allow them to switch on push notifications.'); |
77 | 94 | } else { |
95 | + pushSwitch.checked = false; | |
78 | 96 | console.log('Ooops Push Couldn\'t Register', |
79 | 97 | '<p>When we tried to ' + |
80 | 98 | 'get the subscription ID for GCM, something went wrong, not ' + |
81 | 99 | |
82 | 100 | |
83 | 101 | |
... | ... | @@ -96,20 +114,24 @@ |
96 | 114 | function(pushSubscription) { |
97 | 115 | // Check we have everything we need to unsubscribe |
98 | 116 | if (!pushSubscription) { |
117 | + pushSwitch.checked = false; | |
99 | 118 | return; |
100 | 119 | } |
101 | 120 | |
102 | 121 | // TODO: Remove the device details from the server |
103 | 122 | // i.e. the pushSubscription.subscriptionId and |
104 | 123 | // pushSubscription.endpoint |
124 | + var subscriptionId = pushSubscription.subscriptionId; | |
105 | 125 | |
106 | 126 | pushSubscription.unsubscribe().then(function(successful) { |
107 | 127 | console.log('Unsubscribed from push: ', successful); |
128 | + | |
108 | 129 | if (!successful) { |
109 | 130 | // The unsubscribe was unsuccessful, but we can |
110 | 131 | // remove the subscriptionId from our server |
111 | 132 | // and notifications will stop |
112 | 133 | // This just may be in a bad state when the user returns |
134 | + pushSwitch.checked = true; | |
113 | 135 | console.error('We were unable to unregister from push'); |
114 | 136 | } |
115 | 137 | |
... | ... | @@ -128,6 +150,7 @@ |
128 | 150 | // If the notification permission is denied, it's a permanent block |
129 | 151 | switch (permissionStatus.status) { |
130 | 152 | case 'denied': |
153 | + pushSwitch.disabled = true; | |
131 | 154 | console.log('Ooops Push has been Blocked', |
132 | 155 | 'Unfortunately the user permanently blocked push. Please unblock / ' + |
133 | 156 | 'allow them to switch on push notifications.'); |
... | ... | @@ -137,6 +160,7 @@ |
137 | 160 | console.log('case granted'); |
138 | 161 | break; |
139 | 162 | case 'prompt': |
163 | + pushSwitch.checked = false; | |
140 | 164 | console.log('case prompt'); |
141 | 165 | break; |
142 | 166 | } |
... | ... | @@ -163,6 +187,7 @@ |
163 | 187 | return; |
164 | 188 | } |
165 | 189 | |
190 | + console.log('update current state.'); | |
166 | 191 | // Update the current state with the |
167 | 192 | // subscriptionid and endpoint |
168 | 193 | onPushSubscription(subscription); |
... | ... | @@ -179,13 +204,9 @@ |
179 | 204 | } |
180 | 205 | |
181 | 206 | function setUpNotificationPermission() { |
182 | - // If the notification permission is denied, it's a permanent block | |
183 | - if (Notification.permission === 'denied') { | |
184 | - console.log('Ooops Notifications are Blocked', | |
185 | - 'Unfortunately notifications are permanently blocked. Please unblock / ' + | |
186 | - 'allow them to switch on push notifications.'); | |
187 | - return; | |
188 | - } else if (Notification.permission === 'default') { | |
207 | + console.log('setting notification setting'); | |
208 | + | |
209 | + if (Notification.permission === 'default') { | |
189 | 210 | console.log('notification permissions === default'); |
190 | 211 | return; |
191 | 212 | } |
... | ... | @@ -214,6 +235,27 @@ |
214 | 235 | |
215 | 236 | // Once the service worker is registered set the initial state |
216 | 237 | function initialiseState() { |
238 | + // Check if notifications are supported | |
239 | + if (!('showNotification' in ServiceWorkerRegistration.prototype)) { | |
240 | + console.warn('Notifications aren\'t supported.'); | |
241 | + return; | |
242 | + } | |
243 | + // Check the current Notification permission. | |
244 | + // If its denied, it's a permanent block until the | |
245 | + // user changes the permission | |
246 | + else if (Notification.permission === 'denied') { | |
247 | + console.log('Ooops Notifications are Blocked', | |
248 | + 'Unfortunately notifications are permanently blocked. Please unblock / ' + | |
249 | + 'allow them to switch on push notifications.'); | |
250 | + return; | |
251 | + } | |
252 | + // Check if push messaging is supported | |
253 | + else if (!('PushManager' in window)) { | |
254 | + console.warn('Push messaging isn\'t supported.'); | |
255 | + return; | |
256 | + } | |
257 | + | |
258 | + pushSwitch.disabled = false; | |
217 | 259 | // Is the Permissions API supported |
218 | 260 | if ('permissions' in navigator) { |
219 | 261 | console.log('setting push permissions'); |
... | ... | @@ -226,6 +268,9 @@ |
226 | 268 | } |
227 | 269 | |
228 | 270 | var enablePushSwitch = $('.js-checkbox'); |
271 | + var pushSwitch = document.getElementById("notifbox"); | |
272 | + pushSwitch.disabled = true; | |
273 | + | |
229 | 274 | enablePushSwitch.change(function(e) { |
230 | 275 | console.log('checkbox changed'); |
231 | 276 | if (e.target.checked) { |
... | ... | @@ -242,6 +287,7 @@ |
242 | 287 | navigator.serviceWorker.register('service-worker.js') |
243 | 288 | .then(initialiseState); |
244 | 289 | } else { |
290 | + // disable button if serviceworker is not available | |
245 | 291 | // Service Workers aren't supported so you should hide the push UI |
246 | 292 | // If it's currently visible. |
247 | 293 | console.log('Ooops Service Workers aren\'t Supported', |