firebase-messaging-sw.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // eslint-disable-next-line
  2. importScripts('https://www.gstatic.com/firebasejs/8.2.4/firebase-app.js');
  3. // eslint-disable-next-line
  4. importScripts('https://www.gstatic.com/firebasejs/8.2.4/firebase-messaging.js');
  5. const firebaseConfig = {
  6. apiKey: 'AIzaSyCaNMtlSmxykF-yZoTGGU6fRFOR_U9koBU',
  7. authDomain: 'testalaa-3a227.firebaseapp.com',
  8. projectId: 'testalaa-3a227',
  9. storageBucket: 'testalaa-3a227.appspot.com',
  10. messagingSenderId: '844877992016',
  11. appId: '1:844877992016:web:9d1143b2d68150a5264155',
  12. measurementId: 'G-14ZKWSMWNN'
  13. }
  14. // // Import the functions you need from the SDKs you need
  15. // import { initializeApp } from 'firebase/app'
  16. // import { getMessaging } from 'firebase/messaging'
  17. // // TODO: Add SDKs for Firebase products that you want to use
  18. // // https://firebase.google.com/docs/web/setup#available-libraries
  19. // // Initialize Firebase
  20. // const app = initializeApp(firebaseConfig)
  21. // // Initialize Firebase Cloud Messaging and get a reference to the service
  22. // const messaging = getMessaging(app)
  23. // eslint-disable-next-line
  24. firebase.initializeApp(firebaseConfig)
  25. // eslint-disable-next-line
  26. const messaging = firebase.messaging()
  27. messaging.getToken({ vapidKey: 'BO_z4fxtwD_L-TTQMh3OE7iIX77M2jezra4Kj7x9dI_nE8_OdLtmxUhJrvTOtS9sSClKqe1pEMA6gLNJr8sdfPY' })
  28. .then((currentToken) => {
  29. if (currentToken) {
  30. // eslint-disable-next-line
  31. console.warn('client token', currentToken)
  32. const channel = new BroadcastChannel('sw-fcm-token')
  33. channel.postMessage({ title: 'Hello from SW', fcm: currentToken })
  34. } else {
  35. // Show permission request UI
  36. const channel = new BroadcastChannel('sw-permission-request')
  37. channel.postMessage()
  38. // eslint-disable-next-line
  39. console.log('No registration token available. Request permission to generate one.')
  40. // ...
  41. }
  42. })
  43. .catch((err) => {
  44. // eslint-disable-next-line
  45. console.warn('An error occurred while retrieving token. ', err)
  46. // ...
  47. })