Phone connectivity
Phone connectivity turns a registered Android phone into a managed SMS endpoint for a user account. The endpoint stores a notification address, sends liveness signals, receives SMS and MMS from the mobile network, accepts outbound work, and reports device-side results to the API. Notifications can use Firebase Cloud Messaging (FCM) or a configured HTTPS notification endpoint. The notification service keeps failure guidance specific to the selected transport. See Android phone agent, Phone registration and API keys, Phone heartbeats and availability, and Push delivery and read receipts for focused views.
Connected endpoint model
Registered phone endpoint
A registered phone endpoint represents one user's phone number on an Android device. Its record joins the normalized number and SIM slot to a notification token and the settings that control message rate, retry, expiry, and send behavior. A dual-SIM device can expose one endpoint for each active SIM. api/…/phone.go• L12-34 api/…/phone_service.go• L245-271
The notification token is the address used to reach the Android agent or its configured notification adapter. An opaque token selects FCM. An HTTPS URL selects an HTTP notification transport. Registration creates or updates the endpoint and emits a phone update event. That event lets the availability and key-scope systems learn about the endpoint without coupling those concerns to the registration request. api/…/phone.go• L37-123 api/…/phone_service.go• L111-170 api/…/phone_updated_event.go• L10-21
References: api/…/phone.go• L12-34 api/…/phone.go• L37-123 api/…/phone_service.go• L111-170 api/…/phone_service.go• L245-271 api/…/phone_updated_event.go• L10-21
Device access boundary
The Android agent uses a phone API key as its device credential. The API resolves the key to a user and a set of allowed phone numbers, then checks that scope for heartbeat, message receipt, and message outcome requests. A device can therefore act for its assigned numbers without gaining access to every phone in the account. api/…/phone_api_key_auth_middleware.go• L13-35 api/…/heartbeat_handler.go• L126-134 api/…/message_handler.go• L313-365
This boundary is part of the endpoint contract. If a phone is removed from a key or the key is deleted, the device can no longer prove that it may report work for that endpoint. See Phone registration and API keys for enrollment and key-scope rules. api/…/phone_api_key_handler.go• L181-217 api/…/phone_api_key_service.go• L113-133
References: api/…/phone_api_key_auth_middleware.go• L13-35 api/…/heartbeat_handler.go• L126-134 api/…/message_handler.go• L313-365 api/…/phone_api_key_handler.go• L181-217 api/…/phone_api_key_service.go• L113-133
Endpoint availability
Heartbeat signal
A heartbeat is a time-stamped signal from the Android agent that says an active endpoint can still reach the service. The agent sends the active SIM numbers and whether the device is charging. The API validates the full set of numbers against the device key, then stores a heartbeat for each named number so the latest signal can drive availability checks. android/…/HeartbeatWorker.kt• L11-43 android/…/HttpSmsApiService.kt• L134-159 api/…/heartbeat_handler.go• L95-150
The agent also sends a heartbeat after it receives a high-priority liveness notification. The service sends this probe with high priority and treats a dispatch failure as a warning rather than as a failed API request. This gives the service a way to probe an endpoint that has stopped sending regular signals. See Phone heartbeats and availability for the monitor and timing model. android/…/FirebaseMessagingService.kt• L21-31 android/…/FirebaseMessagingService.kt• L60-81 api/…/phone_notification_service.go• L81-118
References: android/…/HeartbeatWorker.kt• L11-43 android/…/HttpSmsApiService.kt• L134-159 api/…/heartbeat_handler.go• L95-150 android/…/FirebaseMessagingService.kt• L21-31 android/…/FirebaseMessagingService.kt• L60-81 api/…/phone_notification_service.go• L81-118
Availability state
The service derives endpoint availability from recent heartbeat signals. A heartbeat monitor links a phone to its owner and records whether the phone is online. The monitor schedules a check about every 16 minutes. A late signal can trigger a missed-heartbeat event, and continued silence can mark the phone offline. api/…/heartbeat_monitor.go• L9-29 api/…/heartbeat_service.go• L20-23 api/…/heartbeat_service.go• L274-364
When a heartbeat arrives for an endpoint that the monitor marked offline, the service marks it online again and emits an online event. The monitor continues to schedule checks so later silence can repeat the missed and offline path. api/…/heartbeat_service.go• L117-132 api/…/heartbeat_service.go• L274-364
Availability is not proof that an SMS was sent. It only shows that the service has recent evidence that the endpoint can respond. SMS work still depends on push reachability, network access, Android permissions, SIM state, and the result reported by the phone. api/…/phone_heartbeat_online_event.go• L10-22 api/…/phone_heartbeat_offline_event.go• L10-22 android/…/Receiver.kt• L50-72
References: api/…/heartbeat_monitor.go• L9-29 api/…/heartbeat_service.go• L20-23 api/…/heartbeat_service.go• L117-132 api/…/heartbeat_service.go• L274-364 api/…/phone_heartbeat_online_event.go• L10-22 api/…/phone_heartbeat_offline_event.go• L10-22 android/…/Receiver.kt• L50-72
Message work delivery
Outbound work request
For an outbound message, the service creates a phone notification and schedules it using the endpoint's rate and send-time rules. At the scheduled time it resolves the endpoint's notification transport and sends the message through the matching client. The message notification uses normal priority and the endpoint's expiration duration. In the Android FCM path, the notification carries only the message ID as a data message. The push is a wake-up signal. The Android agent then fetches the outstanding message from the API instead of receiving the SMS content in the notification. api/…/phone_notification_service.go• L131-228 api/…/phone_notification_service.go• L244-303 api/…/phone.go• L77-123 android/…/FirebaseMessagingService.kt• L33-40 android/…/HttpSmsApiService.kt• L41-63
The transport client receives the trimmed notification address. A successful client call means that the service accepted the wake-up request. It does not mean that the phone sent the SMS or that the recipient received it. If dispatch fails, the service emits a notification-failed event and marks the phone notification failed. Failure guidance distinguishes an unavailable Android notification path from an unavailable configured HTTP adapter. An HTTP adapter failure points to the adapter URL and availability, while an FCM failure points to the Android agent. api/…/phone_notification_service.go• L191-228 api/…/phone_notification_service.go• L389-433 api/…/phone_notification_service_test.go• L213-279
This sequence shows how an API message becomes device work and then reaches the mobile network.
sequenceDiagram
participant API as SMS API
participant Queue as Work scheduler
participant Transport as Notification transport
participant Phone as Android agent
participant Mobile as Mobile network
API->>Queue: Schedule phone notification
Queue->>Transport: Send message ID to endpoint
Transport-->>Phone: Wake-up notification
Phone->>API: Fetch outstanding message
Phone->>Mobile: Send SMS or MMS
References: api/…/phone_notification_service.go• L131-228 api/…/phone_notification_service.go• L244-303 api/…/phone_notification_service.go• L389-433 api/…/phone.go• L77-123 api/…/phone_notification.go• L9-30 android/…/FirebaseMessagingService.kt• L33-40 api/…/phone_notification_service_test.go• L168-279
Device-side message action
After the agent fetches work, it checks the selected SIM and required message protection settings. It then hands the content to Android's SMS service. Queued work waits for network access when the agent must fetch the message or report its result. android/…/FirebaseMessagingService.kt• L134-180 android/…/SmsManagerService.kt• L43-84
The agent supports single-part and multipart SMS plus MMS attachments. The selected SIM determines which subscription sends the message. The endpoint's SIM setting must therefore match the number that the API scheduled. api/…/message.go• L70-101 android/…/FirebaseMessagingService.kt• L155-186 android/…/FirebaseMessagingService.kt• L204-323 android/…/SmsManagerService.kt• L43-64
Android selects the active subscription that corresponds to the requested SIM. If that subscription is not available, it falls back to the default SMS subscription. A device-side compatibility fallback also handles a platform error when Android resolves an emergency number. android/…/SmsManagerService.kt• L66-120
References: android/…/FirebaseMessagingService.kt• L134-186 android/…/FirebaseMessagingService.kt• L204-323 android/…/SmsManagerService.kt• L43-120 api/…/message.go• L70-101
Device outcomes
Outbound outcome reporting
Android reports SENT, DELIVERED, or FAILED after the SMS service returns a device result. The agent places each report in network-constrained work and sends it to POST /v1/messages/:messageID/events with the event time and an optional failure reason. A temporary loss of network access can therefore delay a report without losing the device result. android/…/SentReceiver.kt• L19-135 android/…/DeliveredReceiver.kt• L18-109 android/…/HttpSmsApiService.kt• L65-75 api/…/message_event_request.go• L12-45
The API accepts the report only for a message and phone number authorized by the device key. It maps the device event to the message lifecycle and emits the matching server event for downstream systems. See Push delivery and read receipts for the delivery and receipt path. api/…/message_handler.go• L265-325 api/…/message_service.go• L281-304 api/…/message_listener.go• L74-141
The outcomes mark different points in the device and network path:
| Device outcome | Meaning |
|---|---|
SENT | Android accepted the message for sending. |
DELIVERED | Android received a delivery result from the mobile network. |
FAILED | Android could not send the message or could not obtain a delivery result. |
References: android/…/SentReceiver.kt• L19-135 android/…/DeliveredReceiver.kt• L18-109 android/…/HttpSmsApiService.kt• L65-75 api/…/message_event_request.go• L12-45 api/…/message_handler.go• L265-325 api/…/message_service.go• L281-304 api/…/message_listener.go• L74-141
Inbound message reporting
The Android agent listens for SMS and MMS broadcasts from the mobile network. It identifies the receiving SIM and phone number, ignores inactive or unauthenticated endpoints, and queues the message for upload when network access is available. android/…/AndroidManifest.xml• L10-23 android/…/AndroidManifest.xml• L72-81 android/…/ReceivedReceiver.kt• L27-61 android/…/ReceivedReceiver.kt• L101-170
The upload calls POST /v1/messages/receive with the sender, receiving number, SIM, timestamp, content, encryption flag, and optional MMS attachments. The API checks the device key's scope, stores the attachments, creates a received-message event, and stores the message for account features such as threads and webhooks. android/…/ReceivedReceiver.kt• L173-227 api/…/message_receive_request.go• L14-73 api/…/message_handler.go• L327-373 api/…/message_service.go• L319-363
References: android/…/AndroidManifest.xml• L10-23 android/…/AndroidManifest.xml• L72-81 android/…/ReceivedReceiver.kt• L27-61 android/…/ReceivedReceiver.kt• L101-170 android/…/ReceivedReceiver.kt• L173-227 api/…/message_receive_request.go• L14-73 api/…/message_handler.go• L327-373 api/…/message_service.go• L319-363
Connectivity lifecycle
Endpoint state sequence
Connectivity is a repeating cycle rather than a one-time registration step. Enrollment supplies the number and notification token. Heartbeats provide evidence that the endpoint remains reachable. Notifications ask the endpoint to act, and device reports close the loop with an outcome. api/…/phone_service.go• L111-170 api/…/heartbeat_service.go• L274-364 api/…/phone_notification_service.go• L244-303 api/…/message_service.go• L281-304
The main state changes are:
- The Android agent enrolls the phone number and stores its notification token.
- The agent sends heartbeats for active SIMs.
- The API schedules and sends outbound message work through the endpoint's notification transport.
- The agent fetches the work and uses the selected SIM to send it.
- The agent reports
SENT,DELIVERED, orFAILED, or uploads an inbound message. - The availability monitor checks the latest heartbeat. It can emit a missed-heartbeat signal and then mark the endpoint offline when signals stop.
This diagram shows the relationship between liveness and message work. A phone can be considered online while a particular message still fails.
flowchart LR
E[Enrollment] --> H[Heartbeat signal]
H --> A[Available endpoint]
A --> P[Notification message work]
P --> D[Device SMS action]
D --> R[Device outcome]
R --> H
H -->|Signals stop| O[Offline endpoint]
O -->|Heartbeat returns| A
References: api/…/phone_service.go• L111-170 api/…/heartbeat_service.go• L274-364 api/…/phone_notification_service.go• L81-118 api/…/phone_notification_service.go• L244-303 api/…/message_service.go• L281-304 api/…/phone.go• L37-123 android/…/ReceivedReceiver.kt• L128-227
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the phone number and SIM identity aligned across enrollment, heartbeat payloads, push scheduling, message retrieval, and device authorization.
- Keep notification address classification aligned with the configured transport clients. Opaque values must select FCM, while notification URLs must remain valid HTTPS endpoints.
- Keep notification address trimming aligned with client requests so adapter URLs and FCM tokens reach the selected client in their usable form.
- Keep message notification priority and expiry separate from high-priority heartbeat probes. A change can delay message work or weaken liveness checks.
- Keep FCM data keys aligned between server push payloads and the Android message receiver. A changed key can silently turn a valid push into no work.
- Keep transport-specific failure guidance aligned with failure events and phone notification status updates. An adapter failure should not tell a user to reinstall the Android agent.
- Keep the phone API key scope checks aligned with every device route that accepts heartbeats, inbound messages, or message outcomes.
- Keep
SENT,DELIVERED, andFAILEDnames aligned across Android broadcasts, HTTP event payloads, server event handling, and message status updates. - Keep queued Android work retryable when network access is absent so device outcomes and received messages are not lost.
- Keep the selected SIM mapping aligned with Android's active subscription order and its default-subscription fallback. A change can route work through the wrong number or stop it when the requested SIM is unavailable.
- Keep phone update and deletion events aligned with heartbeat monitor creation, deletion, and online-state changes.
- Keep the heartbeat check interval and its missed-heartbeat and offline thresholds aligned with the liveness notification and availability events. A mismatch can mark a reachable phone offline or delay a liveness probe.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The phone has no current notification token | The API cannot send outbound work or a liveness probe to the endpoint. | Re-enroll the phone or submit its current notification token through the phone registration route. |
| A notification URL is not HTTPS or has no host | The endpoint cannot select the HTTP notification transport and notification validation fails. | Use an opaque FCM token or a valid HTTPS notification URL. |
| The selected transport has no configured client | The service cannot dispatch the notification and marks the phone notification failed. | Configure the client for every transport used by registered endpoints. |
| The configured HTTP adapter is unavailable | Outbound notification delivery fails and the account receives guidance to check the adapter URL and availability. | Keep the adapter URL correct and make the adapter reachable. |
| An FCM notification cannot reach the Android agent | Outbound notification delivery fails and the account receives Android-agent guidance. | Keep the Android app registered with a current FCM token and able to receive notifications. |
| A device key does not include the phone number named in a request | Heartbeats, inbound messages, or message outcomes are rejected even when the account owns the number. | Keep key associations current and enroll the device with the intended key. |
| Heartbeats stop arriving | The monitor emits a missed-heartbeat signal and can mark the endpoint offline. | Keep the Android agent logged in with an active SIM and permit network and background work. |
| A notification arrives but the phone cannot fetch the outstanding message | The message remains unprocessed or can later expire without a device outcome. | Preserve network-constrained work and keep the API key and server URL valid on the device. |
| The selected SIM is inactive or does not match the endpoint | The Android agent stops outbound work or sends through an unintended subscription. | Keep the endpoint SIM setting and Android SIM activity aligned. |
| Android cannot resolve the requested subscription | The message may use the default SMS subscription instead of the endpoint's intended SIM. | Keep the active subscription order aligned with the endpoint and confirm the selected default when a SIM is unavailable. |
| Android reports a send or delivery result while offline | The account does not see the device outcome until the queued report can reach the API. | Use the agent's network-constrained outcome workers and allow them to retry. |
Key files
| File | Why to read it |
|---|---|
| api/…/phone.go | Defines the business state of a registered phone endpoint and the notification transport encoded by its token. |
| api/…/phone_service.go | Shows endpoint creation, update, deletion, and lifecycle events. |
| api/…/heartbeat_service.go | Shows how recent heartbeats become online, missed, and offline state. |
| api/…/heartbeat_monitor.go | Defines the persisted availability state for an endpoint. |
| api/…/phone_notification_service.go | Shows how message work and liveness probes select a transport, dispatch notifications, and record sent or failed notification state. |
| api/…/phone_notification_service_test.go | Guards transport selection, notification payload behavior, and transport-specific failure guidance. |
| api/…/container.go | Shows the notification clients available for FCM and HTTPS delivery. |
| android/…/FirebaseMessagingService.kt | Shows how the Android agent turns pushes into heartbeat work or message work. |
| android/…/SmsManagerService.kt | Shows how outbound work selects an Android subscription and sends SMS or MMS. |
| android/…/ReceivedReceiver.kt | Shows how inbound SMS and MMS become API requests. |
| android/…/SentReceiver.kt | Shows how Android send results become queued API events. |
| android/…/DeliveredReceiver.kt | Shows how Android delivery results become queued API events. |
| android/…/HttpSmsApiService.kt | Defines the device-to-API contracts for work, heartbeats, receipt, and outcomes. |
| api/…/message_handler.go | Defines the server boundary for device message events and inbound messages. |
| api/…/message_service.go | Connects device reports to message state and downstream events. |
| api/…/phone_api_key_auth_middleware.go | Defines the device credential context used by endpoint checks. |