如何根据构建风格在开发和生产Firebase项目之间进行选择?

时间:2019-01-03 08:36:09

标签: firebase flutter

在本机开发中,我会根据构建“风味”在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];
}

2 个答案:

答案 0 :(得分:0)

通过具有不同的入口点,您可以在Flutter中获得风味。

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

  

枚举BuildConfig {     发展,     生产,   }

您可以为应用程序提供两个入口文件。

一个用于生产:

  

main_production.dart

在某个时候它会在以下某个地方创建一个常量:

  

buildConfig = BuildConfig.production;

另一个要开发的文件:

  

main_dev.dart

哪个常量将初始化为:

  

buildConfig = BuildConfig.development;

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

  

flutter运行-t lib / main_production.dart

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

我强烈建议您查看this article以获得更多信息。

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

答案 1 :(得分:0)

最终我做到了:

- (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];
}

似乎可行。