3.5 KiB
3.5 KiB
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.jsonandfcm_v1_helper.phpto send via FCM HTTP v1 API. - Android apps: Each app uses
google-services.jsonthat 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-appgoogle-services.jsonas in test/app/google-services.json (or ensure your Firebase Console has these three Android apps under ngojol-trial).
- User:
2. Login and FCM token (FCM v1)
- Token retrieval: All apps use
FirebaseMessaging.getInstance().getToken()(async) instead of deprecatedFirebaseInstanceId.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 asreg_idin the login request so the server can target this device for FCM. - RegisterActivity (User): Registration request sends the FCM token via
getToken().addOnCompleteListener(...)thenrequest.setToken(...)andservice.register(request). - onNewToken: In each app’s
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()andsend_generic_to_topic()callfcm_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
MessagingServiceextendingFirebaseMessagingService:- User:
id.ontime.customer.utils.api.service.MessagingService - Driver:
id.ontime.driver.utils.api.service.MessagingService - Merchant:
id.ontime.merchant.utils.api.service.MessagingService
- User:
onMessageReceived(RemoteMessage)handles data payloads (e.g.type1=order, 2=chat, 3/4=other), shows notifications and updates UI (including broadcasts for order status).- No legacy FCM or
FirebaseInstanceIdusage; token handling is FCM v1 only.
5. Checklist
- BaseApp: FCM token via
FirebaseMessaging.getToken()async + topic subscribe. - LoginActivity: Send
reg_idto backend using token fromgetToken().addOnCompleteListener(...). - RegisterActivity (User): Send token with register request via async
getToken(). - MessagingService:
onNewToken()implemented; token stored in Realm and posted on EventBus. - Backend: Sends all FCM via
fcm_v1_helper(FCM HTTP v1). - All apps and backend use the same Firebase project (ngojol-trial) and matching
google-services.json/ service account.