无法注册默认服务工作进程,无法为(“http://localhost:8100/firebase-cloud-messaging-push-scope”)注册ServiceWorker

lb3vh1jj  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(93)

我正在尝试为Ionic Angular应用程序设置Firebase Cloud Messaging。我有我的服务工人文件的问题,我认为这是因为它不是一个http网址,根据文档,但我不知道如何改变,如果这是问题。
我已经阅读了Firebase文档,但他们没有告诉如何解决这个问题。

importScripts('https://www.gstatic.com/firebasejs/8.2.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/8.2.0/firebase-messaging.js');
import { environment } from './environments/environment';
import {initializeApp} from "firebase/app";
import {getMessaging, onBackgroundMessage} from "firebase/messaging/sw";
firebase.initializeApp(environment.firebase);

const messaging = firebase.messaging();

这个错误实际上出现在我设置的获取令牌的消息服务中:

import { Injectable } from '@angular/core';
import { AngularFireMessaging } from '@angular/fire/compat/messaging';
import { getMessaging, getToken } from '@angular/fire/messaging';

@Injectable({
  providedIn: 'root'
})
export class MessagingService {

  constructor(private angularFireMessaging: AngularFireMessaging) { }

  async saveDeviceToken () {
    console.log('test')
    const VAPID_KEY = 'key'
    const messaging = getMessaging();
    console.log('messag', messaging);
    await getToken(messaging, {vapidKey: VAPID_KEY})
    .then((currentToken) => {
      if (currentToken) {
        console.log('token', currentToken)
      } else {
        // Show permission request UI
        console.log('No registration token available. Request permission to generate one.');
        // ...
      }
    }).catch((error) =>{
      console.log('An error occurred while retrieving token. ', error);
    })
  }
}

即使它出现在消息服务中,这个错误也让我相信这是我的service worker文件的问题。它在src/文件夹中。

"firebase": "^9.18.0",
"firebase-tools": "^8.0.0",
"@angular/common": "^15.2.2",
"@angular/core": "^15.2.2",
"@angular/fire": "^7.5.0",

服务人员:

importScripts('https://www.gstatic.com/firebasejs/8.0.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/8.0.0/firebase-messaging.js');

firebase.initializeApp({

  });

const messaging = firebase.messaging();
mpbci0fu

mpbci0fu1#

//Create Firebase-messaging-sw.js where you want
firebase.js
 
import { initializeApp } from "firebase/app";
import { getMessaging, getToken } from "firebase/messaging";

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

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

export const getToken1 = () => {

    console.log('Requesting permission...');
    if (navigator.serviceWorker) {
        navigator.serviceWorker
            .register(`fullpath-to-/firebase-messaging-sw.js`)
            .then((registration) => {
                Notification.requestPermission().then((permission) => {
                    if (permission === 'granted') {
                        return getToken(messaging, { vapidKey: "your vapid", serviceWorkerRegistration: registration }).then((currentToken) => {
                            if (currentToken) {
                                console.log('current token for client: ', currentToken);

                                // Track the token -> client mapping, by sending to backend server
                                // show on the UI that permission is secured
                            } else {
                                console.log('No registration token available. Request permission to generate one.');

                                // shows on the UI that permission is required 
                            }
                        }).catch((err) => {
                            console.log('An error occurred while retrieving token. ', err);
                            // catch error while creating client token
                        })
                    }
                })
            })
            .catch((error) => {
                console.error('Service Worker registration failed:', error);
            });

          }
       

    }

相关问题