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

时间:2020-05-06 17:20:27

标签: angular firebase ionic-framework google-cloud-functions angularfire

所以我正在像这样通过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。您有任何线索为什么会这样吗?

2 个答案:

答案 0 :(得分:5)

就我而言,我通过注释掉 messagesSenderId 解决了这个问题。
火力:{
apiKey: 'XXXXX',
authDomain: 'XXXXX',
databaseURL: 'XXXXX',
projectId: 'XXXXX',
storageBucket: 'XXXXX',
// messagesSenderId: 'XXXXX', <== 注释掉这个
appId:'XXXXX'
}

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

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

错误:
FirebaseError:消息传递:我们无法注册默认服务工作线程......

环境:

  • 本地:好的

  • firebase 托管默认域(projectName.web.app):错误

  • firebase 托管默认域(projectName.firebaseapp.com):好的

我不知道原因,但希望它可以帮助您和其他遇到类似问题的人。

答案 1 :(得分:1)

好,我解决了整个应用程序中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'

现在一切正常。