Commit e019a8dfd12068c7288a5ff5f74639810e23107c

Authored by Chung Wang
1 parent 0aff91dfe2

unscribe device removes registration id from the server

Showing 1 changed file with 3 additions and 1 deletions Inline Diff

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