|
| 1 | +# Build a mobile app using Firebase and App Engine flexible environment |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +This repository contains Android client sample code for the [Build a Mobile App |
| 6 | +Using Firebase and App Engine Flexible |
| 7 | +Environment](https://cloud.google.com/solutions/mobile/mobile-firebase-app-engine-flexible) |
| 8 | +solution. You can find the sample code for the Android client code in the |
| 9 | +[firebase-android-client](../../../firebase-android-client) repository. |
| 10 | + |
| 11 | +## Deployment requirements |
| 12 | + |
| 13 | +- Enable the following services in the [Google Cloud Platform |
| 14 | + console](https://console.cloud.google.com): |
| 15 | + - Google App Engine |
| 16 | + - Google Compute Engine |
| 17 | +- Sign up for [Firebase](https://firebase.google.com/) and create a new project |
| 18 | + in the [Firebase console](https://console.firebase.google.com/). |
| 19 | +- Install the following tools in your development environment: |
| 20 | + - [Java 8](https://java.com/en/download/) |
| 21 | + - [Apache Maven](https://maven.apache.org/) |
| 22 | + - [Google Cloud SDK](https://cloud.google.com/sdk/) |
| 23 | + |
| 24 | +> **Note**: Firebase is a Google product, independent from Google Cloud |
| 25 | +> Platform. |
| 26 | +
|
| 27 | +A Java application deployed to App Engine Flexible Environment [needs to use Java 8 Runtime](https://cloud.google.com/appengine/docs/flexible/java/setting-up-environment). |
| 28 | +However, in your local development environment you can |
| 29 | +use JDK 8 or newer as long as your JDK is able to produce Java 8 class files. |
| 30 | + |
| 31 | +## Google Cloud SDK setup |
| 32 | + |
| 33 | +Configure the SDK to access the Google Cloud Platform by using the following |
| 34 | +command: |
| 35 | + |
| 36 | +```bash |
| 37 | +gcloud auth login |
| 38 | +``` |
| 39 | + |
| 40 | +Get the project ID from the settings page of your Firebase project. Use the |
| 41 | +following command to set your Firebase project as the active project for the |
| 42 | +SDK: |
| 43 | + |
| 44 | +```bash |
| 45 | +gcloud config set project [project-id] |
| 46 | +``` |
| 47 | + |
| 48 | +## Configuration |
| 49 | + |
| 50 | +Enable the Google sign-in provider by following these steps: |
| 51 | + |
| 52 | +1. Sign in to the [Firebase console](https://console.firebase.google.com) and |
| 53 | + select your project. |
| 54 | +1. In the **Develop** section, select **Authentication**. |
| 55 | +1. In the **Authentication** page, select **Sign-in Method**. |
| 56 | +1. Select and enable the **Google** sign-in provider. |
| 57 | + |
| 58 | +Follow these steps to configure a service account for the backend application: |
| 59 | + |
| 60 | +1. Go to your project settings page on the [Firebase |
| 61 | + console](https://console.firebase.google.com). |
| 62 | +1. Click the **Settings** gear next to 'Project Overview' and then **Project settings**. |
| 63 | +1. Select **Service accounts** and click the link **Manage service account permissions**. |
| 64 | +1. In the **IAM & admin** page click **Create service account**. |
| 65 | +1. In the dialog, create an account with the following parameters: |
| 66 | + * Enter *playchat-servlet* in the **Service account name** field. |
| 67 | + * Select **Project** > **Owner** in the **Role** menu. |
| 68 | + > **Caution**: The owner role gives the service account full access to all |
| 69 | + > resources in the project. In a production app, you should change the role |
| 70 | + > to the minimum access that your service account requires. |
| 71 | +1. After the service account is created, click it and choose **Create new key** in the **ADD KEY** dropdown button. |
| 72 | + * Choose **JSON** as the key type. |
| 73 | + * Click **CREATE** to download the key. |
| 74 | +1. After you finish creating the account, your browser downloads the service |
| 75 | + account's private key to your computer as a JSON file. Move the file to the |
| 76 | + `src/main/webapp/WEB-INF` folder in the backend project. |
| 77 | +1. From the left menu of the [Firebase |
| 78 | + console](https://console.firebase.google.com), |
| 79 | + select **Database** in the **Develop** group. |
| 80 | + |
| 81 | +1. In the **Database** page, click **Create database** in the **Realtime Database** section. |
| 82 | + |
| 83 | +1. In the **Security rules for Realtime Database** dialog, select **Start in |
| 84 | + test mode** and click **Enable**. |
| 85 | + |
| 86 | + Caution: Test mode allows anyone with your database reference to perform |
| 87 | + read and write operations to your database. If test mode isn't appropriate |
| 88 | + for your purposes, you can write security rules to manage access to your |
| 89 | + data. For more information, see |
| 90 | + [Get Started with Database Rules](https://firebase.google.com/docs/database/security/quickstart) |
| 91 | + in the Firebase documentation. |
| 92 | + |
| 93 | + This step displays the data you’ve stored in Firebase. In later steps of |
| 94 | + this tutorial, you can revisit this web page to see data added and updated |
| 95 | + by the client app and backend servlet. |
| 96 | +1. In the **Rules** tab of the database, make sure you have the security rules for read/write. For example: |
| 97 | + ```json |
| 98 | + { |
| 99 | + "rules": { |
| 100 | + ".read": true, |
| 101 | + ".write": true |
| 102 | + } |
| 103 | + } |
| 104 | + ``` |
| 105 | +1. Make a note of the Firebase URL for your project, which is in the form |
| 106 | + `https://[project-id].firebaseio.com/` and appears next to a |
| 107 | + link icon. |
| 108 | +1. Open the `src/main/webapp/WEB-INF/web.xml` file and do the following: |
| 109 | + * Replace the `JSON_FILE_NAME` placeholder with the JSON file from that |
| 110 | + stores the service account's private key. |
| 111 | + * Replace the `FIREBASE_URL` placeholder with the URL of the Realtime |
| 112 | + Database from the previous step. |
| 113 | + |
| 114 | + The following example shows the placeholders in the `web.xml` file: |
| 115 | + ```xml |
| 116 | + <init-param> |
| 117 | + <param-name>credential</param-name> |
| 118 | + <param-value>/WEB-INF/JSON_FILE_NAME</param-value> |
| 119 | + </init-param> |
| 120 | + <init-param> |
| 121 | + <param-name>databaseUrl</param-name> |
| 122 | + <param-value>FIREBASE_URL</param-value> |
| 123 | + </init-param> |
| 124 | + ``` |
| 125 | + |
| 126 | + |
| 127 | +## Build and deploy |
| 128 | + |
| 129 | +To build and run the backend module locally: |
| 130 | + |
| 131 | +```bash |
| 132 | +mvn clean package appengine:run |
| 133 | +``` |
| 134 | + |
| 135 | +To deploy the backend module to App Engine: |
| 136 | + |
| 137 | +```bash |
| 138 | +mvn clean package appengine:deploy |
| 139 | +``` |
| 140 | + |
| 141 | +## View user event logs |
| 142 | + |
| 143 | +Run the Android client app, perform some activities such as signing in and |
| 144 | +switching channels, and go to the following URL to view user event logs: |
| 145 | + |
| 146 | +```bash |
| 147 | +https://[project-id].appspot.com/printLogs |
| 148 | +``` |
| 149 | + |
| 150 | +## License |
| 151 | + |
| 152 | +Copyright 2018 Google LLC. All Rights Reserved. |
| 153 | + |
| 154 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use |
| 155 | +this file except in compliance with the License. You may obtain a copy of the |
| 156 | +License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by |
| 157 | +applicable law or agreed to in writing, software distributed under the License |
| 158 | +is distributed on an "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 159 | +KIND, either express or implied. See the License for the specific language |
| 160 | +governing permissions and limitations under the License. |
| 161 | + |
| 162 | +This is not an official Google product. |
0 commit comments