我已经使用webspack在react网站上实现了fcm Push通知。
我已经在公共场所创建了firebase-messaging-sw.js
文件
// [START initialize_firebase_in_sw]
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here,other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/5.8.3/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/5.8.3/firebase-messaging.js');
// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
firebase.initializeApp({ 'messagingSenderId': '332364892095' });
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
// If you would like to customize notifications that are received in the
// background (Web app is closed or not in browser focus) then you should
// implement this optional method.
// [START background_handler]
messaging.setBackgroundMessageHandler((payload) => {
// Customize notification here
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.'
// icon: '/firebase-logo.png'
};
alert("called")
return self.registration.showNotification(notificationTitle,notificationOptions);
});
// [END background_handler]
我在"gcm_sender_id": "103953800507"
文件中添加了manifest.json
。
我已经在init-fcm.js
文件夹中创建了src
文件。
import * as firebase from "firebase";
var config = {
apiKey: "AIzaSyDuL6CgCdHYfsaAgw9Iom7CwLcCg29BS4E",authDomain: "filmthere-fa6e0.firebaseapp.com",databaseURL: "https://filmthere-fa6e0.firebaseio.com",projectId: "filmthere-fa6e0",storageBucket: "filmthere-fa6e0.appspot.com",messagingSenderId: "332364892095",};
export default firebase.initializeApp(config);
这是我的App.js
文件
import React,{ Component } from 'react';
import '../App.css';
import Footer from './Footer'
import Main from './Main';
import firbaseApp from "./init-fcm";
import * as firebase from "firebase";
class App extends Component {
constructor(props) {
super(props);
this.state = {};
}
componentDidmount() {
if(firebase.messaging.isSupported()){
// messaging.onmessage(this.handleBgCb);
firbaseApp.messaging().requestPermission()
.then(() => {
firbaseApp.messaging().getToken()
.then(currentToken => {
if (currentToken) {
console.log('token',currentToken)
// sendTokenToServer(currentToken);
// updateUIForPushEnabled(currentToken);
} else {
// Show permission request.
console.log(
"No Instance ID token available. Request permission to generate one."
);
// Show permission UI.
// updateUIForPushPermissionRequired();
// setTokenSentToServer(false);
}
})
.catch(err => {
console.log("An error occurred while retrieving token. ",err);
// showToken('Error retrieving Instance ID token. ',err);
// setTokenSentToServer(false);
});
})
.catch(err => {
console.log("Unable to get permission to notify.",err);
});
}
}
render() {
return (
<div >
<Main />
<Footer />
}
</div>
);
}
}
export default App;
但是在页面加载时会出现此错误。
FirebaseError:“消息:我们无法注册默认值 服务人员。的ServiceWorker脚本位于 http://localhost:8090/firebase-messaging-sw.js用于范围 遇到http://localhost:8090/firebase-cloud-messaging-push-scope 安装期间发生错误。 (邮件/服务工作人员注册失败
请帮助我解决此问题。