firebase-messaging-sw.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. importScripts('https://www.gstatic.com/firebasejs/10.13.2/firebase-app-compat.js');
  2. importScripts('https://www.gstatic.com/firebasejs/10.13.2/firebase-messaging-compat.js');
  3. // if ('serviceWorker' in navigator) {
  4. // navigator.serviceWorker.register('./firebase-messaging-sw.js').then(function (reg) {
  5. // console.log('reg: ', reg.scope);
  6. // }).catch(function (err) {
  7. // console.log('err: ', err);
  8. // })
  9. // }
  10. class CustomPushEvent extends Event {
  11. constructor(data) {
  12. super('push')
  13. Object.assign(this, data)
  14. this.custom = true
  15. }
  16. }
  17. console.log(`🚀🚀🚀🚀🚀-> in firebase-messaging-sw.js on 19`,process);
  18. firebase.initializeApp({
  19. apiKey: process.env.NEXT_PUBLIC_FIREBASE_APIKEY,
  20. authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTHDOMAIN,
  21. projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECTID,
  22. storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGEBUCKET,
  23. messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGINGSENDERID,
  24. appId: process.env.NEXT_PUBLIC_FIREBASE_APPID,
  25. measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENTID,
  26. });
  27. // Retrieve an instance of Firebase Messaging so that it can handle background
  28. // messages.
  29. self.addEventListener('push', (e) => {
  30. // Skip if event is our own custom event
  31. if (e.custom) return;
  32. // Kep old event data to override
  33. let oldData = e.data
  34. // Create a new event to dispatch, pull values from notification key and put it in data key,
  35. // and then remove notification key
  36. let newEvent = new CustomPushEvent({
  37. data: {
  38. json() {
  39. let newData = oldData.json()
  40. newData.data = {
  41. ...newData.data,
  42. ...newData.notification
  43. }
  44. delete newData.notification
  45. return newData
  46. },
  47. },
  48. waitUntil: e.waitUntil.bind(e),
  49. })
  50. // Stop event propagation
  51. e.stopImmediatePropagation()
  52. // Dispatch the new wrapped event
  53. dispatchEvent(newEvent)
  54. })
  55. const messaging = firebase.messaging();
  56. messaging.onBackgroundMessage( (payload) => {
  57. console.log(`🚀🚀🚀🚀🚀-> in firebase-messaging-sw.js on 24`,payload)
  58. // Customize notification here
  59. const notificationTitle = payload.data.title;
  60. const notificationOptions = {
  61. body: payload.data.body,
  62. icon: '/logo.png',
  63. data: payload.data
  64. };
  65. return self.registration.showNotification(notificationTitle,
  66. notificationOptions);
  67. });
  68. self.onnotificationclick = function (event) {
  69. console.log(`🚀🚀🚀🚀🚀-> in firebase-messaging-sw.js on 91`,event);
  70. //example
  71. const endpoint = event.notification.data.type;
  72. self.clients.openWindow('/' + endpoint || "")
  73. event.notification.close()
  74. }