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 / android-phone-agent

Android phone agent

The Android phone agent turns a real Android phone into an HTTPSMS endpoint. It links a phone number and SIM slot to an API key, receives wake-up work through Firebase Cloud Messaging (FCM) or a configured HTTPS notification adapter, uses the phone radio to send or receive SMS and MMS, and reports the result to the HTTPSMS API. Its local state also controls which SIM slots can take part in messaging and availability checks.

The agent is a child of Phone connectivity. Read Phone registration and API keys for enrollment rules, Phone heartbeats and availability for liveness, and Push delivery and read receipts for the wider push path.

Agent role

The Android app joins four boundaries into one phone endpoint: Android telephony, Google push delivery, an optional HTTPS notification adapter, and the HTTPSMS API. The app does not hold the message queue as its main store. A wake-up event identifies work on the API, and the phone fetches that work when it can act on it.

The main responsibilities are:

  • keep the phone and each active SIM slot known to the API;
  • turn API message work into SMS or MMS radio actions;
  • turn Android SMS, MMS, and call events into API records;
  • send delivery results and periodic availability signals;
  • preserve the user’s phone, SIM, permission, and encryption choices locally.

Authentication and enrollment

Login and phone identity

Login binds the app to an API key, a server URL, and one phone number for each active SIM. The app checks phone numbers in international E.164 form and requires an HTTPS server URL. It obtains an FCM token from Google Play services, then sends that token with the phone number and SIM name to PUT /v1/phones/fcm-token. A successful response supplies the user identity used by the app to confirm that the local setup is complete.

The API key travels in the X-API-Key header on later API calls. The phone API-key path accepts keys with the pk_ prefix and uses the key’s phone access when it checks message and event requests. This keeps a phone key limited to the phone numbers linked to it.

The app can scan the API key from a QR code. It can also try to read the phone numbers from Android. The user can correct the detected values before login. A logged-in state requires both a saved API key and a saved SIM1 phone number.

References: android/…/LoginViewModel.kt• L27-39 android/…/LoginViewModel.kt• L125-221 android/…/HttpSmsApiService.kt• L260-295 api/…/phone_api_key_auth_middleware.go• L13-35 api/…/phone_handler.go• L173-210

Token refresh

The FCM token identifies the app instance that should receive message work. Android can replace this token, so the app saves a new token and uploads it for SIM1 and SIM2 when the token changes. The main screen also refreshes the token registration after about one day. This keeps push delivery tied to the current app instance.

References: android/…/FirebaseMessagingService.kt• L43-57 android/…/FirebaseMessagingService.kt• L104-120 android/…/MainActivity.kt• L159-203 api/…/phone_fcm_token_request.go• L12-39

Outbound message flow

Wake-up notification transport

The HTTPSMS API schedules phone work and sends a wake-up notification when that work is ready. The phone’s notification target selects the transport: a normal FCM token uses Firebase, while an HTTPS notification URL uses a configured adapter that accepts the same FCM-compatible message. In both cases, the notification carries the message ID that lets the Android app fetch the message from the API.

The HTTPS adapter receives a short-lived bearer token and a signed request. A successful response confirms wake-up delivery only; it does not confirm that the phone sent the SMS. The API records notification sent or failed events separately from the later radio result.

References: api/…/phone.go• L37-104 api/…/phone_notification_service.go• L131-227 api/…/http_notification_sender.go• L31-123 api/…/http_notification_sender.go• L132-220 api/…/phone_notification_listener.go• L35-42 api/…/phone_notification_service_test.go• L168-310

Push-to-radio delivery

An outbound message is first stored by the HTTPSMS API. The API then sends a wake-up data message through FCM or the configured HTTPS adapter. After the notification reaches the app, the Android agent uses the message ID to fetch the still-outstanding message. It checks the local login and SIM state before it uses the Android SMS or MMS service.

This diagram shows how API work becomes a phone action and then returns as a state event.

flowchart LR
    A[HTTPSMS API message] --> B{Wake-up transport}
    B --> C[FCM]
    B --> D[HTTPS notification adapter]
    C --> E[Android work request]
    D --> E
    E --> F[Fetch outstanding message]
    F --> G{SIM active and content usable?}
    G -- No --> H[Report FAILED]
    G -- Yes --> I[Send SMS or MMS]
    I --> J[Android sent or delivered result]
    J --> K[Report SENT, DELIVERED, or FAILED]

