Firebase Integration
Overview
Firebase is used for two purposes in the API:
- Firebase Cloud Messaging (FCM) - Push notifications to mobile devices
- Firebase Realtime Database - Real-time chat message storage
Setup
Firebase is initialized in api/include/dbcon.php using the Kreait Firebase Admin SDK:
use Kreait\Firebase\Factory;
$factory = (new Factory)
->withServiceAccount(json_decode($_ENV['FIREBASE_CREDENTIALS'], true))
->withDatabaseUri($_ENV['FIREBASE_DATABASE_URL']);
$database = $factory->createDatabase();
$messaging = $factory->createMessaging();The FIREBASE_CREDENTIALS environment variable contains the full Firebase service account JSON (stored as a string in AWS Secrets Manager).
Firebase Projects by Environment
| Environment | Firebase Database URL |
|---|---|
| UK Dev | https://housr-firebase.firebaseio.com |
| UK UAT | https://housr-firebase-uat.firebaseio.com |
| UK Prod | https://housr-firebase-default-rtdb.firebaseio.com |
| US Demo | https://housr-us-demo.firebaseio.com |
| US Dev | https://myhousr-dev.firebaseio.com |
| US UAT | https://myhousr-uat.firebaseio.com |
| US Prod | https://myhousr-prod.firebaseio.com |
Cloud Messaging (FCM)
Push notifications are sent using the Kreait Messaging service.
Sending Notifications
From api/utils/notificationUtils.php:
function sendNotification(string $title, string $body, array $data = [], array $fcmTokens = [])
{
global $messaging;
foreach ($fcmTokens as $fcmToken) {
$message = CloudMessage::withTarget(MessageTarget::TOKEN, $fcmToken)
->withNotification(Notification::create($title, $body))
->withData($data)
->withHighestPossiblePriority();
$messaging->send($message);
}
}FCM Token Management
- Tokens are stored in the
users.fcm_tokencolumn - Updated via
api/updateFCM.phpwhen the app registers/refreshes - Set to NULL when the user signs out
Legacy FCM (Direct HTTP)
Some older code (e.g., in signup.php) uses direct HTTP calls to the FCM legacy API (https://fcm.googleapis.com/fcm/send) with a server key, rather than the Kreait SDK. This pattern predates the SDK adoption.
Realtime Database
The Firebase Realtime Database is used for real-time chat messaging. The $database object provides read/write access.
The API manages chat room metadata in MySQL (via chat_rooms and chat_room_members tables), while actual message content and real-time delivery is handled through Firebase Realtime Database. The mobile app reads and writes messages directly to Firebase.
Dependencies
From composer.json:
"kreait/laravel-firebase": "^5.3.0"Note: Despite the package name including “laravel”, it’s used standalone (not within a Laravel framework). Only the Firebase Factory and service classes are used.