为什么在调用 firebase 函数时会出现 FCM 错误

Why does an FCM error appear when calling a firebase function

所以我像这样通过 AngularFire 调用 firebase 函数:

const response = await this.aFunctions.httpsCallable<void, ResponseType>('funcName')().toPromise();

这在部署到 firebase(托管)时有效,但在本地环境中(使用 ionic serve),它会抛出此错误:

ERROR Error: Uncaught (in promise): FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker for scope ('http://localhost:8100/firebase-cloud-messaging-push-scope') with script ('http://localhost:8100/firebase-messaging-sw.js'): A bad HTTP response code (404) was received when fetching the script. (messaging/failed-service-worker-registration).
FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker for scope ('http://localhost:8100/firebase-cloud-messaging-push-scope') with script ('http://localhost:8100/firebase-messaging-sw.js'): A bad HTTP response code (404) was received when fetching the script. (messaging/failed-service-worker-registration).

我在这个项目中没有以任何方式使用 FCM。你知道为什么会这样吗?

好的,我通过解决在我的应用程序中导入 Firebase 的问题来修复它。

// 此导入加载 firebase 命名空间及其所有类型信息。 import firebase from 'firebase/app';

// 这些导入将单个服务加载到 firebase 命名空间中。 import 'firebase/auth'; import 'firebase/database';

我能够删除 firebase-messaging-sw.js 文件并且所有 onCall 功能都正常工作。

我以前混合过:

import firebase from 'firebase;

import * as firebase from 'firebase'

标准化所有内容以从 'firebase/app' 导入 firebase + 您需要的后续包 import 'firebase/functions'

现在一切正常。

就我而言,我通过注释掉 messagingSenderId 来解决它。

firebase: {
  apiKey: 'XXXXX',
  authDomain: 'XXXXX',
  databaseURL: 'XXXXX',
  projectId: 'XXXXX',
  storageBucket: 'XXXXX',
  // messagingSenderId: 'XXXXX',   **<== comment out this**
  appId: 'XXXXX'
}

我的情况有点不同和奇怪。 我需要将一个 Angular 项目部署到 firebase 托管。

代码:

const ob = this.functions.httpsCallable('api-name')({});
ob.subscribe((data)=>{
  //do something here
});

错误:

FirebaseError: Messaging: We are unable to register the default service worker.......

环境:

  • 本地:好的
  • firebase 托管默认域(projectName.web.app):错误
  • firebase 托管默认域(projectName.firebaseapp.com):好的

不知道是什么原因,希望对你和其他有类似问题的人有所帮助。

我希望我有一个更好的答案,但就我而言,我支持 FCM,所以我需要提供 messagingSenderId(尽管注释掉确实解决了调用我的云函数的问题)。

看来如果你支持 FCM 并且想使用 htttpsCallable 你需要先获得你的 FCM 令牌 ‍♂️

就我而言,在我的云函数调用解决我的问题之前移动它:

    const vapidKey = 'yourkey'
    const token = await firebase.messaging().getToken({ vapidKey })
    const result = await firebase.functions().httpsCallable('updateEventReaction');
    ...