1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- class CustomPushEvent extends Event {
- constructor(data) {
- super("push");
- Object.assign(this, data);
- this.custom = true;
- }
- }
- if ("serviceWorker" in navigator) {
- importScripts("https://www.gstatic.com/firebasejs/10.13.2/firebase-app-compat.js");
- importScripts("https://www.gstatic.com/firebasejs/10.13.2/firebase-messaging-compat.js");
- importScripts("./swenv.js");
- navigator.serviceWorker
- .register("./firebase-messaging-sw.js")
- .then(function (reg) {
- firebase.initializeApp({
- apiKey: process.env.NEXT_PUBLIC_FIREBASE_APIKEY,
- authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTHDOMAIN,
- projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECTID,
- storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGEBUCKET,
- messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGINGSENDERID,
- appId: process.env.NEXT_PUBLIC_FIREBASE_APPID,
- measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENTID,
- });
- self.addEventListener("push", (e) => {
- console.log(`🚀🚀🚀🚀🚀-> in firebase-messaging-sw.js on 41`, e);
- if (e.custom) return;
- let oldData = e.data;
- let newEvent = new CustomPushEvent({
- data: {
- json() {
- let newData = oldData.json();
- newData.data = {
- ...newData.data,
- ...newData.notification,
- };
- delete newData.notification;
- return newData;
- },
- },
- waitUntil: e.waitUntil.bind(e),
- });
- e.stopImmediatePropagation();
- dispatchEvent(newEvent);
- });
- const messaging = firebase.messaging();
- messaging.onBackgroundMessage((payload) => {
- console.log(`🚀🚀🚀🚀🚀-> in firebase-messaging-sw.js on 24`, payload);
- // Customize notification here
- const notificationTitle = payload.data.title;
- const notificationOptions = {
- body: payload.data.body,
- icon: payload.data.image,
- data: payload.data,
- ...payload.data,
- };
- return self.registration.showNotification(notificationTitle, notificationOptions);
- });
- self.onnotificationclick = function (event) {
- //example
- self.clients.openWindow("/");
- event.notification.close();
- };
- console.log("reg: ", reg.scope);
- })
- .catch(function (err) {
- console.log("err: ", err);
- });
- }
|