Skip to main content

Overview

The Sales Management App uses Firebase as its backend-as-a-service platform, providing authentication, real-time data synchronization, file storage, and push notifications.
This guide assumes you have already created a Firebase project in the Firebase Console.

Firebase Project Configuration

Step 1: Add Android App to Firebase

  1. In the Firebase Console, click “Add app” and select Android
  2. Register your app with the package name:
    project.avishkar.salesmanagement
    
  3. Download the google-services.json file
  4. Place it in the app/ directory of your Android project
Never commit google-services.json to public repositories as it contains sensitive configuration data.

Step 2: Add Firebase SDK

The Firebase SDK is configured via Gradle dependencies:
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4'
        // Google Services plugin for Firebase
        classpath 'com.google.gms:google-services:4.0.1'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url 'https://maven.google.com'
        }
    }
}
The apply plugin: 'com.google.gms.google-services' line must be at the bottom of the app-level build.gradle file, after all dependencies.

Firebase Authentication

The app uses Firebase Authentication to manage user accounts for both sales managers and salespeople.

Enable Authentication Methods

  1. In Firebase Console, go to Authentication > Sign-in method
  2. Enable Email/Password authentication
  3. (Optional) Enable other sign-in methods as needed

Authentication Flow

The app implements separate registration flows:
// Sales Manager Registration
FirebaseAuth.getInstance()
    .createUserWithEmailAndPassword(email, password)
    .addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
            // Save manager data to Realtime Database
            FirebaseUser user = task.getResult().getUser();
            saveSalesManagerToDatabase(user.getUid());
        }
    });

// Salesperson Registration
FirebaseAuth.getInstance()
    .createUserWithEmailAndPassword(email, password)
    .addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
            // Save salesperson data to Realtime Database
            FirebaseUser user = task.getResult().getUser();
            saveSalespersonToDatabase(user.getUid());
        }
    });

Firebase Realtime Database

The Realtime Database stores all app data including user profiles, inventory, messages, and leaderboard information.

Database Structure

Recommended database structure for the Sales Management App:
{
  "salesManagers": {
    "<manager_uid>": {
      "name": "John Doe",
      "email": "[email protected]",
      "number": "+1234567890",
      "orgName": "Sales Corp",
      "password": "hashed_password",
      "inventoryItems": {
        "item1": {
          "itemName": "Product A",
          "total_available": 100,
          "sold": 25,
          "profit": 75
        }
      }
    }
  },
  "salesPeople": {
    "<salesperson_uid>": {
      "name": "Jane Smith",
      "email": "[email protected]",
      "number": "+1234567891",
      "managerName": "John Doe",
      "password": "hashed_password",
      "inventoryItems": { }
    }
  },
  "messages": {
    "<chat_room_id>": {
      "<message_id>": {
        "text": "Hello team!",
        "sender": "Jane Smith",
        "timestamp": 1234567890
      }
    }
  },
  "leaderboard": {
    "<salesperson_uid>": {
      "name": "Jane Smith",
      "totalSales": 1500,
      "rank": 1
    }
  }
}

Database Rules

Configure security rules to protect user data:
{
  "rules": {
    "salesManagers": {
      "$uid": {
        ".read": "auth != null && auth.uid == $uid",
        ".write": "auth != null && auth.uid == $uid"
      }
    },
    "salesPeople": {
      "$uid": {
        ".read": "auth != null",
        ".write": "auth != null && auth.uid == $uid"
      }
    },
    "messages": {
      ".read": "auth != null",
      ".write": "auth != null"
    },
    "leaderboard": {
      ".read": "auth != null",
      ".write": "auth != null"
    }
  }
}
The production rules provide more granular access control, ensuring that managers can view their team members’ data while salespeople can only access their own data.

Constants Configuration

The app defines database paths in a Constants class:
package project.avishkar.salesmanagement;

public class Constants {
    public static final String STORAGE_PATH_UPLOADS = "uploads/";
    public static final String DATABASE_UPLOADS = "uploads";
}

Firebase Storage

Firebase Storage is used for uploading and storing files such as profile pictures and product images.

Storage Structure

uploads/
├── profile_images/
│   ├── <user_uid>.jpg
│   └── <user_uid>.jpg
├── product_images/
│   ├── <product_id>.jpg
│   └── <product_id>.jpg
└── documents/
    └── <file_name>.pdf

Storage Rules

Configure storage security rules:
rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    // Allow authenticated users to upload files
    match /uploads/{allPaths=**} {
      allow read: if request.auth != null;
      allow write: if request.auth != null
                   && request.resource.size < 5 * 1024 * 1024  // Max 5MB
                   && request.resource.contentType.matches('image/.*');  // Images only
    }
    
    // Profile images - users can only modify their own
    match /uploads/profile_images/{userId} {
      allow read: if request.auth != null;
      allow write: if request.auth != null && request.auth.uid == userId;
    }
  }
}

FileProvider Configuration

The app uses FileProvider for secure file sharing:
<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

Firebase Cloud Messaging

FCM enables push notifications for real-time updates.

Setup Steps

  1. In Firebase Console, go to Cloud Messaging
  2. Note your Server Key and Sender ID
  3. The app includes FCM dependency:
    implementation 'com.google.firebase:firebase-messaging:10.2.1'
    

Notification Types

The app can send notifications for:
  • New chat messages
  • Inventory updates
  • Leaderboard rank changes
  • Team announcements
  • Performance alerts
Implement a FirebaseMessagingService to handle incoming FCM messages and display notifications to users.

Testing Firebase Integration

Verification Checklist

1

Authentication

Test user registration and login for both managers and salespeople
2

Database

Verify data is being written and read correctly from Realtime Database
3

Storage

Upload test images and verify they appear in Firebase Storage console
4

Messaging

Send test notifications from Firebase Console to verify FCM setup

Troubleshooting

Ensure the file is placed in the app/ directory (not in app/src/). Rebuild the project after adding the file.
Check that the package name in google-services.json matches your app’s applicationId in build.gradle.
Verify your database rules allow authenticated users to read/write. Check that users are successfully authenticated before accessing the database.
Ensure storage rules permit uploads and the file size is within limits. Check that the app has WRITE_EXTERNAL_STORAGE permission.

Security Best Practices

Never store passwords in plain text. The current implementation should be updated to use Firebase Authentication exclusively for password management.

Enable App Check

Protect your Firebase resources from abuse by enabling App Check

Monitor Usage

Set up billing alerts and monitor Firebase usage regularly

Secure Rules

Regularly audit and test your security rules

Environment Config

Use separate Firebase projects for development and production

Next Steps

Data Models

Learn about the core data models used throughout the application

Build docs developers (and LLMs) love