Explorar o código

fix: 修改serviceWorker判断

Before hai 8 meses
pai
achega
543786d86f

+ 54 - 65
public/firebase-messaging-sw.js

@@ -1,13 +1,6 @@
 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')
-if ('serviceWorker' in navigator) {
-  navigator.serviceWorker.register('./firebase-messaging-sw.js').then(function (reg) {
-    console.log('reg: ', reg.scope);
-  }).catch(function (err) {
-    console.log('err: ', err);
-  })
-}
 class CustomPushEvent extends Event {
   constructor(data) {
     super('push')
@@ -16,70 +9,66 @@ class CustomPushEvent extends Event {
     this.custom = true
   }
 }
+if ('serviceWorker' in navigator) {
+  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,
+  });
 
-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,
-});
-
-// Retrieve an instance of Firebase Messaging so that it can handle background
-// messages.
-self.addEventListener('push', (e) => {
-  // Skip if event is our own custom event
-  console.log(`🚀🚀🚀🚀🚀-> in firebase-messaging-sw.js on 41`, e);
-  if (e.custom) return;
-
-  // Kep old event data to override
-  let oldData = e.data
+  self.addEventListener('push', (e) => {
+    console.log(`🚀🚀🚀🚀🚀-> in firebase-messaging-sw.js on 41`, e);
+    if (e.custom) return;
 
-  // Create a new event to dispatch, pull values from notification key and put it in data key,
-  // and then remove notification key
-  let newEvent = new CustomPushEvent({
-    data: {
-      json() {
-        let newData = oldData.json()
-        newData.data = {
-          ...newData.data,
-          ...newData.notification
-        }
-        delete newData.notification
-        return newData
+    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),
-  })
+      waitUntil: e.waitUntil.bind(e),
+    })
 
-  // Stop event propagation
-  e.stopImmediatePropagation()
+    e.stopImmediatePropagation()
 
-  // Dispatch the new wrapped event
-  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) {
-  console.log(`🚀🚀🚀🚀🚀-> in firebase-messaging-sw.js on 91`,event);
-  //example
-  self.clients.openWindow('/')
-  event.notification.close()
+  self.onnotificationclick = function (event) {
+    //example
+    self.clients.openWindow('/')
+    event.notification.close()
+  }
+    console.log('reg: ', reg.scope);
+  }).catch(function (err) {
+    console.log('err: ', err);
+  })
 }

+ 4 - 8
src/app/[locale]/(TabBar)/[[...share]]/@actionWidget/Service.tsx

@@ -69,14 +69,10 @@ const ServiceWidget: FC<Props> = (props) => {
 
     // 首充活动
     const getPayInfo = async (): Promise<PayDataType> => {
-        try {
-            if (token) {
-                const result = await getPaysApi();
-                return result.data;
-            } else {
-                return Promise.reject();
-            }
-        } catch (e) {
+        if (token) {
+            const result = await getPaysApi();
+            return result.data;
+        } else {
             return Promise.resolve({
                 first_pay: [],
                 pay: [],