diff --git a/scripts/SettingsController.js b/scripts/SettingsController.js index b74f1a1..5ea8a91 100644 --- a/scripts/SettingsController.js +++ b/scripts/SettingsController.js @@ -12,6 +12,7 @@ angular.module('flashy.SettingsController', ['ui.router']). console.log('executing things outside of module'); var PUSH_SERVER_URL = '/api/subscribe/'; + var UNPUSH_SERVER_URL = '/api/unsubscribe/'; function onPushSubscription(pushSubscription) { console.log('pushSubscription = ', pushSubscription.endpoint); @@ -27,6 +28,16 @@ angular.module('flashy.SettingsController', ['ui.router']). $http.post(PUSH_SERVER_URL, {'registration_id': subscriptionId}); } + function removeSubscription(pushSubscription) { + console.log('removing subscription'); + console.log('pushSubscription endpoint = ', pushSubscription.endpoint); + + var subscriptionId = pushSubscription.subscriptionId; + + console.log('registration_id: ', subscriptionId); + $http.post(UNPUSH_SERVER_URL, {'registration_id': subscriptionId}); + } + function subscribeDevice() { // We need the service worker registration to access the push manager navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { @@ -41,12 +52,15 @@ angular.module('flashy.SettingsController', ['ui.router']). console.log('subscribe() Error: Push permission status = ', permissionStatus); if (permissionStatus.status === 'denied') { + pushSwitch.checked = false; + pushSwitch.disabled = true; // The user blocked the permission prompt console.log('Ooops Notifications are Blocked', 'Unfortunately you just permanently blocked notifications. ' + 'Please unblock / allow them to switch on push ' + 'notifications.'); } else { + pushSwitch.checked = false; console.log('Ooops Push Couldn\'t Register', '

When we tried to ' + 'get the subscription ID for GCM, something went wrong,' + @@ -58,6 +72,7 @@ angular.module('flashy.SettingsController', ['ui.router']). '

'); } }).catch(function(err) { + pushSwitch.checked = false; console.log('Ooops Push Couldn\'t Register', '

When we tried to ' + 'get the subscription ID for GCM, something went wrong, not ' + @@ -71,10 +86,13 @@ angular.module('flashy.SettingsController', ['ui.router']). } else { // Use notification permission to do something if (Notification.permission === 'denied') { + pushSwitch.disabled = true; + pushSwitch.checked = false; console.log('Ooops Notifications are Blocked', 'Unfortunately you just permanently blocked notifications. ' + 'Please unblock / allow them to switch on push notifications.'); } else { + pushSwitch.checked = false; console.log('Ooops Push Couldn\'t Register', '

When we tried to ' + 'get the subscription ID for GCM, something went wrong, not ' + @@ -96,20 +114,24 @@ angular.module('flashy.SettingsController', ['ui.router']). function(pushSubscription) { // Check we have everything we need to unsubscribe if (!pushSubscription) { + pushSwitch.checked = false; return; } // TODO: Remove the device details from the server // i.e. the pushSubscription.subscriptionId and // pushSubscription.endpoint + var subscriptionId = pushSubscription.subscriptionId; pushSubscription.unsubscribe().then(function(successful) { console.log('Unsubscribed from push: ', successful); + if (!successful) { // The unsubscribe was unsuccessful, but we can // remove the subscriptionId from our server // and notifications will stop // This just may be in a bad state when the user returns + pushSwitch.checked = true; console.error('We were unable to unregister from push'); } @@ -128,6 +150,7 @@ angular.module('flashy.SettingsController', ['ui.router']). // If the notification permission is denied, it's a permanent block switch (permissionStatus.status) { case 'denied': + pushSwitch.disabled = true; console.log('Ooops Push has been Blocked', 'Unfortunately the user permanently blocked push. Please unblock / ' + 'allow them to switch on push notifications.'); @@ -137,6 +160,7 @@ angular.module('flashy.SettingsController', ['ui.router']). console.log('case granted'); break; case 'prompt': + pushSwitch.checked = false; console.log('case prompt'); break; } @@ -163,6 +187,7 @@ angular.module('flashy.SettingsController', ['ui.router']). return; } + console.log('update current state.'); // Update the current state with the // subscriptionid and endpoint onPushSubscription(subscription); @@ -179,13 +204,9 @@ angular.module('flashy.SettingsController', ['ui.router']). } function setUpNotificationPermission() { - // If the notification permission is denied, it's a permanent block - if (Notification.permission === 'denied') { - console.log('Ooops Notifications are Blocked', - 'Unfortunately notifications are permanently blocked. Please unblock / ' + - 'allow them to switch on push notifications.'); - return; - } else if (Notification.permission === 'default') { + console.log('setting notification setting'); + + if (Notification.permission === 'default') { console.log('notification permissions === default'); return; } @@ -214,6 +235,27 @@ angular.module('flashy.SettingsController', ['ui.router']). // Once the service worker is registered set the initial state function initialiseState() { + // Check if notifications are supported + if (!('showNotification' in ServiceWorkerRegistration.prototype)) { + console.warn('Notifications aren\'t supported.'); + return; + } + // Check the current Notification permission. + // If its denied, it's a permanent block until the + // user changes the permission + else if (Notification.permission === 'denied') { + console.log('Ooops Notifications are Blocked', + 'Unfortunately notifications are permanently blocked. Please unblock / ' + + 'allow them to switch on push notifications.'); + return; + } + // Check if push messaging is supported + else if (!('PushManager' in window)) { + console.warn('Push messaging isn\'t supported.'); + return; + } + + pushSwitch.disabled = false; // Is the Permissions API supported if ('permissions' in navigator) { console.log('setting push permissions'); @@ -226,6 +268,9 @@ angular.module('flashy.SettingsController', ['ui.router']). } var enablePushSwitch = $('.js-checkbox'); + var pushSwitch = document.getElementById("notifbox"); + pushSwitch.disabled = true; + enablePushSwitch.change(function(e) { console.log('checkbox changed'); if (e.target.checked) { @@ -242,6 +287,7 @@ angular.module('flashy.SettingsController', ['ui.router']). navigator.serviceWorker.register('service-worker.js') .then(initialiseState); } else { + // disable button if serviceworker is not available // Service Workers aren't supported so you should hide the push UI // If it's currently visible. console.log('Ooops Service Workers aren\'t Supported',