Introducing a new way to review your PRs.

Read the announcement

capcom6 / android-sms-gateway

The SMS Gateway for Android™ app enables sending and receiving SMS messages through an API that can be accessed directly on the device or via a cloud server when direct device access is not possible.

View on GitHub

20 documents · Updated Nov 20, 2026

docs / inbound-sms-mms-capture

Inbound SMS and MMS capture

Inbound capture turns Android telephony events and inbox records into one message flow for storage and webhook delivery. It supports text SMS, binary data SMS, early MMS notifications, and fully downloaded MMS content so downstream systems can receive messages as they arrive or request a later export. See Message processing for the wider message lifecycle and Outbound message delivery for the related send path.

Capture paths

The receiver starts two complementary paths. Broadcast receivers provide low-latency delivery when Android sends an event, while content observers provide a recovery path when the telephony provider stores a message without delivering the matching broadcast.

Capture service startup

The capture paths depend on the receiver and incoming-message services being active. BootReceiver requests the application services after boot, package replacement, reboot, quick-boot, and shutdown events; the orchestrator starts the capture services when the autostart setting allows that request. Background execution limits can prevent service or receiver startup, in which case the app records a warning and live capture may not resume until normal app startup.

References: app/…/BootReceiver.kt• L10-29 app/…/OrchestratorService.kt• L31-62

Broadcast ingestion

Broadcast ingestion accepts text and data SMS broadcasts plus MMS WAP push notifications. It converts each accepted event into a common inbound message form and sends it to the shared processing service.

flowchart LR
    A[Android SMS broadcast] --> C[Message receiver]
    B[Android MMS WAP push] --> D[MMS receiver]
    C --> E[Inbound message]
    D --> F[MMS header message]
    E --> G[Shared processing]
    F --> G

Text SMS segments are joined into one message. Data SMS keeps its binary user data and uses the registered data port. An MMS broadcast carries notification data rather than the full message, so this path emits the sender, transaction details, subject, size, and content class as an early MMS record. The first meaningful source is app/…/MessagesReceiver.kt• L15-51; the MMS path is app/…/MmsReceiver.kt• L20-88, and the shared input shape is app/…/InboxMessage.kt• L6-60.

References: app/…/MessagesReceiver.kt• L15-51 app/…/MmsReceiver.kt• L20-88 app/…/InboxMessage.kt• L6-60

Content-provider monitoring

Content-provider monitoring watches the Android SMS and MMS inboxes for rows that appear without a usable broadcast. SMS monitoring is controlled by the inbox setting and is enabled by default; MMS monitoring is always started by the receiver service when the service is active.

Each observer keeps a persistent high-water mark based on the provider row ID. On start it skips rows that already exist, then catches up rows added while the app was stopped. The SMS observer creates text messages from new inbox rows. The MMS observer selects downloaded inbox messages and sends their full content through the same processing service.

flowchart LR
    A[Telephony inbox row] --> B[Content observer]
    B --> C{New provider ID}
    C -->|SMS| D[Text message]
    C -->|Downloaded MMS| E[Full MMS message]
    D --> F[Shared processing]
    E --> F
    F --> G[Advance high-water mark]

The MMS observer can retry a read when the parent row exists before its sender or parts are ready. A missing READ_SMS grant stops provider monitoring and leaves the broadcast path as the available capture path. The service setup is in app/…/ReceiverService.kt• L39-55; the SMS observer is app/…/SmsContentObserver.kt• L19-84, and the MMS observer is app/…/MmsContentObserver.kt• L16-73.

References: app/…/ReceiverService.kt• L39-55 app/…/SmsContentObserver.kt• L19-84 app/…/MmsContentObserver.kt• L16-73

Message parsing

The shared processing service separates source-specific parsing from message handling. It maps each source into a type that preserves sender, receive time, SIM subscription, and content needed for storage or export.

SMS and data SMS

Text SMS becomes a string message, while data SMS becomes a byte array. Both retain the originating address, timestamp, and subscription ID when Android provides one. Webhook payloads add the selected SIM number and recipient phone number when the subscription can be mapped to a SIM slot.

The stored preview contains the full text for a text SMS. For data SMS it contains a short hexadecimal view of the bytes, while the webhook carries the full byte array as unwrapped Base64. The mapping is in app/…/ReceiverService.kt• L145-172; its payload types are in app/…/SmsEventPayload.kt• L40-56, and preview creation is in app/…/IncomingMessagesService.kt• L31-47.

References: app/…/ReceiverService.kt• L145-172 app/…/SmsEventPayload.kt• L40-56 app/…/IncomingMessagesService.kt• L31-47

MMS headers and content

MMS capture has two useful stages. The WAP push stage parses the notification header and exposes an mms:received event. The content-provider stage reads the downloaded message and exposes an mms:downloaded event with its body and attachments.

