【问题标题】:How to choose between development and production firebase project based on build flavours?如何根据构建风格在开发和生产firebase项目之间进行选择?
【发布时间】:2019-05-29 20:04:02
【问题描述】:

在本机开发中,我根据构建“风味”在 firebase 生产和开发项目之间切换我的应用程序。

在 Android 上,我将 google-services.json 放在文件夹中

<ProjectDir>/app/src/<BuildFlavour>

BuildFlavour 可以是例如debugrelease

因此对于 Flutter 项目也可以轻松完成。我确实看到了:

> Task :app:processDebugGoogleServices
Parsing json file: /Users/shadowsheep/AndroidStudioProjects/flutter_app_test_fcm_messaging/android/app/src/debug/google-services.json

在 iOS 上我会这样做:

        NSString *googleFirebaseJsonFileName = @"GoogleService-Info";
#ifdef DEBUG
        NSLog(@"[FIREBASE] Development mode.");
        googleFirebaseJsonFileName = @"GoogleService-Info-Debug";
#else
        NSLog(@"[FIREBASE] Production mode.");
#endif
        NSLog(@"%@", googleFirebaseJsonFileName);
        NSString *googleFirebaseJsonFilePath = [[NSBundle mainBundle]
                                                pathForResource:googleFirebaseJsonFileName
                                                ofType:@"plist"];
        NSLog(@"%@", googleFirebaseJsonFilePath);

        // https://firebase.google.com/docs/cloud-messaging/ios/client
        FIROptions *options = [[FIROptions alloc]
                               initWithContentsOfFile:googleFirebaseJsonFilePath];
        [FIRApp configureWithOptions:options];

如何在 Flutter 中为 iOS 项目以正确的方式实现这一目标?我必须把这个确切的代码放在AppDelegate 里面吗?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [GeneratedPluginRegistrant registerWithRegistry:self];
  // Override point for customization after application launch.
    // I've to init Firebase here the same way?
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

【问题讨论】:

    标签: firebase flutter


    【解决方案1】:

    最终我做到了:

    - (BOOL)application:(UIApplication *)application
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      [GeneratedPluginRegistrant registerWithRegistry:self];
      // Override point for customization after application launch.
        NSString *googleFirebaseJsonFileName = @"GoogleService-Info";
    #ifdef DEBUG
        NSLog(@"[FIREBASE] Development mode.");
        googleFirebaseJsonFileName = @"GoogleService-Info-Debug";
    
        NSLog(@"%@", googleFirebaseJsonFileName);
        NSString *googleFirebaseJsonFilePath = [[NSBundle mainBundle]
                                                pathForResource:googleFirebaseJsonFileName
                                                ofType:@"plist"];
        NSLog(@"%@", googleFirebaseJsonFilePath);
    
        // https://firebase.google.com/docs/cloud-messaging/ios/client
        FIROptions *options = [[FIROptions alloc]
                               initWithContentsOfFile:googleFirebaseJsonFilePath];
        if ([FIRApp defaultApp]) {
            NSLog(@"Firebase already configured!");
            [[FIRApp defaultApp]
             deleteApp:^(BOOL success) {
                 if (success) {
                     NSLog(@"Reconfigure Firebase");
                     [FIRApp configureWithOptions:options];
                 }
             }];
        } else {
            [FIRApp configureWithOptions:options];
        }
    #else
        NSLog(@"[FIREBASE] Production mode.");
    #endif
    
      return [super application:application didFinishLaunchingWithOptions:launchOptions];
    }
    

    它似乎有效。

    【讨论】:

      【解决方案2】:

      你可以通过不同的入口点在 Flutter 中拥有不同的风格。

      例如,您可以有这样的枚举:

      枚举构建配置 { 发展, 生产, }

      您的应用程序可以有两个入口文件。

      一个用于生产:

      main_production.dart

      在某些时候它会在某个地方初始化一个常量,例如:

      buildConfig = BuildConfig.production;

      还有一个开发文件:

      main_dev.dart

      这将像这样初始化常量:

      buildConfig = BuildConfig.development;

      您可以使用-t 标志(对于target)启动您的应用

      flutter run -t lib/main_production.dart

      在这种情况下,buildConfig var 的值将是 BuildConfig.production

      我强烈建议您查看this article 了解更多信息。

      在此基础上,您可以选择要初始化的 Firebase 项目。

      【讨论】:

      • 谢谢,也许标题有点误导(我希望帖子的正文不是),但我不是在寻找 Flutter 风味(即使它可能是一个热门话题),而是一种方法以简单的方式在 firebase 项目之间自动切换。我认为对于我想要实现我的解决方案来说它更容易。不知道有没有更简单的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 2013-01-24
      • 2016-02-16
      • 2012-07-01
      • 2012-06-23
      • 2019-12-07
      • 2012-06-01
      相关资源
      最近更新 更多