Commit d765b795b45ba13f1099584d60f29c4d10dd7a2e
1 parent
e019a8dfd1
Exists in
master
and in
1 other branch
Disable button if not android/chrome
Showing 1 changed file with 14 additions and 0 deletions Inline Diff
scripts/SettingsController.js
View file @
d765b79
angular.module('flashy.SettingsController', ['ui.router']). | 1 | 1 | angular.module('flashy.SettingsController', ['ui.router']). | |
2 | 2 | |||
controller('SettingsController', function($scope, $http) { | 3 | 3 | controller('SettingsController', function($scope, $http) { | |
$scope.changePassword = function(oldPassword, newPassword, confirmedNewPassword) { | 4 | 4 | $scope.changePassword = function(oldPassword, newPassword, confirmedNewPassword) { | |
5 | 5 | |||
}; | 6 | 6 | }; | |
console.log('checking to see if chrome'); | 7 | 7 | console.log('checking to see if chrome'); | |
if (!chrome) { | 8 | 8 | if (!chrome) { | |
9 | pushSwitch.disabled = true; | |||
return; | 9 | 10 | return; | |
} | 10 | 11 | } | |
12 | ||||
console.log('chrome'); | 11 | 13 | console.log('chrome'); | |
12 | 14 | |||
console.log('executing things outside of module'); | 13 | 15 | console.log('executing things outside of module'); | |
var PUSH_SERVER_URL = '/api/subscribe/'; | 14 | 16 | var PUSH_SERVER_URL = '/api/subscribe/'; | |
var UNPUSH_SERVER_URL = '/api/unsubscribe/'; | 15 | 17 | var UNPUSH_SERVER_URL = '/api/unsubscribe/'; | |
16 | 18 | |||
function onPushSubscription(pushSubscription) { | 17 | 19 | function onPushSubscription(pushSubscription) { | |
console.log('pushSubscription = ', pushSubscription.endpoint); | 18 | 20 | console.log('pushSubscription = ', pushSubscription.endpoint); | |
// Here we would normally send the endpoint | 19 | 21 | // Here we would normally send the endpoint | |
// and subscription ID to our server. | 20 | 22 | // and subscription ID to our server. | |
// In this demo we just use send these values to | 21 | 23 | // In this demo we just use send these values to | |
// our server via XHR which sends a push message. | 22 | 24 | // our server via XHR which sends a push message. | |
23 | 25 | |||
var endpoint = pushSubscription.endpoint; | 24 | 26 | var endpoint = pushSubscription.endpoint; | |
var subscriptionId = pushSubscription.subscriptionId; | 25 | 27 | var subscriptionId = pushSubscription.subscriptionId; | |
26 | 28 | |||
console.log('registration_id: ', subscriptionId); | 27 | 29 | console.log('registration_id: ', subscriptionId); | |
$http.post(PUSH_SERVER_URL, {'registration_id': subscriptionId}); | 28 | 30 | $http.post(PUSH_SERVER_URL, {'registration_id': subscriptionId}); | |
} | 29 | 31 | } | |
30 | 32 | |||
function removeSubscription(pushSubscription) { | 31 | 33 | function removeSubscription(pushSubscription) { | |
console.log('removing subscription'); | 32 | 34 | console.log('removing subscription'); | |
console.log('pushSubscription endpoint = ', pushSubscription.endpoint); | 33 | 35 | console.log('pushSubscription endpoint = ', pushSubscription.endpoint); | |
34 | 36 | |||
var subscriptionId = pushSubscription.subscriptionId; | 35 | 37 | var subscriptionId = pushSubscription.subscriptionId; | |
36 | 38 | |||
console.log('registration_id: ', subscriptionId); | 37 | 39 | console.log('registration_id: ', subscriptionId); | |
$http.post(UNPUSH_SERVER_URL, {'registration_id': subscriptionId}); | 38 | 40 | $http.post(UNPUSH_SERVER_URL, {'registration_id': subscriptionId}); | |
} | 39 | 41 | } | |
40 | 42 | |||
function subscribeDevice() { | 41 | 43 | function subscribeDevice() { | |
// We need the service worker registration to access the push manager | 42 | 44 | // We need the service worker registration to access the push manager | |
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { | 43 | 45 | navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { | |
serviceWorkerRegistration.pushManager.subscribe() | 44 | 46 | serviceWorkerRegistration.pushManager.subscribe() | |
.then(onPushSubscription) | 45 | 47 | .then(onPushSubscription) | |
.catch(function(e) { | 46 | 48 | .catch(function(e) { | |
console.log('Error in subscribing'); | 47 | 49 | console.log('Error in subscribing'); | |
// Check for a permission prompt issue | 48 | 50 | // Check for a permission prompt issue | |
if ('permissions' in navigator) { | 49 | 51 | if ('permissions' in navigator) { | |
navigator.permissions.query({name: 'push', userVisibleOnly: true}) | 50 | 52 | navigator.permissions.query({name: 'push', userVisibleOnly: true}) | |
.then(function(permissionStatus) { | 51 | 53 | .then(function(permissionStatus) { | |
console.log('subscribe() Error: Push permission status = ', | 52 | 54 | console.log('subscribe() Error: Push permission status = ', | |
permissionStatus); | 53 | 55 | permissionStatus); | |
if (permissionStatus.status === 'denied') { | 54 | 56 | if (permissionStatus.status === 'denied') { | |
pushSwitch.checked = false; | 55 | 57 | pushSwitch.checked = false; | |
pushSwitch.disabled = true; | 56 | 58 | pushSwitch.disabled = true; | |
// The user blocked the permission prompt | 57 | 59 | // The user blocked the permission prompt | |
console.log('Ooops Notifications are Blocked', | 58 | 60 | console.log('Ooops Notifications are Blocked', | |
'Unfortunately you just permanently blocked notifications. ' + | 59 | 61 | 'Unfortunately you just permanently blocked notifications. ' + | |
'Please unblock / allow them to switch on push ' + | 60 | 62 | 'Please unblock / allow them to switch on push ' + | |
'notifications.'); | 61 | 63 | 'notifications.'); | |
} else { | 62 | 64 | } else { | |
pushSwitch.checked = false; | 63 | 65 | pushSwitch.checked = false; | |
console.log('Ooops Push Couldn\'t Register', | 64 | 66 | console.log('Ooops Push Couldn\'t Register', | |
'<p>When we tried to ' + | 65 | 67 | '<p>When we tried to ' + | |
'get the subscription ID for GCM, something went wrong,' + | 66 | 68 | 'get the subscription ID for GCM, something went wrong,' + | |
' not sure why.</p>' + | 67 | 69 | ' not sure why.</p>' + | |
'<p>Have you defined "gcm_sender_id" and ' + | 68 | 70 | '<p>Have you defined "gcm_sender_id" and ' + | |
'"gcm_user_visible_only" in the manifest?</p>' + | 69 | 71 | '"gcm_user_visible_only" in the manifest?</p>' + | |
'<p>Error message: ' + | 70 | 72 | '<p>Error message: ' + | |
e.message + | 71 | 73 | e.message + | |
'</p>'); | 72 | 74 | '</p>'); | |
} | 73 | 75 | } | |
}).catch(function(err) { | 74 | 76 | }).catch(function(err) { | |
pushSwitch.checked = false; | 75 | 77 | pushSwitch.checked = false; | |
console.log('Ooops Push Couldn\'t Register', | 76 | 78 | console.log('Ooops Push Couldn\'t Register', | |
'<p>When we tried to ' + | 77 | 79 | '<p>When we tried to ' + | |
'get the subscription ID for GCM, something went wrong, not ' + | 78 | 80 | 'get the subscription ID for GCM, something went wrong, not ' + | |
'sure why.</p>' + | 79 | 81 | 'sure why.</p>' + | |
'<p>Have you defined "gcm_sender_id" and ' + | 80 | 82 | '<p>Have you defined "gcm_sender_id" and ' + | |
'"gcm_user_visible_only" in the manifest?</p>' + | 81 | 83 | '"gcm_user_visible_only" in the manifest?</p>' + | |
'<p>Error message: ' + | 82 | 84 | '<p>Error message: ' + | |
err.message + | 83 | 85 | err.message + | |
'</p>'); | 84 | 86 | '</p>'); | |
}); | 85 | 87 | }); | |
} else { | 86 | 88 | } else { | |
// Use notification permission to do something | 87 | 89 | // Use notification permission to do something | |
if (Notification.permission === 'denied') { | 88 | 90 | if (Notification.permission === 'denied') { | |
pushSwitch.disabled = true; | 89 | 91 | pushSwitch.disabled = true; | |
pushSwitch.checked = false; | 90 | 92 | pushSwitch.checked = false; | |
console.log('Ooops Notifications are Blocked', | 91 | 93 | console.log('Ooops Notifications are Blocked', | |
'Unfortunately you just permanently blocked notifications. ' + | 92 | 94 | 'Unfortunately you just permanently blocked notifications. ' + | |
'Please unblock / allow them to switch on push notifications.'); | 93 | 95 | 'Please unblock / allow them to switch on push notifications.'); | |
} else { | 94 | 96 | } else { | |
pushSwitch.checked = false; | 95 | 97 | pushSwitch.checked = false; | |
console.log('Ooops Push Couldn\'t Register', | 96 | 98 | console.log('Ooops Push Couldn\'t Register', | |
'<p>When we tried to ' + | 97 | 99 | '<p>When we tried to ' + | |
'get the subscription ID for GCM, something went wrong, not ' + | 98 | 100 | 'get the subscription ID for GCM, something went wrong, not ' + | |
'sure why.</p>' + | 99 | 101 | 'sure why.</p>' + | |
'<p>Have you defined "gcm_sender_id" and ' + | 100 | 102 | '<p>Have you defined "gcm_sender_id" and ' + | |
'"gcm_user_visible_only" in the manifest?</p>' + | 101 | 103 | '"gcm_user_visible_only" in the manifest?</p>' + | |
'<p>Error message: ' + | 102 | 104 | '<p>Error message: ' + | |
e.message + | 103 | 105 | e.message + | |
'</p>'); | 104 | 106 | '</p>'); | |
} | 105 | 107 | } | |
} | 106 | 108 | } | |
}); | 107 | 109 | }); | |
}); | 108 | 110 | }); | |
} | 109 | 111 | } | |
110 | 112 | |||
function unsubscribeDevice() { | 111 | 113 | function unsubscribeDevice() { | |
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { | 112 | 114 | navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { | |
serviceWorkerRegistration.pushManager.getSubscription().then( | 113 | 115 | serviceWorkerRegistration.pushManager.getSubscription().then( | |
function(pushSubscription) { | 114 | 116 | function(pushSubscription) { | |
// Check we have everything we need to unsubscribe | 115 | 117 | // Check we have everything we need to unsubscribe | |
if (!pushSubscription) { | 116 | 118 | if (!pushSubscription) { | |
pushSwitch.checked = false; | 117 | 119 | pushSwitch.checked = false; | |
return; | 118 | 120 | return; | |
} | 119 | 121 | } | |
120 | 122 | |||
// TODO: Remove the device details from the server | 121 | 123 | // TODO: Remove the device details from the server | |
// i.e. the pushSubscription.subscriptionId and | 122 | 124 | // i.e. the pushSubscription.subscriptionId and | |
// pushSubscription.endpoint | 123 | 125 | // pushSubscription.endpoint | |
var subscriptionId = pushSubscription.subscriptionId; | 124 | 126 | var subscriptionId = pushSubscription.subscriptionId; | |
125 | 127 | |||
pushSubscription.unsubscribe().then(function(successful) { | 126 | 128 | pushSubscription.unsubscribe().then(function(successful) { | |
console.log('Unsubscribed from push: ', successful); | 127 | 129 | console.log('Unsubscribed from push: ', successful); | |
128 | 130 | |||
if (!successful) { | 129 | 131 | if (!successful) { | |
// The unsubscribe was unsuccessful, but we can | 130 | 132 | // The unsubscribe was unsuccessful, but we can | |
// remove the subscriptionId from our server | 131 | 133 | // remove the subscriptionId from our server | |
// and notifications will stop | 132 | 134 | // and notifications will stop | |
// This just may be in a bad state when the user returns | 133 | 135 | // This just may be in a bad state when the user returns | |
pushSwitch.checked = true; | 134 | 136 | pushSwitch.checked = true; | |
removeSubscription(pushSubscription); | 135 | 137 | removeSubscription(pushSubscription); | |
console.error('We were unable to unregister from push, but we removed'+ | 136 | 138 | console.error('We were unable to unregister from push, but we removed'+ | |
'registration id from the server'); | 137 | 139 | 'registration id from the server'); | |
} | 138 | 140 | } | |
139 | 141 | |||
}).catch(function(e) { | 140 | 142 | }).catch(function(e) { | |
console.log('Unsubscribtion error: ', e); | 141 | 143 | console.log('Unsubscribtion error: ', e); | |
}); | 142 | 144 | }); | |
}.bind(this)).catch(function(e) { | 143 | 145 | }.bind(this)).catch(function(e) { | |
console.error('Error thrown while revoking push notifications. ' + | 144 | 146 | console.error('Error thrown while revoking push notifications. ' + | |
'Most likely because push was never registered', e); | 145 | 147 | 'Most likely because push was never registered', e); | |
}); | 146 | 148 | }); | |
}); | 147 | 149 | }); | |
} | 148 | 150 | } | |
149 | 151 | |||
function permissionStatusChange(permissionStatus) { | 150 | 152 | function permissionStatusChange(permissionStatus) { | |
console.log('permissionStatusChange = ', permissionStatus); | 151 | 153 | console.log('permissionStatusChange = ', permissionStatus); | |
// If the notification permission is denied, it's a permanent block | 152 | 154 | // If the notification permission is denied, it's a permanent block | |
switch (permissionStatus.status) { | 153 | 155 | switch (permissionStatus.status) { | |
case 'denied': | 154 | 156 | case 'denied': | |
pushSwitch.disabled = true; | 155 | 157 | pushSwitch.disabled = true; | |
console.log('Ooops Push has been Blocked', | 156 | 158 | console.log('Ooops Push has been Blocked', | |
'Unfortunately the user permanently blocked push. Please unblock / ' + | 157 | 159 | 'Unfortunately the user permanently blocked push. Please unblock / ' + | |
'allow them to switch on push notifications.'); | 158 | 160 | 'allow them to switch on push notifications.'); | |
break; | 159 | 161 | break; | |
case 'granted': | 160 | 162 | case 'granted': | |
// Set the state of the push switch | 161 | 163 | // Set the state of the push switch | |
console.log('case granted'); | 162 | 164 | console.log('case granted'); | |
break; | 163 | 165 | break; | |
case 'prompt': | 164 | 166 | case 'prompt': | |
pushSwitch.checked = false; | 165 | 167 | pushSwitch.checked = false; | |
console.log('case prompt'); | 166 | 168 | console.log('case prompt'); | |
break; | 167 | 169 | break; | |
} | 168 | 170 | } | |
} | 169 | 171 | } | |
170 | 172 | |||
function setUpPushPermission() { | 171 | 173 | function setUpPushPermission() { | |
navigator.permissions.query({name: 'push', userVisibleOnly: true}) | 172 | 174 | navigator.permissions.query({name: 'push', userVisibleOnly: true}) | |
.then(function(permissionStatus) { | 173 | 175 | .then(function(permissionStatus) { | |
// Set the initial state | 174 | 176 | // Set the initial state | |
permissionStatusChange(permissionStatus); | 175 | 177 | permissionStatusChange(permissionStatus); | |
176 | 178 | |||
// Handle Permission State Changes | 177 | 179 | // Handle Permission State Changes | |
permissionStatus.onchange = function() { | 178 | 180 | permissionStatus.onchange = function() { | |
permissionStatusChange(this); | 179 | 181 | permissionStatusChange(this); | |
}; | 180 | 182 | }; | |
181 | 183 | |||
// Check if push is supported and what the current state is | 182 | 184 | // Check if push is supported and what the current state is | |
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { | 183 | 185 | navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { | |
// Let's see if we have a subscription already | 184 | 186 | // Let's see if we have a subscription already | |
serviceWorkerRegistration.pushManager.getSubscription() | 185 | 187 | serviceWorkerRegistration.pushManager.getSubscription() | |
.then(function(subscription) { | 186 | 188 | .then(function(subscription) { | |
if (!subscription) { | 187 | 189 | if (!subscription) { | |
// NOOP | 188 | 190 | // NOOP | |
return; | 189 | 191 | return; | |
} | 190 | 192 | } | |
191 | 193 | |||
console.log('update current state.'); | 192 | 194 | console.log('update current state.'); | |
// Update the current state with the | 193 | 195 | // Update the current state with the | |
// subscriptionid and endpoint | 194 | 196 | // subscriptionid and endpoint | |
onPushSubscription(subscription); | 195 | 197 | onPushSubscription(subscription); | |
}) | 196 | 198 | }) | |
.catch(function(e) { | 197 | 199 | .catch(function(e) { | |
console.log('An error occured while calling getSubscription()', e); | 198 | 200 | console.log('An error occured while calling getSubscription()', e); | |
}); | 199 | 201 | }); | |
}); | 200 | 202 | }); | |
}).catch(function(err) { | 201 | 203 | }).catch(function(err) { | |
console.log('Ooops Unable to check the permission', | 202 | 204 | console.log('Ooops Unable to check the permission', | |
'Unfortunately the permission for push notifications couldn\'t be ' + | 203 | 205 | 'Unfortunately the permission for push notifications couldn\'t be ' + | |
'checked. Are you on Chrome 43+?'); | 204 | 206 | 'checked. Are you on Chrome 43+?'); | |
}); | 205 | 207 | }); | |
} | 206 | 208 | } | |
207 | 209 | |||
function setUpNotificationPermission() { | 208 | 210 | function setUpNotificationPermission() { | |
console.log('setting notification setting'); | 209 | 211 | console.log('setting notification setting'); | |
210 | 212 | |||
if (Notification.permission === 'default') { | 211 | 213 | if (Notification.permission === 'default') { | |
console.log('notification permissions === default'); | 212 | 214 | console.log('notification permissions === default'); | |
return; | 213 | 215 | return; | |
} | 214 | 216 | } | |
215 | 217 | |||
// Check if push is supported and what the current state is | 216 | 218 | // Check if push is supported and what the current state is | |
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { | 217 | 219 | navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { | |
// Let's see if we have a subscription already | 218 | 220 | // Let's see if we have a subscription already | |
serviceWorkerRegistration.pushManager.getSubscription() | 219 | 221 | serviceWorkerRegistration.pushManager.getSubscription() | |
.then(function(subscription) { | 220 | 222 | .then(function(subscription) { | |
if (!subscription) { | 221 | 223 | if (!subscription) { | |
// NOOP | 222 | 224 | // NOOP | |
console.log('not subscription'); | 223 | 225 | console.log('not subscription'); | |
return; | 224 | 226 | return; | |
} | 225 | 227 | } | |
226 | 228 | |||
// Update the current state with the | 227 | 229 | // Update the current state with the | |
// subscriptionid and endpoint | 228 | 230 | // subscriptionid and endpoint | |
console.log('onpushsubscription should be entered'); | 229 | 231 | console.log('onpushsubscription should be entered'); | |
onPushSubscription(subscription); | 230 | 232 | onPushSubscription(subscription); | |
}) | 231 | 233 | }) | |
.catch(function(e) { | 232 | 234 | .catch(function(e) { | |
console.log('An error occured while calling getSubscription()', e); | 233 | 235 | console.log('An error occured while calling getSubscription()', e); | |
}); | 234 | 236 | }); | |
}); | 235 | 237 | }); | |
} | 236 | 238 | } | |
237 | 239 | |||
// Once the service worker is registered set the initial state | 238 | 240 | // Once the service worker is registered set the initial state | |
function initialiseState() { | 239 | 241 | function initialiseState() { | |
// Check if notifications are supported | 240 | 242 | // Check if notifications are supported | |
if (!('showNotification' in ServiceWorkerRegistration.prototype)) { | 241 | 243 | if (!('showNotification' in ServiceWorkerRegistration.prototype)) { | |
console.warn('Notifications aren\'t supported.'); | 242 | 244 | console.warn('Notifications aren\'t supported.'); | |
return; | 243 | 245 | return; | |
} | 244 | 246 | } | |
// Check the current Notification permission. | 245 | 247 | // Check the current Notification permission. | |
// If its denied, it's a permanent block until the | 246 | 248 | // If its denied, it's a permanent block until the | |
// user changes the permission | 247 | 249 | // user changes the permission | |
else if (Notification.permission === 'denied') { | 248 | 250 | else if (Notification.permission === 'denied') { | |
console.log('Ooops Notifications are Blocked', | 249 | 251 | console.log('Ooops Notifications are Blocked', | |
'Unfortunately notifications are permanently blocked. Please unblock / ' + | 250 | 252 | 'Unfortunately notifications are permanently blocked. Please unblock / ' + | |
'allow them to switch on push notifications.'); | 251 | 253 | 'allow them to switch on push notifications.'); | |
return; | 252 | 254 | return; | |
} | 253 | 255 | } | |
// Check if push messaging is supported | 254 | 256 | // Check if push messaging is supported | |
else if (!('PushManager' in window)) { | 255 | 257 | else if (!('PushManager' in window)) { | |
console.warn('Push messaging isn\'t supported.'); | 256 | 258 | console.warn('Push messaging isn\'t supported.'); | |
return; | 257 | 259 | return; | |
} | 258 | 260 | } | |
259 | 261 | |||
pushSwitch.disabled = false; | 260 | 262 | pushSwitch.disabled = false; | |
// Is the Permissions API supported | 261 | 263 | // Is the Permissions API supported | |
if ('permissions' in navigator) { | 262 | 264 | if ('permissions' in navigator) { | |
console.log('setting push permissions'); | 263 | 265 | console.log('setting push permissions'); | |
setUpPushPermission(); | 264 | 266 | setUpPushPermission(); | |
return; | 265 | 267 | return; | |
} else { | 266 | 268 | } else { | |
console.log('setting notification permissions'); | 267 | 269 | console.log('setting notification permissions'); | |
setUpNotificationPermission(); | 268 | 270 | setUpNotificationPermission(); | |
} | 269 | 271 | } | |
} | 270 | 272 | } | |
271 | 273 | |||
var enablePushSwitch = $('.js-checkbox'); | 272 | 274 | var enablePushSwitch = $('.js-checkbox'); | |
275 | ||||
var pushSwitch = document.getElementById("notifbox"); | 273 | 276 | var pushSwitch = document.getElementById("notifbox"); | |
pushSwitch.disabled = true; | 274 | 277 | pushSwitch.disabled = true; | |
278 | ||||
279 | var ua = navigator.userAgent.toLowerCase(); | |||
280 | var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile"); | |||
281 | ||||
282 | if(!isAndroid) { | |||
283 | // Do something! | |||
284 | // Redirect to Android-site? | |||
285 | pushSwitch.disabled = true; | |||
286 | console.log("not android"); |