The content reader obtains the message subject and date, finds the sender, and combines all non-empty plain-text parts into the body. It ignores SMIL presentation parts as attachments. Other parts remain attachments with their content type, name, size, and encoded data. Subject text may be repaired when a device provider has decoded common non-ASCII text with the wrong character set. The read model is in app/…/MmsContentReader.kt• L12-22; the read flow is app/…/MmsContentReader.kt• L32-131, and the two payload forms are app/…/MmsReceivedPayload.kt• L5-15 and app/…/MmsDownloadedPayload.kt• L5-21.

References: app/…/MmsContentReader.kt• L12-22 app/…/MmsContentReader.kt• L32-131 app/…/MmsReceivedPayload.kt• L5-15 app/…/MmsDownloadedPayload.kt• L5-21

Incoming message persistence

Incoming persistence gives the app a local record of captured traffic and a stable lookup point for deduplication. It stores message identity, type, sender, mapped recipient, SIM details, a content preview, and the receive time in the incoming message table.

Message records

The record model distinguishes four states: SMS, data SMS, MMS notification, and downloaded MMS. The incoming list and counters use these types to show recent traffic and separate SMS, data SMS, and MMS totals. Recent records can be filtered by type and loaded in growing pages. Date-range queries support counts and selection for operational views, while retention can delete selected ranges or truncate old records.

An MMS attachment is written after its downloaded message record is inserted. The app decodes the provider data before it stores the bytes in its private files area. The file survives the lifetime of the Android telephony provider row, so later webhook consumers can still access the attachment through the app’s attachment-serving path. The record is defined in app/…/IncomingMessage.kt• L7-30; persistence is in app/…/IncomingMessagesDao.kt• L9-69 and app/…/IncomingMessagesService.kt• L22-65, while file storage is in app/…/MmsAttachmentStorage.kt• L10-52.

The repository provides the read and cleanup boundary for incoming records. The list view model uses that boundary to keep recent records, type filters, totals, and incremental loading in sync with the local store. These roles are in app/…/IncomingMessagesRepository.kt• L10-36 and app/…/IncomingMessagesListViewModel.kt• L14-56.

References: app/…/IncomingMessage.kt• L7-30 app/…/IncomingMessagesDao.kt• L9-69 app/…/IncomingMessagesService.kt• L22-65 app/…/MmsAttachmentStorage.kt• L10-52 app/…/IncomingMessagesRepository.kt• L10-36 app/…/IncomingMessagesListViewModel.kt• L14-56

Deduplication state

Deduplication checks the derived message ID before saving or emitting a webhook. Text and data SMS identities include their message shape and common fields; MMS notifications use the transaction ID; downloaded MMS uses the provider message ID. The notification and downloaded MMS stages therefore remain distinct records because they describe different capture stages.

The content observers use provider row IDs as progress state, while the shared service uses message IDs as duplicate state. These controls serve different purposes: observer marks prevent missed catch-up work from being scanned repeatedly, and message IDs prevent the same logical input from being stored or emitted twice when broadcast and provider paths overlap. Progress state is defined in app/…/StateStorage.kt• L6-22; duplicate identity is built in app/…/IncomingMessagesService.kt• L125-157 and checked in app/…/ReceiverService.kt• L127-143, using the message forms in app/…/InboxMessage.kt• L11-75.

References: app/…/StateStorage.kt• L6-22 app/…/IncomingMessagesService.kt• L125-157 app/…/ReceiverService.kt• L127-143 app/…/InboxMessage.kt• L11-75

Retention and cleanup

Incoming retention removes old message records and their stored MMS attachment directories. A positive lifetime setting enables daily cleanup; the cleanup deadline applies to message records and attachment files as separate local resources.

The inbox setting controls the lifetime in days. If the setting is absent or not positive, scheduled cleanup is not started and the service does not truncate these records through its normal retention task. The setting is in app/…/IncomingMessagesSettings.kt• L8-18; the service cleanup is in app/…/IncomingMessagesService.kt• L133-146, and its daily worker is app/…/IncomingLogTruncateWorker.kt• L22-55.

References: app/…/IncomingMessagesSettings.kt• L8-18 app/…/IncomingMessagesService.kt• L133-146 app/…/IncomingLogTruncateWorker.kt• L22-55

Export and webhook events

Every live capture enters the same service that can export messages already present in the Android provider. This lets the system recover messages from a time range and choose whether to send one webhook per message, grouped webhooks, or no webhooks.

Live webhook delivery

After a message passes duplicate checks and is saved, the receiver builds the event payload and asks the webhook service to queue delivery. The event type follows the message form:

Message formEventPayload focus
Text SMSsms:receivedText and sender details
Data SMSsms:data-receivedBase64 data and sender details
MMS notificationmms:receivedTransaction and header details
Downloaded MMSmms:downloadedBody, subject, and attachment data