The app decrypts an encrypted outbound message before it sends it. Plain text is split into Android SMS parts when needed. MMS attachments are fetched from their API URLs and sent with the message. The selected SIM maps to an active Android subscription, with a device default used when that subscription cannot be resolved. Temporary attachment and MMS build files are removed after dispatch. This keeps the phone radio action tied to the SIM named by the API without leaving message files in the local cache.

References: android/…/FirebaseMessagingService.kt• L21-40 android/…/FirebaseMessagingService.kt• L134-180 android/…/FirebaseMessagingService.kt• L204-323 android/…/SmsManagerService.kt• L43-120 api/…/message_handler.go• L174-218 api/…/message_service.go• L64-106 api/…/phone_notification_service.go• L147-188

Send state events

Android gives the app separate results for accepting a send and for delivery to the contact’s network. The app maps these results to SENT, DELIVERED, or FAILED events. It sends each event to POST /v1/messages/{messageID}/events with a UTC timestamp. A failure includes a reason such as no service or radio off.

The app registers the sent and delivered receivers when it starts a send. It moves the API call into network-constrained background work. A temporary network failure causes the work to retry. Multipart SMS segments use derived IDs, but only the complete message ID can update the API. This prevents one event per segment from changing the message state more than once.

References: android/…/Receiver.kt• L10-72 android/…/SentReceiver.kt• L19-31 android/…/SentReceiver.kt• L54-135 android/…/DeliveredReceiver.kt• L18-109 api/…/message_handler.go• L281-325 api/…/message_event_request.go• L12-45

Inbound message flow

SMS and MMS reception

Android sends SMS and MMS broadcasts to the app’s inbound receiver. The receiver joins message parts, finds the SIM slot, and uses the saved phone number as the destination. It ignores inbound traffic when that SIM’s incoming setting is off, when the SIM is inactive, or when the app is not logged in.

The receiver places the message in network-constrained work instead of making a long API call inside the broadcast callback. The worker sends from, to, sim, content, encrypted, and the event timestamp to POST /v1/messages/receive. For MMS, it also sends each attachment as base64 content with its name and MIME type. Temporary attachment files are removed after the API request.

References: android/…/AndroidManifest.xml• L72-81 android/…/ReceivedReceiver.kt• L27-61 android/…/ReceivedReceiver.kt• L64-171 android/…/ReceivedReceiver.kt• L173-227 api/…/message_handler.go• L327-374 api/…/message_receive_request.go• L14-73

Received-message encryption

The app can encrypt received message content before it leaves the phone. The encrypted flag tells the API and later readers that the content is protected. The setting only takes effect when the user has supplied an encryption key. The same key must be present on the app when it handles encrypted outbound content.

References: android/…/ReceivedReceiver.kt• L128-158 android/…/Settings.kt• L178-213 android/…/Encrypter.kt• L11-43 android/…/FirebaseMessagingService.kt• L154-167

Missed call events

The phone state receiver turns an incoming call that ends without an answer into an optional HTTPSMS event. It waits for Android to update the call log, confirms that the latest call is missed, maps the call to a SIM slot, and sends the event to POST /v1/messages/calls/missed. The feature is separate from SMS receipt and needs the call-event setting plus call-log permission.

References: android/…/AndroidManifest.xml• L10-14 android/…/AndroidManifest.xml• L83-87 android/…/PhoneStateReceiver.kt• L29-89 android/…/PhoneStateReceiver.kt• L111-172 api/…/message_handler.go• L461-504 api/…/message_call_missed_request.go• L13-41

Background execution

Push and queued work

The Firebase messaging service is the entry point for API-triggered work after a wake-up notification reaches the app. A normal data push carries a message ID and becomes a one-time work request that waits for network access. A heartbeat push does not fetch a message. It asks the app to send a fresh availability signal from a short background task instead.

This split keeps message delivery in Android’s work scheduler, where the app can wait for connectivity and retry API work without an open screen. Heartbeat handling uses a separate background thread because it sends the small availability update directly.

References: android/…/FirebaseMessagingService.kt• L21-40 android/…/FirebaseMessagingService.kt• L60-101 android/…/ReceivedReceiver.kt• L146-170 android/…/SentReceiver.kt• L59-78 api/…/phone_notification_service.go• L81-118

