Build a mobile app using Firebase and App Engine flexible environment

Kokoro Build Status

This repository contains Android client sample code for the Build a Mobile App Using Firebase and App Engine Flexible Environment solution. You can find the sample code for the Android client code in the firebase-android-client repository.

Deployment requirements

Note: Firebase is a Google product, independent from Google Cloud Platform.

Google Cloud SDK setup

Configure the SDK to access the Google Cloud Platform by using the following command:

gcloud auth login

Get the project ID from the settings page of your Firebase project. Use the following command to set your Firebase project as the active project for the SDK:

gcloud config set project [project-id]

Configuration

Enable the Google sign-in provider by following these steps:

  1. Sign in to the Firebase console and select your project.
  2. In the Develop section, select Authentication.
  3. In the Authentication page, select Sign-in Method.
  4. Select and enable the Google sign-in provider.

Follow these steps to configure a service account for the backend application:

  1. Go to your project settings page on the Firebase console.

  2. Click the Settings gear next to 'Project Overview' and then Project settings.

  3. Select Service accounts and click the link Manage service account permissions.

  4. In the IAM & admin page click Create service account.

  5. In the dialog, create an account with the following parameters:

    • Enter playchat-servlet in the Service account name field.
    • Select Project > Owner in the Role menu.

      Caution: The owner role gives the service account full access to all resources in the project. In a production app, you should change the role to the minimum access that your service account requires.

  6. After the service account is created, click it and choose Create new key in the ADD KEY dropdown button.

    • Choose JSON as the key type.
    • Click CREATE to download the key.
  7. After you finish creating the account, your browser downloads the service account's private key to your computer as a JSON file. Move the file to the src/main/webapp/WEB-INF folder in the backend project.

  8. From the left menu of the Firebase console, select Database in the Develop group.

  9. In the Database page, click Create database in the Realtime Database section.

  10. In the Security rules for Realtime Database dialog, select Start in test mode and click Enable.

    Caution: Test mode allows anyone with your database reference to perform read and write operations to your database. If test mode isn't appropriate for your purposes, you can write security rules to manage access to your data. For more information, see Get Started with Database Rules in the Firebase documentation.

    This step displays the data you’ve stored in Firebase. In later steps of this tutorial, you can revisit this web page to see data added and updated by the client app and backend servlet.

  11. In the Rules tab of the database, make sure you have the security rules for read/write. For example:

    {
      "rules": {
        ".read": true,
        ".write": true
      }
    }
  12. Make a note of the Firebase URL for your project, which is in the form https://[project-id].firebaseio.com/ and appears next to a link icon.

  13. Open the src/main/webapp/WEB-INF/web.xml file and do the following:

    • Replace the JSON_FILE_NAME placeholder with the JSON file from that stores the service account's private key.
    • Replace the FIREBASE_URL placeholder with the URL of the Realtime Database from the previous step.

    The following example shows the placeholders in the web.xml file:

    <init-param>
     <param-name>credential</param-name>
     <param-value>/WEB-INF/JSON_FILE_NAME</param-value>
    </init-param>
    <init-param>
     <param-name>databaseUrl</param-name>
     <param-value>FIREBASE_URL</param-value>
    </init-param>

Build and deploy

To build and run the backend module locally:

mvn clean package appengine:run

To deploy the backend module to App Engine:

mvn clean package appengine:deploy

View user event logs

Run the Android client app, perform some activities such as signing in and switching channels, and go to the following URL to view user event logs:

https://[project-id].appspot.com/printLogs

License

Copyright 2018 Google LLC. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

This is not an official Google product.