|
@@ -1,76 +1,77 @@
|
|
|
-
|
|
|
class CustomPushEvent extends Event {
|
|
|
- constructor(data) {
|
|
|
- super('push')
|
|
|
+ constructor(data) {
|
|
|
+ super("push");
|
|
|
|
|
|
- Object.assign(this, data)
|
|
|
- this.custom = true
|
|
|
- }
|
|
|
+ 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,
|
|
|
- });
|
|
|
+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;
|
|
|
+ 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),
|
|
|
- })
|
|
|
+ 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()
|
|
|
+ e.stopImmediatePropagation();
|
|
|
|
|
|
- dispatchEvent(newEvent)
|
|
|
- })
|
|
|
+ dispatchEvent(newEvent);
|
|
|
+ });
|
|
|
|
|
|
- const messaging = firebase.messaging();
|
|
|
+ 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);
|
|
|
- });
|
|
|
+ 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);
|
|
|
- })
|
|
|
+ self.onnotificationclick = function (event) {
|
|
|
+ //example
|
|
|
+ self.clients.openWindow("/");
|
|
|
+ event.notification.close();
|
|
|
+ };
|
|
|
+ console.log("reg: ", reg.scope);
|
|
|
+ })
|
|
|
+ .catch(function (err) {
|
|
|
+ console.log("err: ", err);
|
|
|
+ });
|
|
|
}
|