Foreground listener service

The sticky notification service keeps an active phone visible to Android as a foreground listener. Its notification tells the user that the app listens for sent and received SMS messages. The service asks Android to restart it after a process kill, but it starts only when at least one SIM is active.

The boot receiver starts this service after device startup when a SIM remains active. This restores the listener state without requiring the user to open the app.

References: android/…/StickyNotificationService.kt• L12-35 android/…/StickyNotificationService.kt• L38-73 android/…/BootReceiver.kt• L12-29 android/…/AndroidManifest.xml• L56-70 android/…/AndroidManifest.xml• L89-96

Heartbeat scheduling

The app sends heartbeats on a periodic schedule when the device has network access. A heartbeat lists active phone numbers and reports whether the device is charging. The app also accepts a heartbeat request through FCM and offers a manual send action in the main screen. The server uses these records to judge phone availability; see Phone heartbeats and availability.

References: android/…/MainActivity.kt• L74-79 android/…/MainActivity.kt• L128-145 android/…/HeartbeatWorker.kt• L11-43 android/…/HttpSmsApiService.kt• L134-159 api/…/heartbeat_store_request.go• L10-43

Local configuration

SIM and feature state

The app stores its operating state in Android preferences. It keeps the API key, server URL, FCM token, user ID, phone number for each SIM, activity flags, incoming-message flags, call-event flags, heartbeat times, and encryption settings. SIM1 is the default slot. SIM2 participates only when the phone has a second active subscription and a saved SIM2 number.

Outgoing activity and incoming activity are separate controls. A user can keep a SIM available for one direction while disabling the other. Logging out clears identity and encryption data, resets the message controls, and disables missed-call events.

References: android/…/Settings.kt• L12-29 android/…/Settings.kt• L70-72 android/…/Settings.kt• L113-149 android/…/Settings.kt• L225-277 android/…/SettingsViewModel.kt• L25-43 android/…/SettingsViewModel.kt• L98-112

Permissions and power access

The Android manifest declares network, SMS, phone-state, boot, notification, wake-lock, and foreground-service access. The app requests the SMS permissions at run time. It requests notification permission on newer Android versions. It requests call-log and phone-state access when missed-call events are enabled.

Battery optimization can delay background work. The main screen exposes Android’s setting to let the user exempt the app. The foreground notification and network constraints support background operation, but they do not replace the user’s permission and power settings.

References: android/…/AndroidManifest.xml• L6-23 android/…/LoginActivity.kt• L121-146 android/…/MainActivity.kt• L81-126 android/…/MainViewModel.kt• L61-83

HTTPSMS API boundary

Device-facing operations

The Android client uses one small API surface. Every call includes the saved API key and the app version. The default server is https://api.httpsms.com, but login can save another HTTPS server URL.

PurposeOperationDevice meaning
Register push addressPUT /v1/phones/fcm-tokenLink an FCM token to a phone number and SIM slot.
Fetch outbound workGET /v1/messages/outstanding?message_id=...Get the message named by a push event.
Report outbound resultPOST /v1/messages/{messageID}/eventsStore sent, delivered, or failed state.
Store inbound messagePOST /v1/messages/receiveAdd SMS or MMS received by the phone.
Store missed callPOST /v1/messages/calls/missedAdd an optional missed-call record.
Store availabilityPOST /v1/heartbeatsRecord active phone numbers and charging state.

The API validates the phone key and then checks the requested phone number for phone-scoped actions. It can reject an otherwise valid request when the key is not linked to that phone, when the message is no longer outstanding, or when account limits apply.

References: android/…/HttpSmsApiService.kt• L22-45 android/…/HttpSmsApiService.kt• L65-159 api/…/message_handler.go• L61-67 api/…/phone_handler.go• L48-51 api/…/phone_api_key_auth_middleware.go• L21-34

