Introducing a new way to review your PRs.

Read the announcement

ndolestudio / httpsms

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API with end to end encryption supported.

View on GitHub

22 documents · Updated Nov 18, 2026

docs / push-delivery-and-read-receipts

Push delivery and read receipts

The push delivery path moves an outbound message from an asynchronous backend event to an Android phone through Firebase Cloud Messaging (FCM). The phone then reports SMS send and delivery results to the API, while the web client acknowledges attention by clearing a thread's unread count. These signals remain separate: FCM acceptance, SMS delivery, and a user reading a thread are different events.

Delivery system

The delivery system separates work scheduling from phone work. The backend first schedules a notification event, a queue later sends that event to a consumer, and the consumer sends a small FCM data message that tells the Android agent which message to fetch.

End-to-end delivery path

The following flow shows the main hand-offs from an outbound API request to message state updates.

flowchart LR
    A[Outbound message request] --> B[Message notification scheduled]
    B --> C[Push queue]
    C --> D[Event consumer]
    D --> E[FCM data message]
    E --> F[Android phone agent]
    F --> G[Fetch outstanding message]
    G --> H[Android SMS or MMS service]
    H --> I[Sent or delivered broadcast]
    I --> J[Message event API]
    J --> K[Message and thread state]

The queue and FCM service have different jobs. The queue carries backend events to the event consumer. FCM wakes or reaches the Android agent. The agent fetches the message from the API instead of receiving the message body in the FCM payload. See SMS message lifecycle for the wider message state flow.

References: api/…/phone_notification_service.go• L185-243 api/…/event_dispatcher_service.go• L63-104 android/…/FirebaseMessagingService.kt• L21-40

Push queue role

The push queue is the asynchronous bridge for CloudEvents. A task contains the event body, a target consumer URL, an HTTP method, and the API key needed by that consumer. In the normal deployment, Google Cloud Tasks holds the task until its scheduled time and returns a queue task name. The local mode performs the same delayed HTTP hand-off in process.

The queue reports that a task was accepted for processing. It does not report that the Android phone received an FCM message or that the SMS reached its destination. This boundary keeps queue health separate from phone delivery state.

References: api/…/push_queue_service.go• L10-30 api/…/google_cloud_push_queue_service.go• L40-95 api/…/emulator_push_queue.go• L38-82

FCM delivery path

FCM is a wake-up and work-start signal for the Android agent. The backend sends a data message with the message ID and the phone's current registration token. The phone uses that ID to retrieve the outstanding message through the authenticated API.

The backend uses one FCM client boundary for its notification transports. The boundary accepts both the FCM message and the target phone identity, so an adapter transport can use that identity when it signs a request. The production client passes the message to the Firebase messaging service and returns its delivery identifier. A configured local emulator can use the same boundary without changing the scheduling or phone-work flow.

Notification scheduling

PhoneNotificationService creates a pending phone notification and emits a scheduled event before the send time. A later notification-send event loads the phone and its current FCM token. The service sends the message with normal Android priority and the phone's message expiration period as the FCM time-to-live.

If the phone has no token or FCM rejects the send, the notification is marked failed through the event path. If FCM accepts the message, the backend records the FCM result and starts the message expiration check. FCM acceptance is therefore a notification-sent state, not an SMS-sent or SMS-delivered state.

References: api/…/phone_notification_service.go• L128-168 api/…/phone_notification_service.go• L351-420 api/…/phone_notification_listener.go• L120-143

Android message work

The Android agent receives two kinds of data messages. A heartbeat message starts a heartbeat request. A message notification carries KEY_MESSAGE_ID and starts one-time network work for that message. The worker checks login, message, destination, SIM, and encryption state before it sends the fetched SMS or MMS through Android telephony.

The FCM payload does not carry the message content. This design makes the API the source for the current message and lets the phone apply its own checks before sending. SMS can use one or more parts. MMS work downloads its attachments before it builds and sends the multimedia message. Local validation or send setup errors create a deferred FAILED event for the API. Token refresh sends the new token back for each active SIM, so later notifications use the current device registration.

References: android/…/FirebaseMessagingService.kt• L21-57 android/…/FirebaseMessagingService.kt• L83-120 android/…/FirebaseMessagingService.kt• L134-187 android/…/FirebaseMessagingService.kt• L204-323 android/…/FirebaseMessagingService.kt• L326-415

FCM token registration

An FCM token identifies the app installation that can receive a push. Android stores a new token when Firebase creates or refreshes it, then updates the API through the phone FCM-token endpoint. The update includes the phone number and SIM slot, which lets a dual-SIM phone associate the shared app token with both active phone records.

