From 17907a95c7a56da82008ccfe4d7749847bf79d90 Mon Sep 17 00:00:00 2001
From: Andrew Buss When we tried to ' +
+ 'get the subscription ID for GCM, something went wrong,' +
+ ' not sure why. Have you defined "gcm_sender_id" and ' +
+ '"gcm_user_visible_only" in the manifest? Error message: ' +
+ e.message +
+ ' When we tried to ' +
+ 'get the subscription ID for GCM, something went wrong, not ' +
+ 'sure why. Have you defined "gcm_sender_id" and ' +
+ '"gcm_user_visible_only" in the manifest? Error message: ' +
+ err.message +
+ ' When we tried to ' +
- 'get the subscription ID for GCM, something went wrong,' +
- ' not sure why.
Have you defined "gcm_sender_id" and ' + '"gcm_user_visible_only" in the manifest?
' + 'Error message: ' + e.message + '
'); } - }).catch(function(err) { - console.log('Ooops Push Couldn\'t Register', - 'When we tried to ' + - 'get the subscription ID for GCM, something went wrong, not ' + - 'sure why.
' + - 'Have you defined "gcm_sender_id" and ' + - '"gcm_user_visible_only" in the manifest?
' + - 'Error message: ' + - err.message + - '
'); - }); - } else { - // Use notification permission to do something - if (Notification.permission === 'denied') { - console.log('Ooops Notifications are Blocked', - 'Unfortunately you just permanently blocked notifications. ' + - 'Please unblock / allow them to switch on push notifications.'); - } else { - console.log('Ooops Push Couldn\'t Register', - 'When we tried to ' + - 'get the subscription ID for GCM, something went wrong, not ' + - 'sure why.
' + - 'Have you defined "gcm_sender_id" and ' + - '"gcm_user_visible_only" in the manifest?
' + - 'Error message: ' + - e.message + - '
'); - } - } + } + }); }); - }); -} + } -function unsubscribeDevice() { - navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { - serviceWorkerRegistration.pushManager.getSubscription().then( - function(pushSubscription) { - // Check we have everything we need to unsubscribe - if (!pushSubscription) { - return; - } + function unsubscribeDevice() { + navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { + serviceWorkerRegistration.pushManager.getSubscription().then( + function(pushSubscription) { + // Check we have everything we need to unsubscribe + if (!pushSubscription) { + return; + } - // TODO: Remove the device details from the server - // i.e. the pushSubscription.subscriptionId and - // pushSubscription.endpoint + // TODO: Remove the device details from the server + // i.e. the pushSubscription.subscriptionId and + // pushSubscription.endpoint - 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 - console.error('We were unable to unregister from push'); - } + 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 + console.error('We were unable to unregister from push'); + } - }).catch(function(e) { - console.log('Unsubscribtion error: ', e); - }); - }.bind(this)).catch(function(e) { - console.error('Error thrown while revoking push notifications. ' + - 'Most likely because push was never registered', e); + }).catch(function(e) { + console.log('Unsubscribtion error: ', e); + }); + }.bind(this)).catch(function(e) { + console.error('Error thrown while revoking push notifications. ' + + 'Most likely because push was never registered', e); + }); }); - }); -} + } + + function permissionStatusChange(permissionStatus) { + console.log('permissionStatusChange = ', permissionStatus); + // If the notification permission is denied, it's a permanent block + switch (permissionStatus.status) { + case 'denied': + console.log('Ooops Push has been Blocked', + 'Unfortunately the user permanently blocked push. Please unblock / ' + + 'allow them to switch on push notifications.'); + break; + case 'granted': + // Set the state of the push switch + console.log('case granted'); + break; + case 'prompt': + console.log('case prompt'); + break; + } + } -function permissionStatusChange(permissionStatus) { - console.log('permissionStatusChange = ', permissionStatus); - // If the notification permission is denied, it's a permanent block - switch (permissionStatus.status) { - case 'denied': - console.log('Ooops Push has been Blocked', - 'Unfortunately the user permanently blocked push. Please unblock / ' + - 'allow them to switch on push notifications.'); - break; - case 'granted': - // Set the state of the push switch - console.log('case granted'); - break; - case 'prompt': - console.log('case prompt'); - break; - } -} + function setUpPushPermission() { + navigator.permissions.query({name: 'push', userVisibleOnly: true}) + .then(function(permissionStatus) { + // Set the initial state + permissionStatusChange(permissionStatus); -function setUpPushPermission() { - navigator.permissions.query({name: 'push', userVisibleOnly: true}) - .then(function(permissionStatus) { - // Set the initial state - permissionStatusChange(permissionStatus); + // Handle Permission State Changes + permissionStatus.onchange = function() { + permissionStatusChange(this); + }; + + // Check if push is supported and what the current state is + navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { + // Let's see if we have a subscription already + serviceWorkerRegistration.pushManager.getSubscription() + .then(function(subscription) { + if (!subscription) { + // NOOP + return; + } + + // Update the current state with the + // subscriptionid and endpoint + onPushSubscription(subscription); + }) + .catch(function(e) { + console.log('An error occured while calling getSubscription()', e); + }); + }); + }).catch(function(err) { + console.log('Ooops Unable to check the permission', + 'Unfortunately the permission for push notifications couldn\'t be ' + + 'checked. Are you on Chrome 43+?'); + }); + } - // Handle Permission State Changes - permissionStatus.onchange = function() { - permissionStatusChange(this); - }; + 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('notification permissions === default'); + return; + } // Check if push is supported and what the current state is navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { @@ -187,93 +223,57 @@ function setUpPushPermission() { .then(function(subscription) { if (!subscription) { // NOOP + console.log('not subscription'); return; } // Update the current state with the // subscriptionid and endpoint + console.log('onpushsubscription should be entered'); onPushSubscription(subscription); }) .catch(function(e) { console.log('An error occured while calling getSubscription()', e); }); }); - }).catch(function(err) { - console.log('Ooops Unable to check the permission', - 'Unfortunately the permission for push notifications couldn\'t be ' + - 'checked. Are you on Chrome 43+?'); - }); -} - -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('notification permissions === default'); - return; - } + } - // Check if push is supported and what the current state is - navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { - // Let's see if we have a subscription already - serviceWorkerRegistration.pushManager.getSubscription() - .then(function(subscription) { - if (!subscription) { - // NOOP - console.log('not subscription'); - return; - } +// Once the service worker is registered set the initial state + function initialiseState() { + // Is the Permissions API supported + if ('permissions' in navigator) { + console.log('setting push permissions'); + setUpPushPermission(); + return; + } else { + console.log('setting notification permissions'); + setUpNotificationPermission(); + } + } - // Update the current state with the - // subscriptionid and endpoint - console.log('onpushsubscription should be entered'); - onPushSubscription(subscription); - }) - .catch(function(e) { - console.log('An error occured while calling getSubscription()', e); - }); - }); -} + var enablePushSwitch = $('.js-checkbox'); + enablePushSwitch.change(function(e) { + console.log('checkbox changed'); + if (e.target.checked) { + console.log('subscribing device'); + subscribeDevice(); + } else { + console.log('unsubscribing device'); + unsubscribeDevice(); + } + }); -// Once the service worker is registered set the initial state -function initialiseState() { - // Is the Permissions API supported - if ('permissions' in navigator) { - console.log('setting push permissions'); - setUpPushPermission(); - return; - } else { - console.log('setting notification permissions'); - setUpNotificationPermission(); - } -} -window.addEventListener('load', function() { - // When the toggle switch changes, enabled / disable push - // messaging - var enablePushSwitch = document.querySelector('.js-checkbox'); - enablePushSwitch.addEventListener('change', function(e) { - if (e.target.checked) { - subscribeDevice(); + // Check that service workers are supported + if ('serviceWorker' in navigator) { + navigator.serviceWorker.register('scripts/service-worker.js') + .then(initialiseState); } else { - unsubscribeDevice(); + // 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', + 'Service Workers aren\'t supported in this browser. ' + + 'For this demo be sure to use ' + + 'Chrome Canary' + + ' or version 42.'); } }); - - // Check that service workers are supported - if ('serviceWorker' in navigator) { - navigator.serviceWorker.register('scripts/service-worker.js') - .then(initialiseState); - } else { - // 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', - 'Service Workers aren\'t supported in this browser. ' + - 'For this demo be sure to use ' + - 'Chrome Canary' + - ' or version 42.'); - } -}); -- 1.9.1