Licitator 1.0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
6.7 KiB

5 years ago
  1. let isPushEnabled = false;
  2. let registration;
  3. let pushButton;
  4. window.addEventListener('load', function() {
  5. pushButton = document.querySelector('.js-push-toggle');
  6. if(pushButton !== undefined){
  7. pushButton.addEventListener('change', function() {
  8. document.querySelector('.label-text').textContent = this.checked ? 'Disable Push Messages' : 'Enable Push Messages';
  9. if (isPushEnabled) {
  10. unsubscribe();
  11. } else {
  12. subscribe();
  13. }
  14. });
  15. }
  16. // Check that service workers are supported, if so, progressively
  17. // enhance and add push messaging support, otherwise continue without it.
  18. if ('serviceWorker' in navigator) {
  19. navigator.serviceWorker.register('/service-worker.js')
  20. .then(initialiseState);
  21. } else {
  22. console.warn('Service workers aren\'t supported in this browser.');
  23. }
  24. });
  25. function initialiseState() {
  26. // Are Notifications supported in the service worker?
  27. if (!('showNotification' in ServiceWorkerRegistration.prototype)) {
  28. console.warn('Notifications aren\'t supported.');
  29. return;
  30. }
  31. // Check the current Notification permission.
  32. // If its denied, it's a permanent block until the
  33. // user changes the permission
  34. if (Notification.permission === 'denied') {
  35. console.warn('The user has blocked notifications.');
  36. return;
  37. }
  38. // Check if push messaging is supported
  39. if (!('PushManager' in window)) {
  40. console.warn('Push messaging isn\'t supported.');
  41. return;
  42. }
  43. // We need the service worker registration to check for a subscription
  44. navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
  45. // Do we already have a push message subscription?
  46. registration = serviceWorkerRegistration;
  47. serviceWorkerRegistration.pushManager.getSubscription()
  48. .then(function(subscription) {
  49. // Enable any UI which subscribes / unsubscribes from
  50. // push messages.
  51. //var pushButton = document.querySelector('.js-push-toggle');
  52. pushButton.disabled = false;
  53. if (!subscription) {
  54. // We aren't subscribed to push, so set UI
  55. // to allow the user to enable push
  56. return;
  57. }
  58. // Keep your server in sync with the latest subscriptionId
  59. //sendSubscriptionToServer(subscription);
  60. // Set your UI to show they have subscribed for
  61. // push messages
  62. document.querySelector('.label-text').text = 'Disable Push Messages';
  63. isPushEnabled = true;
  64. })
  65. .catch(function(err) {
  66. console.warn('Error during getSubscription()', err);
  67. });
  68. });
  69. }
  70. function subscribe() {
  71. // Disable the button so it can't be changed while
  72. // we process the permission request
  73. var pushButton = document.querySelector('.js-push-toggle');
  74. pushButton.disabled = true;
  75. navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
  76. serviceWorkerRegistration.pushManager.subscribe()
  77. .then(function(subscription) {
  78. // The subscription was successful
  79. isPushEnabled = true;
  80. pushButton.textContent = 'Disable Push Messages';
  81. pushButton.disabled = false;
  82. // TODO: Send the subscription.endpoint to your server
  83. // and save it to send a push message at a later date
  84. return sendSubscriptionToServer(subscription);
  85. })
  86. .catch(function(e) {
  87. if (Notification.permission === 'denied') {
  88. // The user denied the notification permission which
  89. // means we failed to subscribe and the user will need
  90. // to manually change the notification permission to
  91. // subscribe to push messages
  92. console.warn('Permission for Notifications was denied');
  93. //pushButton.disabled = true;
  94. } else {
  95. // A problem occurred with the subscription; common reasons
  96. // include network errors, and lacking gcm_sender_id and/or
  97. // gcm_user_visible_only in the manifest.
  98. console.error('Unable to subscribe to push.', e);
  99. pushButton.disabled = false;
  100. document.querySelector('.label-text').text = 'Enable Push Messages';
  101. }
  102. });
  103. });
  104. }
  105. function unsubscribe() {
  106. var pushButton = document.querySelector('.js-push-toggle');
  107. pushButton.disabled = true;
  108. navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
  109. // To unsubscribe from push messaging, you need get the
  110. // subscription object, which you can call unsubscribe() on.
  111. serviceWorkerRegistration.pushManager.getSubscription().then(
  112. function(pushSubscription) {
  113. // Check we have a subscription to unsubscribe
  114. if (!pushSubscription) {
  115. // No subscription object, so set the state
  116. // to allow the user to subscribe to push
  117. isPushEnabled = false;
  118. pushButton.disabled = false;
  119. document.querySelector('.label-text').text = 'Enable Push Messages';
  120. return;
  121. }
  122. var subscriptionId = pushSubscription.subscriptionId;
  123. // TODO: Make a request to your server to remove
  124. // the subscriptionId from your data store so you
  125. // don't attempt to send them push messages anymore
  126. // We have a subscription, so call unsubscribe on it
  127. pushSubscription.unsubscribe().then(function(successful) {
  128. pushButton.disabled = false;
  129. document.querySelector('.label-text').text = 'Enable Push Messages';
  130. isPushEnabled = false;
  131. }).catch(function(e) {
  132. // We failed to unsubscribe, this can lead to
  133. // an unusual state, so may be best to remove
  134. // the users data from your data store and
  135. // inform the user that you have done so
  136. console.log('Unsubscription error: ', e);
  137. pushButton.disabled = false;
  138. document.querySelector('.label-text').text = 'Enable Push Messages';
  139. });
  140. }).catch(function(e) {
  141. console.error('Error thrown while unsubscribing from push messaging.', e);
  142. });
  143. });
  144. }