简体   繁体   English

有没有办法在 Angular 中为 firebase 添加基于环境的配置

[英]Is there a way to add configuration based on environment for firebase in Angular

Is there a way to add configuration based on the environment for firebase in Angular?有没有办法在Angular中为firebase添加基于环境的配置?

I have different firebase projectIds and messagingSenderIds for Dev environment and Prod Environment.我有不同的 firebase projectIds 和 messengingSenderIds 用于开发环境和生产环境。 I want to use this different configuration in my firebase_messaging_sw.js I am doing for background Notifications in my app.我想在我正在为我的应用程序中的后台通知做的 firebase_messaging_sw.js 中使用这种不同的配置。 Below is the code.下面是代码。

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},
      );
    }
  }
);

Here, I wish to use different firebaseConfig based on the environment.在这里,我希望根据环境使用不同的 firebaseConfig。 I have tried to import the environment which gave the below error.我试图导入给出以下错误的环境。

Cannot use import statement outside a module不能在模块外使用 import 语句

I also tried using file replacement in angular.json similar to the environment file but it also didn't work.我还尝试在 angular.json 中使用类似于环境文件的文件替换,但它也不起作用。

Please help me with these.请帮我解决这些问题。

For some reason, Firebase always looks for the firebase-messaging-sw.js file in the root of the directory.出于某种原因,Firebase 总是在目录的根目录中查找 firebase-messaging-sw.js 文件。 Hence we could not change the name or location of this file.因此,我们无法更改此文件的名称或位置。

So I found a way around this.所以我找到了解决这个问题的方法。 Create 2 copies of the same file and store it in a different directory.创建同一文件的 2 个副本并将其存储在不同的目录中。 In my case, I have create a directory named service-worker and inside it created 2 directories named dev and prod.就我而言,我创建了一个名为 service-worker 的目录,并在其中创建了 2 个名为 dev 和 prod 的目录。 Below is my directory structure.下面是我的目录结构。

src
|---> service-worker
      |----> dev
      |----> prod

then copied firebase-messaging-sw.js to the dev and prod folder.然后将 firebase-messaging-sw.js 复制到 dev 和 prod 文件夹。 Updated files on both folder with the environment specific configuration (hard-coded)使用特定于环境的配置(硬编码)更新了两个文件夹上的文件

firebase.initializeApp({
  messagingSenderId: 'xxxxxxxx',
  apiKey: 'xxxxxxxxxxxx',
  projectId: 'xxxxxxxx',
  appId: 'xxxxxxxxxxx',
});

In angular.json add below changes.在 angular.json 添加以下更改。

configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "assets": [
                "src/favicon.ico",
                "src/assets",
                "src/manifest.json",
                {
                  "glob": "firebase-messaging-sw.js",
                  "input": "src/service-worker/prod",
                  "output": "/"
                }
              ],
....

Do the same for dev configuration.对开发配置执行相同的操作。

What it does is pick up file or files mentioned under glob for input location and copy it to the output location.它的作用是选择 glob 下提到的文件作为输入位置,并将其复制到 output 位置。

Then run and test whether your service worker has proper configuration.然后运行并测试你的 service worker 是否有正确的配置。

Hopefully this helps some one.希望这对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM