Files
Ontime/FCM_V1_PROCESS.md
2026-03-11 15:29:37 +07:00

48 lines
3.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Firebase FCM v1 Process (Login, Send, Receive)
Reference: **test** app (`test/app`). All three live apps and the backend use the same Firebase project **ngojol-trial** and FCM HTTP v1 for login, sending, and receiving messages.
## 1. Firebase project and config
- **Project:** `ngojol-trial` (project_number: 158861273889)
- **Backend:** Uses `ngojol-trial-firebase-adminsdk-lc81n-00c9e935db.json` and `fcm_v1_helper.php` to send via FCM HTTP v1 API.
- **Android apps:** Each app uses `google-services.json` that includes the **id.ontime.*** package entries for this project:
- **User:** `id.ontime.customer`
- **Driver:** `id.ontime.driver`
- **Merchant:** `id.ontime.merchant`
Use the same multi-app `google-services.json` as in **test/app/google-services.json** (or ensure your Firebase Console has these three Android apps under ngojol-trial).
## 2. Login and FCM token (FCM v1)
- **Token retrieval:** All apps use `FirebaseMessaging.getInstance().getToken()` (async) instead of deprecated `FirebaseInstanceId.getInstance().getToken()`.
- **BaseApp (User, Driver, Merchant):** On startup, get token with `getToken().addOnCompleteListener(...)`, then subscribe to topics (`pelanggan` / `driver`+`ouride` / `mitra`+`ouride`) and store the token in Realm.
- **LoginActivity:** On sign-in, get token with `getToken().addOnCompleteListener(...)` and send it to the backend as `reg_id` in the login request so the server can target this device for FCM.
- **RegisterActivity (User):** Registration request sends the FCM token via `getToken().addOnCompleteListener(...)` then `request.setToken(...)` and `service.register(request)`.
- **onNewToken:** In each apps `MessagingService`, `onNewToken()` saves the new token in Realm and posts it on EventBus so the UI can use the latest token; the next login will send the updated token to the backend.
## 3. Sending messages (backend → devices)
- Backend uses **FCM HTTP v1** only:
- `application/helpers/fcm_v1_helper.php`: `fcm_v1_get_access_token()`, `fcm_v1_send($target, $data, $is_topic, $options)`.
- `application/models/Notification_model.php`: `send_generic_to_token()` and `send_generic_to_topic()` call `fcm_v1_send`.
- Config in `application/config/config.php`: `FCM_PROJECT_ID` = `ngojol-trial`, `FCM_CREDENTIALS_PATH` = path to the service account JSON.
- All notification flows (order, chat, top-up, etc.) go through this v1 path.
## 4. Receiving messages (devices)
- Each app has a `MessagingService` extending `FirebaseMessagingService`:
- **User:** `id.ontime.customer.utils.api.service.MessagingService`
- **Driver:** `id.ontime.driver.utils.api.service.MessagingService`
- **Merchant:** `id.ontime.merchant.utils.api.service.MessagingService`
- `onMessageReceived(RemoteMessage)` handles data payloads (e.g. `type` 1=order, 2=chat, 3/4=other), shows notifications and updates UI (including broadcasts for order status).
- No legacy FCM or `FirebaseInstanceId` usage; token handling is FCM v1 only.
## 5. Checklist
- [x] BaseApp: FCM token via `FirebaseMessaging.getToken()` async + topic subscribe.
- [x] LoginActivity: Send `reg_id` to backend using token from `getToken().addOnCompleteListener(...)`.
- [x] RegisterActivity (User): Send token with register request via async `getToken()`.
- [x] MessagingService: `onNewToken()` implemented; token stored in Realm and posted on EventBus.
- [x] Backend: Sends all FCM via `fcm_v1_helper` (FCM HTTP v1).
- [x] All apps and backend use the same Firebase project (**ngojol-trial**) and matching `google-services.json` / service account.