CARP Firebase Data Backend

A Flutter plugin for uploading data from the CARP Mobile Sensing Framework to a Google Firebase data backend.

Supports uploading json to Firebase as either (zipped) files to Firebase Storage or as plain json to a Firebase Database.

pub package

For Flutter plugins for other CARP products, see CARP Mobile Sensing in Flutter.

This package can upload sending data to Goggle Firebase in two ways:

  • Google Firebase Storage – By using the Google Firebase Storage endpoint, CARP sensing data are uploaded as raw or zipped JSON file, generated using the FileDataManager in carp_mobile_sensing. In Firebase, files with sensed data is stored in the path specified in the FirebaseStorageDataEndPoint plus subfolders for each study and device. The path on Firebase hence follow this pattern: /<path>/<study_id>/<device_id>/

  • Google Firebase Database – By using the Google Firebase Database endpoint, CARP sensing data are uploaded as raw JSON data points, using Firebase as a DataManager in carp_mobile_sensing. In Firebase, data json objects are stores in the collection specified in the FirebaseDatabaseDataManager. JSON objects will be stored in collections named /<collection>/<study_id>/<device_id>/upload/<data_type> relative to this path. For example, if collection is carp_data, study_id is 1234 and device_id is R16NW, location data will be stored as documents in this collection: carp_data/1234/R16NW/upload/location.

Setting up support for Google Firebase

For Firebase to work with your Flutter app, configuration of both Firebase and your Flutter app has to be done. Please follow the step below in details, since the level of debugging / error messages are quite limited when setting this up. If you are new to Firebase, then please start by reading the extensive Firebase documentation first. Then read the Add Firebase to your Flutter app tutorial.

Step 1: Configure Google Firebase

  1. Create a Firebase project with a cloud storage
  2. Add Firebase to your Flutter project by following the description. Note that you need to add support for both the Android and iOS version of the Flutter app.
  3. Configure authentication
  4. Add users that can upload data e.g. via the console
  5. Set up your storage security rules as shown below.
# Default access rule
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}

For testing/debugging purposes, you may remove authentication by the authentication rule below (but should be removed in production)

# No authentication rule
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}

Step 2: Configure your app to use Firebase

Follow the direction on how to configure a Flutter app to use Firebase.

On iOS

  1. Add your app to your Firebase project. Enter your app’s bundle ID in the iOS bundle ID field.
  2. Download the GoogleService-Info.plist file to obtain your Firebase iOS config file.
  3. Using XCode, move the file into the Runner/Runner directory of your Flutter app.
  4. Google services use CocoaPods to install and manage dependencies. Open a terminal window and navigate to the location of the Xcode project for your app. The type
$ pod init
§ pod install

This should install all the necessary pods for Firebase.

Note: It seems like Firebase is putting constraints on the naming of the iOS Bundle Name. Even though a valid iOS bundle name can contain . (dots) and spaces, Firebase throws the following exception:

Terminating app due to uncaught exception 'com.firebase.core', reason: 'App name can only contain alphanumeric, hyphen (-), and underscore (_) characters'

