firebase-messaging-sw.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // eslint-disable-next-line
  2. importScripts('https://www.gstatic.com/firebasejs/7.19.1/firebase-app.js')
  3. // eslint-disable-next-line
  4. importScripts('https://www.gstatic.com/firebasejs/7.19.1/firebase-messaging.js')
  5. const firebaseConfig = {
  6. apiKey: 'AIzaSyBMSKsBzEFtfBHkudjHuLr9brCuRUJQYX4',
  7. authDomain: 'alaa-office.firebaseapp.com',
  8. databaseURL: 'https://alaa-office.firebaseio.com',
  9. projectId: 'alaa-office',
  10. storageBucket: 'alaa-office.appspot.com',
  11. messagingSenderId: '300754869233',
  12. appId: '1:300754869233:web:c730b68385257132ed8250',
  13. measurementId: 'G-V614DM1FRK'
  14. }
  15. self.addEventListener('install', () => {
  16. })
  17. // eslint-disable-next-line
  18. firebase.initializeApp(firebaseConfig)
  19. // eslint-disable-next-line
  20. const messaging = firebase.messaging()
  21. messaging.setBackgroundMessageHandler(function (payload) {
  22. // eslint-disable-next-line
  23. const promiseChain = clients.matchAll({
  24. type: 'window',
  25. includeUncontrolled: true
  26. })
  27. .then((windowClients) => {
  28. for (let i = 0; i < windowClients.length; i++) {
  29. const windowClient = windowClients[i]
  30. windowClient.postMessage(payload)
  31. }
  32. })
  33. return promiseChain
  34. })
  35. self.addEventListener('notificationclick', function (event) {
  36. event.waitUntil(self.clients.openWindow(event.currentTarget.dataEventJson.click_action))
  37. })
  38. messaging.getToken({ vapidKey: 'BKJlaTO0dnXtHHFho3i53VF_mGMkyxSv0dnC7ldF1wTZ8sRgXQIzYu2P4O3l2n0yKQ0H8BYcq86VOjbHAKAIFZY' })
  39. .then((currentToken) => {
  40. if (currentToken) {
  41. // console.log('Send the token to your server and update the UI if necessary')
  42. console.info('client token', currentToken)
  43. // Send the token to your server and update the UI if necessary
  44. // ...
  45. } else {
  46. // Show permission request UI
  47. console.info('No registration token available. Request permission to generate one.')
  48. // ...
  49. }
  50. })
  51. .catch((err) => {
  52. console.info('An error occurred while retrieving token. ', err)
  53. // ...
  54. })
  55. // // Initialize Firebase
  56. // import { initializeApp } from 'firebase/app'
  57. // import { onBackgroundMessage } from 'firebase/messaging/sw'
  58. // import { getMessaging, getToken, onMessage } from 'firebase/messaging'
  59. //
  60. // const firebaseConfig = {
  61. // apiKey: 'AIzaSyBMSKsBzEFtfBHkudjHuLr9brCuRUJQYX4',
  62. // authDomain: 'alaa-office.firebaseapp.com',
  63. // databaseURL: 'https://alaa-office.firebaseio.com',
  64. // projectId: 'alaa-office',
  65. // storageBucket: 'alaa-office.appspot.com',
  66. // messagingSenderId: '300754869233',
  67. // appId: '1:300754869233:web:c730b68385257132ed8250',
  68. // measurementId: 'G-V614DM1FRK'
  69. // }
  70. //
  71. // const app = initializeApp(firebaseConfig)
  72. // const messaging = getMessaging(app)
  73. //
  74. // console.log('firebase-messaging-sw.js')
  75. //
  76. // getToken(messaging, { vapidKey: 'BKJlaTO0dnXtHHFho3i53VF_mGMkyxSv0dnC7ldF1wTZ8sRgXQIzYu2P4O3l2n0yKQ0H8BYcq86VOjbHAKAIFZY' })
  77. // .then((currentToken) => {
  78. // if (currentToken) {
  79. // console.log('Send the token to your server and update the UI if necessary')
  80. // console.log('currentToken: ', currentToken)
  81. // // Send the token to your server and update the UI if necessary
  82. // // ...
  83. // } else {
  84. // // Show permission request UI
  85. // console.log('No registration token available. Request permission to generate one.')
  86. // // ...
  87. // }
  88. // })
  89. // .catch((err) => {
  90. // console.log('An error occurred while retrieving token. ', err)
  91. // // ...
  92. // })
  93. //
  94. // onMessage(messaging, (payload) => {
  95. // console.log('Message received. ', payload)
  96. // // ...
  97. // })
  98. //
  99. // onBackgroundMessage(messaging, (payload) => {
  100. // console.log('[firebase-messaging-sw.js] Received background message ', payload)
  101. // // Customize notification here
  102. // const notificationTitle = 'Background Message Title'
  103. // const notificationOptions = {
  104. // body: 'Background Message body.',
  105. // icon: '/firebase-logo.png'
  106. // }
  107. //
  108. // self.registration.showNotification(notificationTitle,
  109. // notificationOptions)
  110. // })