【发布时间】:2021-06-20 03:51:18
【问题描述】:
有没有办法在Angular中为firebase添加基于环境的配置?
我有不同的 firebase projectIds 和 messingsSenderIds 用于 Dev 环境和 Prod 环境。 我想在我的 firebase_messaging_sw.js 中使用这种不同的配置 我正在为我的应用程序中的后台通知做准备。 下面是代码。
importScripts("https://www.gstatic.com/firebasejs/8.2.7/firebase-app.js");
importScripts("https://www.gstatic.com/firebasejs/8.2.7/firebase-messaging.js");
// For an optimal experience using Cloud Messaging, also add the Firebase SDK for Analytics.
importScripts("https://www.gstatic.com/firebasejs/8.2.7/firebase-analytics.js");
// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
firebase.initializeApp({
messagingSenderId: 'xxxxxxxx',
apiKey: 'xxxxxxxxxxxx',
projectId: 'xxxxxxxx',
appId: 'xxxxxxxxxxx',
});
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function (payload) {
console.log(
"[firebase-messaging-sw.js] Received background message ",
payload,
);
let notificationBody;
if (payload && payload.data && payload.data.notification) {
notificationBody = JSON.parse(payload.data.notification);
}
if (notificationBody) {
return self.registration.showNotification(
notificationBody.title,
{body: notificationBody.body, icon: notificationBody.icon, image: notificationBody.image},
);
}
}
);
在这里,我希望根据环境使用不同的 firebaseConfig。 我已尝试导入出现以下错误的环境。
不能在模块外使用 import 语句
我也尝试在 angular.json 中使用类似于环境文件的文件替换,但它也不起作用。
请帮我解决这些问题。
【问题讨论】:
标签: javascript angular firebase-cloud-messaging service-worker angular-service-worker