首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >角+火基:无法注册默认的服务工作人员。未能为作用域注册ServiceWorker

角+火基:无法注册默认的服务工作人员。未能为作用域注册ServiceWorker
EN

Stack Overflow用户
提问于 2022-05-26 11:26:36
回答 2查看 486关注 0票数 0

我使用的是有角度和火力的推送通知

  1. I安装了firebase软件包版本9.8.1 (最新)

安装了@

  1. /fire软件包7.3.0版

  1. 在src目录中添加了manifest.json文件。

manifest.json

{ "gcm_sender_id":“My发件人-id”}

firebase-messaging-sw.js

  1. 也在src目录中添加了我的firebase消息传递-sw.js。

importScripts(https://www.gstatic.com/firebasejs/9.8.1/firebase-app.js');importScripts(https://www.gstatic.com/firebasejs/9.8.1/firebase-messaging.js');firebase.initializeApp({ apiKey:"xxxxxxxxxx",authDomain:"xxxxxxxx",projectId:“xxxxxxxx”,storageBucket:"xxxxxxxxx",messagingSenderId:“xxxxxxxxx”,appId:"xxxxxx",measurementId:“xxxxxxxxxx”});const消息传递=xxxxxxxxx}

  1. 在angular.json文件中添加了这两个文件:

angular.json

“资产”:“src/src.src”,"src/assets",“src/firebase Messaging-sw.js”,“src/limic.json”,

  1. 更新了我的app.model.ts文件:

app.model.ts

从“@ NgModule /core”导入{ BrowserModule };从“@角/平台-浏览器”导入{BrowserModule};.从‘@ AngularFireMessagingModule /fire/compat/messaging’导入{ AngularFireDatabaseModule };从“@角钢/火/compat/数据库”导入{ AngularFireAuthModule };从“@ AngularFireModule /AngularFireModule”导入{AngularFireModule};从‘src/环境/环境’导入{ MessagingService };从‘./services/Messaging.service’导入{MessagingService};从@NgModule({声明:.,进口: BrowserModule,HttpClientModule,AppRoutingModule,AngularFireDatabaseModule,AngularFireAuthModule,AngularFireMessagingModule,AngularFireMessagingModule.,提供程序: xxx,引导程序: AppComponent })导出类AppModule { }

  1. 在我的环境文件中添加了防火墙凭据.

  1. 制作了messagingService:

messaging.service.ts

从‘@角/核心’导入{ AngularFireMessaging};从‘@角/火/compat/消息传递’导入{ AngularFireMessaging };从'rxjs‘@ Injectable ({ providedIn:'root’})导出类MessagingService { x: any = null currentMessage =新BehaviorSubject (this.x.x);构造函数(私有angularFireMessaging: AngularFireMessaging){}requestPermission(令牌){console.log(令牌){console.log(令牌);},(err) => {console.error(‘无法获得通知的权限’,err;} );} receiveMessage() { this.angularFireMessaging.messages.subscribe( (有效载荷) => {console.log(“新收到的消息”)。",有效载荷);this.currentMessage.next(有效载荷);}

  1. 将消息传递功能添加到app.component中:

app.component.ts

导入{ Component,OnInit }从‘@角/核心’;从‘./services/messaging.service’导入{ MessagingService };@Component({ selector:'app-root',templateUrl:'./app.component.html',styleUrls:'./app.component.css‘})导出类AppComponent实现OnInit{ message: any;构造函数(私有messagingService: MessagingService){ } ngOnInit():void { this.messagingService.requestPermission() this.messagingService.receiveMessage() this.message = this.messagingService.currentMessage } title = 'SFAeCommerce';}

当我运行我的项目并允许Chrome中的通知时,它给了我这个错误:

代码语言:javascript
运行
复制
firebase-messaging-sw.js:1 Uncaught DOMException: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at 'https://www.gstatic.com/firebasejs/9.8.1/firebase-app.js' failed to load.
    at http://localhost:4200/firebase-messaging-sw.js:1:1
(anonymous) @ firebase-messaging-sw.js:1
messaging.service.ts:21 Unable to get permission to notify. FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker for scope ('http://localhost:4200/firebase-cloud-messaging-push-scope') with script ('http://localhost:4200/firebase-messaging-sw.js'): ServiceWorker script evaluation failed (messaging/failed-service-worker-registration).

当我转到https://localhost:4200/firebase-messaging-sw.js时,它将返回firebase消息传递-sw.js文件。

EN

回答 2

Stack Overflow用户

发布于 2022-07-14 16:48:30

我也遇到了同样的问题,我在这里找到了解决方案:https://github.com/firebase/firebase-js-sdk/issues/5732#issuecomment-969453902

从版本9中,如果您想继续使用importScripts,请导入在文件名末尾添加'compat‘的文件,您应该可以继续使用。比如:

代码语言:javascript
运行
复制
importScripts('https://www.gstatic.com/firebasejs/9.8.1/firebase-app-compat.js');
importScripts('https://www.gstatic.com/firebasejs/9.8.1/firebase-messaging-compat.js');

例如,要使用"9.8.1/firebase-app.js“(没有”compat“),它将无法使用importScripts。原因是因为

importScripts是仅在浏览器上下文中使用<script type="module">支持的ESMs。

更多的解释和导入文件的第二种方法,只需查看上面的参考链接。

票数 0
EN

Stack Overflow用户

发布于 2022-11-28 12:55:00

对于那些犯了我所犯的错误的人,再检查一遍你的文件名!它应该是firebase-messaging-sw.js,而不是firebase消息-sw.js。

多亏了@Gi Tavares,web 8(命名空间)导入为我工作,而不是web 9。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72391058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档