我使用的是有角度和火力的推送通知
安装了@
manifest.json:
{ "gcm_sender_id":“My发件人-id”}
firebase-messaging-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}
angular.json:
“资产”:“src/src.src”,"src/assets",“src/firebase Messaging-sw.js”,“src/limic.json”,
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 { }
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(有效载荷);}
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中的通知时,它给了我这个错误:
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文件。
发布于 2022-07-14 16:48:30
我也遇到了同样的问题,我在这里找到了解决方案:https://github.com/firebase/firebase-js-sdk/issues/5732#issuecomment-969453902
从版本9中,如果您想继续使用importScripts
,请导入在文件名末尾添加'compat‘的文件,您应该可以继续使用。比如:
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。
更多的解释和导入文件的第二种方法,只需查看上面的参考链接。
发布于 2022-11-28 12:55:00
对于那些犯了我所犯的错误的人,再检查一遍你的文件名!它应该是firebase-messaging-sw.js,而不是firebase消息-sw.js。
多亏了@Gi Tavares,web 8(命名空间)导入为我工作,而不是web 9。
https://stackoverflow.com/questions/72391058
复制相似问题