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 / gateway-command-workers

Gateway command synchronization

Gateway command synchronization keeps an Android device aligned with the cloud gateway. It registers the device, receives remote commands, brings cloud messages and configuration into the app, and sends message results back so that the cloud has a current view of device work.

Gateway synchronization boundary

The gateway boundary connects the cloud API with local message, settings, and webhook services. It runs only when cloud mode is enabled and uses saved registration data to authenticate later work. See the Cloud gateway integration page for the wider connection model.

Cloud session

The gateway service starts push registration, message polling, remote webhook sync, remote settings sync, and event collection as one cloud-mode group. It stops these activities together when cloud mode stops, so no worker should continue to act as a cloud device after the gateway is disabled. Connection status and transport health are covered in Connection and device health.

References: app/…/GatewayService.kt• L44-68 app/…/GatewaySettings.kt• L16-48

Command event routing

The cloud can deliver a command through Firebase push data or the server-sent event stream. Both inputs become an external event, and the event router maps that event to a local event that starts the right synchronization action.

The supported gateway commands are:

  • MessageEnqueued, which prompts a message pull.
  • MessageCancelled, which carries a message ID for local cancellation.
  • SettingsUpdated, which prompts a settings pull.
  • WebhooksUpdated, which prompts a webhook pull.
  • MessagesExportRequested, which is passed to the inbound-message export flow.

An event without the data needed for its command cannot be applied. Unknown event names also fail before a local command is emitted.

The following flow shows how cloud notifications reach local workers and services.

flowchart LR
    C[Cloud gateway] --> P[Firebase push]
    C --> S[Server-sent event stream]
    P --> R[External event router]
    S --> R
    R --> E[Local gateway event]
    E --> W[Gateway worker or local service]
    W --> L[Local message, settings, or webhook state]

References: app/…/PushService.kt• L19-51 app/…/SSEForegroundService.kt• L43-79 app/…/EventsRouter.kt• L16-55 app/…/ExternalEventType.kt• L5-20

Device registration

Device registration gives the cloud a device identity and a token for later API calls. The result is stored locally and reused for message pulls, settings and webhook reads, and state reports.

Registration lifecycle

Registration supports a new anonymous device, a device linked with user credentials, and a device linked with a one-time code. A registration request identifies the Android device, its push token when available, and its active SIM cards. If a saved access token exists, the gateway first tries to update the device; an unauthorized response causes a new registration attempt.

The background registration worker handles the saved-device refresh path. It sends the current push token to the gateway together with the device state needed by the update request. The work requires a connected network, replaces older queued registration work, and retries failures with exponential backoff. The worker logs a failed attempt before it asks the scheduler to retry.

References: app/…/GatewayService.kt• L115-178 app/…/GatewayService.kt• L180-208 app/…/RegistrationWorker.kt• L21-80 app/…/GatewayApi.kt• L48-85

Registration result

The registration result supplies the device ID, access token, login, and optional password. The gateway stores this result as one registration record, while the push token remains separate so a later push-token change can update the same device. A registration success event updates the device view and can start the real-time event connection when push delivery is not being used.

References: app/…/GatewayApi.kt• L142-159 app/…/GatewaySettings.kt• L20-40 app/…/DeviceRegisteredEvent.kt• L5-21 app/…/EventsReceiver.kt• L87-97

Remote command workers

Gateway workers turn cloud commands into durable local work. They require a network connection and retry temporary errors instead of making the cloud command depend on an active screen.

Message pull and cancellation

The message pull worker checks the cloud queue on a periodic schedule and can also be started by a push, a server-sent event, a ping, or a local enqueue signal. The gateway asks for the configured processing order, then maps each cloud message into the local message queue. Local message processing remains responsible for scheduling, SIM choice, sending, and Android delivery results.

When a pulled message is marked as cancelling, the gateway cancels the matching local pending message. If the message is not stored locally, it reports the message and all recipients as cancelled. If a local message already moved out of the pending state, the cancellation is treated as complete because no local cancellation is safe to apply.

References: app/…/PullMessagesWorker.kt• L21-66 app/…/GatewayService.kt• L213-318 app/…/EventsReceiver.kt• L28-63 app/…/MessagesService.kt• L94-124

Settings synchronization

The settings worker reads the cloud settings map and passes it to the central settings service. That service applies changes to the matching local settings groups and alerts the user when a change needs an app restart before it takes effect. The worker runs as periodic network work and retries a failed read or invalid update.