Change checklist

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

  • Keep the API route, auth header, and payload shape aligned between HttpSmsApiService.kt and the API request types.
  • Keep SIM names and phone-number ownership aligned across login, FCM token registration, heartbeats, inbound messages, and outbound message selection.
  • Keep message event names and multipart message-ID handling aligned between Android receivers and PostEvent.
  • Keep FCM data keys aligned between the push sender and FirebaseMessagingService.
  • Keep notification transport selection aligned with the phone token format. An FCM token and an HTTPS adapter URL must reach the matching sender.
  • Keep HTTPS adapter request signing, endpoint validation, FCM-compatible payloads, and success or failure handling aligned between the API sender and the adapter.
  • Keep attachment size, temporary-file handling, and base64 attachment fields aligned for both MMS directions.
  • Keep permission changes aligned with the manifest, run-time requests, and the settings that enable call or message features.
  • Keep the Android build toolchain and Gradle wrapper versions aligned when changing project-level build settings. The project resolves its Java toolchain through Foojay and builds the :app module with Gradle 9.7.

Important failure modes

TriggerConsequenceGuard
API key is missing, invalid, or not linked to the phone numberLogin or a device API call is rejected.Validate the key during FCM token registration and use the phone-scoped key for device calls.
FCM token is missing or staleThe phone does not receive new outbound work through the standard push path.Require a token at login and refresh its registration after token changes or the daily refresh interval.
Configured HTTPS adapter URL is invalid or its sender is unavailableThe API cannot wake the phone through the configured notification path and marks the notification failed.Accept only HTTPS endpoints with a host and configure the matching notification sender.
HTTPS adapter returns a terminal errorThe wake-up is not accepted and the API reports adapter-specific notification guidance.Return a successful 2xx response only after accepting the wake-up request.
SMS permissions are deniedAndroid cannot send or receive SMS.Request runtime permissions and show the SMS permission state in the main screen.
A SIM is inactive or its incoming setting is offOutbound work stops or inbound broadcasts are ignored for that SIM.Check the per-SIM settings before radio work and before enqueueing received messages.
The selected SIM has no matching active subscriptionAndroid may use its default SMS subscription instead of the requested SIM.Keep saved SIM settings aligned with active subscriptions and validate dual-SIM state before use.
Device has no network when an event is emittedThe API state is delayed.Use network-constrained work and retry failed event, receive, and message-fetch calls.
Encrypted content has no matching local keyThe message cannot be safely sent or received as configured.Stop the send or use encryption only when a key is present.
MMS attachment is too large or cannot be downloadedThe MMS is marked failed and is not sent.Enforce the client attachment limit before composing the MMS.
Battery optimization blocks background executionPush, heartbeat, or receiver follow-up work can arrive late.Keep the foreground listener active and let the user disable battery optimization.

Key files

FileWhy to read it
android/…/AndroidManifest.xmlShows the phone permissions, exported receivers, foreground services, and push entry point.
android/…/LoginActivity.ktShows the user-facing permission and login entry flow.
android/…/LoginViewModel.ktShows phone validation, FCM registration, and saved login state.
android/…/FirebaseMessagingService.ktShows how push data becomes outbound SMS or MMS work and how heartbeat pushes are handled.
android/…/SmsManagerService.ktShows how the selected SIM maps to an Android subscription and radio send operation.
android/…/ReceivedReceiver.ktShows how Android SMS and MMS broadcasts become API receive requests.
android/…/SentReceiver.kt and android/…/DeliveredReceiver.ktShow how radio results become API message events.
android/…/HttpSmsApiService.ktDefines the device-facing HTTPSMS API boundary.
android/…/Settings.ktDefines the local identity, SIM, feature, heartbeat, and encryption state.
android/…/StickyNotificationService.ktShows the foreground listener that supports background operation.
android/…/BootReceiver.kt and android/…/PhoneStateReceiver.ktShow startup recovery and optional missed-call reporting.
api/…/phone.goDefines how a phone’s notification target selects FCM or an HTTPS adapter.
api/…/phone_notification_service.goCoordinates wake-up delivery, message notification status, and notification events for a phone.
api/…/http_notification_sender.goShows the HTTPS adapter boundary for FCM-compatible wake-up notifications.
api/…/phone_notification_service_test.goShows the expected behavior for FCM and HTTPS notification transports and their failure guidance.
android/settings.gradle.kts, android/gradle/wrapper/gradle-wrapper.properties, and android/gradle/gradle-daemon-jvm.propertiesShow the Android project boundary and the Gradle and Java toolchain used to build the app.
api/…/message_handler.goDefines the API operations for outstanding work, message events, received messages, and missed calls.
api/…/phone_handler.goDefines the API operation that links an FCM token to a phone.