The token update is part of phone connectivity. If a token is missing or stale, the backend cannot start the Android send worker. See phone registration and API keys for the related device identity and authentication contract, phone heartbeats and availability for the health signal, and Android phone agent for the device-side service.

References: android/…/FirebaseMessagingService.kt• L43-57 android/…/FirebaseMessagingService.kt• L104-120 android/…/HttpSmsApiService.kt• L260-295 api/…/phone_fcm_token_request.go• L12-39

Device acknowledgements

The Android phone reports the result of the telephony operation after it receives and processes the FCM-triggered work. These reports are separate from the FCM send result and travel back to the API as authenticated message events.

SMS send and delivery events

Android registers receivers for the telephony sent and delivered broadcasts. The sent receiver reports whether the phone handed the SMS to the carrier service. The delivered receiver reports whether the carrier returned a delivery report. Both receivers store the event in deferred work that waits for network access and retries when the API call fails.

Multipart SMS uses segment IDs while Android sends the parts, but only the final part carries the base message ID for send and delivery reporting. The API ignores segment events so one logical SMS does not receive several state updates.

MMS uses the same sent-event channel after Android hands the multimedia request to the telephony service. Its send path does not provide the same separate delivered callback as SMS, so SMS and MMS do not expose identical delivery signals.

The device sends SENT, DELIVERED, or FAILED to POST /v1/messages/{messageID}/events. A successful event call updates the message and returns the current message to the phone. A deleted message is treated as a successful terminal response for the device worker.

References: android/…/Receiver.kt• L10-71 android/…/SentReceiver.kt• L19-31 android/…/SentReceiver.kt• L54-120 android/…/DeliveredReceiver.kt• L18-92 android/…/SmsManagerService.kt• L47-64 android/…/FirebaseMessagingService.kt• L204-218 android/…/FirebaseMessagingService.kt• L326-363 android/…/HttpSmsApiService.kt• L65-75 android/…/HttpSmsApiService.kt• L221-257

Backend acknowledgement handling

The message event endpoint validates the event, checks the phone API key against the message owner, and passes the event to MessageService. SENT changes the message to sent. DELIVERED changes it to delivered and records the delivery time. FAILED records the failure reason and changes the message to failed.

The same phone event also updates the message thread's last-message preview and status. Inbound received events increase the thread's unread_count; outbound and delivery updates do not. A thread can therefore show delivered activity while preserving its existing unread count.

References: api/…/message_handler.go• L265-325 api/…/message_service.go• L272-304 api/…/message.go• L24-68 api/…/message_listener.go• L97-117 api/…/message_thread_listener.go• L148-172 api/…/message_thread.go• L9-25

Read-receipt propagation

In this system, a read receipt is a thread-level web state represented by unread_count. It answers whether the user has cleared the conversation's unread activity. It is not the same as the Android DELIVERED event, which answers whether the phone's SMS delivery report reached the API.

Thread read state

Each message thread exposes unread_count rather than a per-message read flag. New inbound SMS activity and missed calls increase that count. New outbound activity and later send, delivery, failure, scheduling, or expiry updates preserve it. A newly created thread starts at zero unless the event marks it unread.

The thread also keeps the latest message preview and status. These fields can change as phone events arrive without changing the user's read acknowledgement. This separation lets delivery progress appear in the thread list without making an already read thread unread.

References: api/…/message_thread.go• L9-25 api/…/message_thread_listener.go• L202-255 api/…/message_thread_service.go• L52-64 api/…/message_thread_service.go• L87-140 api/…/gorm_message_thread_repository.go• L38-70

Read update path

The web client clears a thread's unread count when the user opens or otherwise reads it. It uses the existing PUT /v1/message-threads/{messageThreadID} endpoint with { "unread_count": 0 }. The API applies only the requested unread or archive fields within the authenticated user's thread and returns the updated thread.

The web store replaces its local copy after a successful response. If the request fails, it reports that the thread could not be marked as read and reloads the thread list. The read state remains per thread rather than per message. There is no Android read UI and no per-message read receipt in this path.

The thread list endpoint can request matching contact details as read-time display data. These details are not persisted in the thread and do not change its unread count.

References: web/…/threads.ts• L98-146 api/…/message_thread_handler.go• L95-137 api/…/message_thread_update_request.go• L10-26 api/…/message_thread_service.go• L151-165 api/…/message_thread.go• L14-25 api/…/gorm_message_thread_repository.go• L62-70

