简体   繁体   English

服务消息不可用

[英]Service messaging is not available

I want to integrate FCM with nextjs project.我想将 FCM 与 nextjs 项目集成。 This error is occurring whenever I save the firebase.js config file.每当我保存 firebase.js 配置文件时,就会发生此错误。 I'm not being able to use Firebase Cloud Messaging in Firebase V9.我无法在 Firebase V9 中使用 Firebase Cloud Messaging。

I use firebase 9.10.0

在此处输入图像描述

my firebase.js config我的 firebase.js 配置

import { initializeApp } from 'firebase/app';
import { getToken, getMessaging, onMessage } from 'firebase/messaging';

const firebaseConfig = {
  apiKey: "*************",
  authDomain: "********************",
  projectId: "******************",
  storageBucket: "*****************",
  messagingSenderId: "*************",
  appId: "**********************"
};

console.log('*** Environment ***', process.env.REACT_APP_ENV)
console.log('*** Firebase Config ***', firebaseConfig)

const firebaseApp = initializeApp(firebaseConfig);
const messaging = getMessaging(firebaseApp);

export const getOrRegisterServiceWorker = () => {
  if ('serviceWorker' in navigator) {
    return window.navigator.serviceWorker
      .getRegistration('/firebase-push-notification-scope')
      .then((serviceWorker) => {
        if (serviceWorker) return serviceWorker;
        return window.navigator.serviceWorker.register('/firebase-messaging-sw.js', {
          scope: '/firebase-push-notification-scope',
        });
      });
  }
  throw new Error('The browser doesn`t support service worker.');
};

export const getFirebaseToken = () =>
  getOrRegisterServiceWorker()
    .then((serviceWorkerRegistration) =>
      getToken(messaging, { vapidKey: "***********", serviceWorkerRegistration }));

export const onForegroundMessage = () =>
  new Promise((resolve) => onMessage(messaging, (payload) => resolve(payload)));

Ather searing a lot I found solutions: Ather 灼热很多,我找到了解决方案:

I change the code of my firebase.js file to the below code我将firebase.js文件的代码更改为以下代码

// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getMessaging, getToken } from "firebase/messaging";
import localforage from "localforage";

const firebaseConfig = {
  apiKey: "**********************",
  authDomain: "*******************",
  projectId: "******************",
  storageBucket: "****************",
  messagingSenderId: "****************",
  appId: "**************"
};
// Initialize Firebase

const firebaseCloudMessaging = {
  init: async () => {
     initializeApp(firebaseConfig);

    try {
      const messaging = getMessaging();
      const tokenInLocalForage = await localStorage.getItem("fcm_token");

      // Return the token if it is alredy in our local storage
      if (tokenInLocalForage !== null) {
        return tokenInLocalForage;
      }

      // Request the push notification permission from browser
      const status = await Notification.requestPermission();
      if (status && status === "granted") {
        // Get new token from Firebase
        const fcm_token = await getToken(messaging, {
          vapidKey:
            "********************",
        });
        console.log("token in fcm_token", fcm_token);
        // Set token in our local storage
        if (fcm_token) {
          localforage.setItem("fcm_token", fcm_token);
          return fcm_token;
        }
      }
    } catch (error) {
      console.error(error);
      return null;
    }
  },
};
export { firebaseCloudMessaging };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM