我正在尝试将我的反应应用程序与 Firebase 集成以实现推送通知。我的 src 文件夹中有这个包含所有配置的文件:
import firebase from 'firebase/app';
import 'firebase/messaging';
const config = {
apiKey: "API_KEY",
authDomain: "MY_DOMAIN",
databaseURL: "DATABASE_URL",
projectId: "PROJECT_ID",
storageBucket: "STORAGE_BUCKET",
messagingSenderId: "SENDER_ID",
appId: "APP_ID",
measurementId: "MEASURE_ID"
};
firebase.initializeApp(config);
const messaging = firebase.messaging();
export const requestFirebaseNotificationPermission = () =>
new Promise((resolve, reject) => {
messaging
.requestPermission()
.then(() => messaging.getToken())
.then((firebaseToken) => {
resolve(firebaseToken);
})
.catch((err) => {
reject(err);
});
});
export const onMessageListener = () =>
new Promise((resolve) => {
messaging.onMessage((payload) => {
resolve(payload);
});
});
而且,这是我如何实现使推送通知在我的 App.js 中工作的功能:
// Load the FCM configuration parameters and activate the push notifications through the app
requestFirebaseNotificationPermission()
.then((firebaseToken) => {
// eslint-disable-next-line no-console
console.log("CHECK_FCM_TOKEN", firebaseToken);
this.setState({ fcmToken: firebaseToken })
})
.catch((err) => {
return err;
});
如果有任何帮助来解决这个问题,我将不胜感激。