if dots are used. Hence, avoid using dots, but use - (hypen) instead. This is specified in the `ios/Runner/info.plist' file:

<key>CFBundleName</key>
<string>your-app-name</string>

Note – it seems like adding Firebase support to your iOS app dramatically increases the Xcode build process for the Flutter app. So – have patience...

On Android

  1. Add your app to your Firebase project. Enter your app’s application ID in the Android package name field.

  2. Download the configuration file named google-services.json and make sure to put in in your Android app module root directory, i.e a folder like <appname>/android/app/

  3. Add support for the Google Services Gradle plugin to read the google-services.json file that was generated by Firebase.

    • in your IDE, open android/app/build.gradle, and add the following line as the last line in the file:

    apply plugin: 'com.google.gms.google-services'

    • In android/build.gradle, inside the buildscript tag, add a new dependency:
    dependencies {
            // ...
            classpath 'com.google.gms:google-services:4.3.2'   // new
        }
    

Step 3: Use the CARP Firebase Backend in your Flutter App

In Flutter, the carp_firebase_backend plugin is used to upload data from carp_mobile_sensing to a Firebase endpoint.

Add the carp_firebase_backend plugin to the pubspec.yaml file.

dependencies:
  flutter:
    sdk: flutter
  carp_firebase_backend: ^0.5.0     # support for uploading CARP data to Firebase

Run flutter packages get. For more information on managing packages and plugins, see Using Packages.

Using the Plugin

Upload of files to Firebase Storage uses the FirebaseStorageDataManager and upload of json objects to Firebase Database uses FirebaseDatabaseDataManager. Using the library takes three steps.

1. Register the Data Managers

First you should register the data manager you want to use (or both) in the DataManagerRegistry.

DataManagerRegistry().register(DataEndPointType.FIREBASE_STORAGE, new FirebaseStorageDataManager());
DataManagerRegistry().register(DataEndPointType.FIREBASE_DATABASE, new FirebaseDatabaseDataManager());

2. Specify Access Details to the Firebase App

Both data managers uses a FirebaseEndPoint object to handle access to Firebase. In this object, Firebase endpoint configuration keys are stored as well as authentication details. All of the Firebase configuration keys can be found in the Projects Settings in the Firebase Console. Remember to register your app in Firebase, as described above. The firebaseAuthenticationMethod key specify the authentication method. Currently, only email/password and Google Sign-In is implemented (even though FireBaseAuthenticationMethods lists them all (for future use)).

Using email/password as authentication

final FirebaseEndPoint firebaseEndPoint = new FirebaseEndPoint(
    name: "Flutter Sensing Sandbox",
    uri: 'gs://flutter-sensing-sandbox.appspot.com',
    projectID: 'flutter-sensing-sandbox',
    webAPIKey: 'AIzaSyCGy6MeHkiv5XkBtMcMbtgGYOpf6ntNVE4',
    gcmSenderID: '201621881872',
    androidGoogleAppID: '1:201621881872:android:8e84e7ccfc85e121',
    iOSGoogleAppID: '1:159623150305:ios:4a213ef3dbd8997b',
    firebaseAuthenticationMethod: FireBaseAuthenticationMethods.PASSWORD,
    email: "some_email@dtu.dk",
    password: "some_password");

Using Google Sign-In as authentication

final FirebaseEndPoint firebaseEndPoint = new FirebaseEndPoint(
    name: "Flutter Sensing Sandbox",
    uri: 'gs://flutter-sensing-sandbox.appspot.com',
    projectID: 'flutter-sensing-sandbox',
    webAPIKey: 'AIzaSyCGy6MeHkiv5XkBtMcMbtgGYOpf6ntNVE4',
    gcmSenderID: '201621881872',
    androidGoogleAppID: '1:201621881872:android:8e84e7ccfc85e121',
    iOSGoogleAppID: '1:159623150305:ios:4a213ef3dbd8997b',
    firebaseAuthenticationMethod: FireBaseAuthenticationMethods.GOOGLE);

3. Create the Data Endpoint

Finally, you create the data endpoint (FirebaseStorageDataEndPoint or FirebaseDatabaseDataEndPoint) providing it with a FirebaseEndPoint and add it as your Study data endpoint.

Firebase Storage Endpoint

final FirebaseStorageDataEndPoint storageEndPoint = new FirebaseStorageDataEndPoint(
   firebaseEndPoint,
   path: 'sensing/data', 
   bufferSize: 500 * 1000, 
   zip: true, 
   encrypt: false);

CAMSStudyProtocol study = CAMSStudyProtocol()..dataEndPoint = storageEndPoint;

Note that a FirebaseStorageDataEndPoint extends the FileDataEndPoint class and parameters related to how to create the files can be specified, including bufferSize, zip, and encrypt. In the example above, the file buffer size is set to 1 MB, which is zipped before upload.

Firebase Database Endpoint

final FirebaseDatabaseDataEndPoint databaseEndPoint =
    new FirebaseDatabaseDataEndPoint(firebaseEndPoint, collection: 'carp_data');


CAMSStudyProtocol study = CAMSStudyProtocol()..dataEndPoint = databaseEndPoint;

Features and bugs

Please file feature requests and bug reports at the issue tracker.

License

This software is copyright (c) Copenhagen Center for Health Technology (CACHET) at the Technical University of Denmark (DTU). This software is available 'as-is' under a MIT license.