References: app/…/SettingsUpdateWorker.kt• L19-69 app/…/GatewayService.kt• L332-337 app/…/SettingsService.kt• L14-63

Webhook synchronization

The webhook worker reads the cloud webhook list and replaces the local cloud-sourced webhook set. This keeps cloud-managed webhook definitions distinct from local-server definitions and removes cloud webhooks that no longer exist on the server. The worker runs on a daily network schedule and can be started immediately by a webhook-update event.

References: app/…/WebhooksUpdateWorker.kt• L22-78 app/…/GatewayService.kt• L321-329 app/…/WebHooksService.kt• L48-77

Cloud state reporting

Cloud state reporting sends the device's view of message progress back to the cloud. It links local recipient results and message timestamps to the cloud message ID, which lets cloud clients see whether each recipient is pending, processed, sent, delivered, cancelled, or failed.

Delivery state publication

The message service emits a state-change event whenever local processing changes a message or recipient. The gateway event receiver accepts changes from cloud-originated work and starts a state worker for the message. The worker reads the latest local record before sending it, so the report includes the current message state, recipient states, errors, and known state times rather than only the event that triggered the work.

State publication waits for network access and retries transient failures. After its retry limit, the worker fails and the cloud may remain behind the device until another state change or pull causes a later report.

References: app/…/MessageStateChangedEvent.kt• L7-19 app/…/EventsReceiver.kt• L40-51 app/…/SendStateWorker.kt• L23-80 app/…/GatewayApi.kt• L94-100

Device state publication

Device state is published through registration and device-update requests rather than through message state reports. Those requests refresh the cloud record with the current push token and active SIM information, while registration events expose the resulting cloud identity and credentials to the app UI.

References: app/…/GatewayService.kt• L147-194 app/…/GatewayApi.kt• L54-85 app/…/DeviceRegisteredEvent.kt• L5-17

Change checklist

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

  • Keep the external event names aligned across push data, server-sent event data, ExternalEventType, and EventsRouter.
  • Keep message fields used for cloud reports aligned with local recipient states and state timestamps.
  • Preserve the source distinction between cloud webhooks and local-server webhooks during sync and delivery.
  • Keep registration token changes compatible with both push delivery and the server-sent event fallback.
  • Preserve network constraints and retry behavior when changing worker schedules or cloud API calls.

Important failure modes

TriggerConsequenceGuard
Cloud mode is disabled or registration data is missingPull, settings sync, webhook sync, and state reporting have no active cloud sessionGateway service checks its enabled state and gateway service methods stop when registration data is absent
Network access is unavailableBackground synchronization waits or retries instead of completingGateway workers require a connected network and use backoff
A push or server-sent event names an unknown commandThe event is rejected and no local action startsExternal event parsing validates the event enum
A cancellation arrives after local sending has startedThe local message cannot be cancelled safelyMessage cancellation accepts only pending local messages and treats later states as already resolved
A remote settings value has an invalid type or valueSettings import fails and the worker retriesSettings import validates each setting group before it reports success
State publication exhausts its retry attemptsThe cloud can show an older message stateThe state worker reloads the latest local record before each attempt and retries transient failures

Key files

FileWhy to read it
app/…/GatewayService.ktCentral gateway behavior for registration, message import and cancellation, settings reads, webhook reads, and state reports
app/…/GatewayApi.ktCloud request boundary and the message, device, webhook, and state data exchanged with the server
app/…/EventsReceiver.ktLocal reactions that connect gateway events to polling, cancellation, sync, SSE, and state work
app/…/RegistrationWorker.ktNetwork-bound saved-device refresh and retry lifecycle
app/…/PullMessagesWorker.ktPeriodic and event-triggered cloud message intake
app/…/SendStateWorker.ktDelivery-state publication and retry boundary
app/…/SettingsUpdateWorker.ktRemote settings refresh
app/…/WebhooksUpdateWorker.ktCloud webhook replacement and source mapping
app/…/SSEForegroundService.ktReal-time cloud event intake and the pull-on-connect behavior
app/…/PushService.ktPush token registration and push event intake
app/…/EventsRouter.ktMapping from external cloud commands to local application events
app/…/MessagesService.ktLocal queue behavior that receives cloud messages and emits state changes
app/…/SettingsService.ktApplication of cloud settings to local setting groups
app/…/WebHooksService.ktStorage and source-aware replacement of synchronized webhooks