Realtime read propagation

Inbound SMS and missed-call events can reach the web client through the existing realtime event path. When the selected thread is open, the client refreshes its messages and can clear that thread's unread count. When another thread receives inbound activity, its unread count stays above zero until the user reads it.

This behavior links notification state to user attention without using delivery events as read signals. For the related realtime and thread behavior, see phone connectivity and SMS message lifecycle.

References: api/…/message_thread_listener.go• L202-255 api/…/message_phone_delivered_event.go• L11-25 web/…/threads.ts• L103-146 api/…/message_thread.go• L14-25

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the FCM data key for message IDs aligned between PhoneNotificationService and FirebaseMessagingService; a key mismatch stops the Android worker before it can fetch the message.
  • Keep the phone event names and message ID rules aligned between Android event workers and the message event validator; multipart segment IDs must not become separate logical message events.
  • Preserve the distinction between FCM acceptance, phone SMS events, and thread unread state when changing status transitions or API responses.
  • Keep the FCM token update contract aligned for both SIM slots so token refresh does not leave one phone record unable to receive pushes.
  • Keep inbound unread increments separate from outbound and delivery activity updates; otherwise a delivery callback can create a false unread notification.
  • Keep the unread_count update contract aligned between the web store, thread update request, and thread status persistence.
  • Keep optional thread contact enrichment separate from persisted unread and archive fields when changing the thread list or thread update response.
  • If the event consumer URL or queue authentication changes, update the queue task contract and the consumer authorization together.
  • Keep the FCMClient send contract aligned with both the production Firebase client and any local transport; a changed result or error contract can alter notification state handling.

Important failure modes

TriggerConsequenceGuard
Phone has no FCM token or Firebase rejects the notificationNotification becomes failed and the Android agent does not start the send workRefresh the token through the phone FCM-token path and surface the notification failure
FCM accepts a data message but Android cannot access the APIThe phone does not fetch or send the outstanding SMSAndroid work requires network access and retries its event calls; message expiration handles work that never completes
Android token refresh updates only one SIM recordPushes to the other active SIM record can failToken registration updates both active phone records
MMS attachment download, message composition, or telephony setup failsThe multimedia message is not sent and the backend receives a failed state instead of a send callbackThe Android worker queues a FAILED event under a network constraint
Sent or delivered event uses a multipart segment IDThe API ignores the duplicate segment eventReport only the base message ID for the final segment
A delivery or outbound activity update is treated as unread activityThe web client shows a false unread threadOnly inbound received and missed-call events set MarkAsUnread; delivery updates only update thread activity
Read update targets a thread outside the authenticated userNo thread is changed and the API returns not foundScope the update by user ID and thread ID
Thread contact lookup fails while contact enrichment is requestedThe thread list still returns without matching contact detailsTreat enrichment as optional display data and keep the core thread result available

Key files

FileWhy to read it
api/…/phone_notification_service.goDefines notification scheduling, FCM payloads, notification failure handling, and the expiration hand-off.
api/…/fcm_client.goDefines the common FCM send boundary, the target phone identity passed to transports, and the production Firebase transport used by notification delivery.
api/…/event_dispatcher_service.goShows how CloudEvents become delayed HTTP tasks and how the event consumer receives them.
api/…/google_cloud_push_queue_service.goShows the production queue boundary and task scheduling behavior.
android/…/FirebaseMessagingService.ktShows FCM reception, outstanding-message fetches, token refresh, and SMS or MMS work dispatch.
android/…/SmsManagerService.ktShows how the phone selects a SIM and exposes SMS or MMS send and delivery callback paths.
android/…/HttpSmsApiService.ktDefines the Android API calls for FCM token updates and message event acknowledgements.
android/…/SentReceiver.ktShows how the phone reports SMS send success or failure.
android/…/DeliveredReceiver.ktShows how the phone reports carrier delivery results.
api/…/message_handler.goDefines the authenticated message event endpoint used by the phone.
api/…/message_service.goApplies phone events to message status and schedules expiration checks.
api/…/message_thread.goDefines thread activity fields, the unread_count read state, and read-time contact details.
api/…/message_thread_service.goConnects inbound activity, user unread-count updates, and optional contact enrichment to thread state.
api/…/message_thread_handler.goDefines the authenticated thread list and update endpoints used by the web client.
api/…/gorm_message_thread_repository.goShows how activity and unread-count updates are persisted separately from archive changes.
web/…/threads.tsShows when the web client clears a thread's unread count and how it handles failed updates.