The webhook service then applies the configured webhook source rules and queues delivery. This page covers the inbound event creation; delivery retries and endpoint behavior belong in Outbound message delivery. The app-level app/…/EventsReceiver.kt handles outbound send and delivery result broadcasts, so its status callbacks do not create inbound records or trigger message export. Event creation is in app/…/ReceiverService.kt• L100-124; event names are in app/…/WebHookEvent.kt• L5-53, queueing is in app/…/WebHooksService.kt• L153-210, and outbound status handling is in app/…/EventsReceiver.kt• L19-71.

References: app/…/ReceiverService.kt• L100-124 app/…/WebHookEvent.kt• L5-53 app/…/WebHooksService.kt• L153-210 app/…/EventsReceiver.kt• L19-71

Requested message export

The MessagesExportRequestedEvent is an internal event that asks the receiver to read SMS or MMS records from a date range. It carries the message types and a webhook delivery mode, then invokes the same save, deduplication, payload, and webhook path used by live capture.

Export behavior depends on the delivery mode:

ModeResult
IndividualProcesses each selected message and emits its individual event
BatchGroups each event type and sends chunks of up to 100 payloads
DisabledSaves selected messages without emitting webhooks

SMS export reads the Android SMS inbox by sent date. MMS export reads fully downloaded inbox messages by provider date, then reads their body and parts before processing. Invalid or missing event parameters fall back to SMS selection and individual webhook delivery. The event contract is in app/…/MessagesExportRequestedEvent.kt• L11-52; collection is in app/…/EventsReceiver.kt• L12-30, selection and processing are in app/…/ReceiverService.kt• L57-97, and batch emission is in app/…/ReceiverService.kt• L207-247.

References: app/…/MessagesExportRequestedEvent.kt• L11-52 app/…/EventsReceiver.kt• L12-30 app/…/ReceiverService.kt• L57-97 app/…/ReceiverService.kt• L207-247

Change checklist

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

  • Keep the broadcast message types, provider-observer message types, stored enum values, webhook event names, and export batch mapping aligned.
  • Preserve the distinction between provider row progress IDs and derived message deduplication IDs. Changing one does not update the other.
  • Keep inbound export event collection distinct from outbound send and delivery status callbacks.
  • Keep MMS attachment encoding consistent between provider reads, local file storage, and the downloaded MMS payload.
  • When changing MMS retention, update both database truncation and attachment cleanup so one does not outlive the other.
  • When changing export defaults or delivery modes, preserve compatibility with the deprecated triggerWebhooks event field.
  • When changing lifecycle startup or the autostart gate, confirm that the incoming-message and receiver services still start after supported boot and package events.

Important failure modes

TriggerConsequenceGuard
READ_SMS is not grantedContent-provider catch-up does not run.Permission checks stop observers and record a warning.
Autostart is disabled or Android blocks background startupThe receiver and provider observers may not restart after a device or app lifecycle event.The autostart setting gates lifecycle startup, and startup failures are recorded as warnings.
An MMS parent row appears before its sender or partsThe first read can produce incomplete content.The MMS observer retries incomplete reads before processing.
A provider row cannot be read after an observer callbackThe observer still advances its row mark.One corrupt row cannot block later rows, but that message is not recovered by the same scan.
Broadcast and provider paths report the same messageA duplicate save or webhook could occur.The derived incoming message ID is checked before processing.
MMS part data is missing or cannot be decodedThe message can be saved without that attachment’s local bytes.Attachment failures are logged and other parts continue.
Incoming storage insert failsThe processing path can still build and emit the event because persistence errors are logged inside save.Monitor error logs when stored history is required.

Key files

FileWhy to read it
app/…/ReceiverService.ktShows the common path from capture to persistence, payload creation, webhook emission, and export.
app/…/BootReceiver.ktShows which lifecycle events request capture service startup.
app/…/OrchestratorService.ktShows how lifecycle startup enables the incoming-message and receiver services.
app/…/MessagesReceiver.ktShows live text and data SMS capture.
app/…/MmsReceiver.ktShows MMS notification capture and header parsing entry.
app/…/SmsContentObserver.ktShows SMS provider catch-up and progress state.
app/…/MmsContentObserver.ktShows downloaded MMS detection and incomplete-content retry behavior.
app/…/MmsContentReader.ktShows the high-level MMS body, sender, subject, and part read model.
app/…/IncomingMessagesService.ktShows incoming record identity, previews, deduplication, and attachment handoff.
app/…/IncomingMessagesRepository.ktDefines the read, count, filter, and cleanup boundary for incoming records.
app/…/IncomingMessagesListViewModel.ktCoordinates recent-record filters, totals, and incremental list loading for the incoming message view.
app/…/MmsAttachmentStorage.ktShows the private lifetime of stored MMS attachment bytes.
app/…/MessagesExportRequestedEvent.ktDefines the event contract for time-range exports and webhook modes.