我正在尝试为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();
1条答案
按热度按时间mpbci0fu1#