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 / webhook-delivery

Webhook delivery

Webhook delivery sends selected SMS, MMS, system, and application events to registered external URLs. The system separates event creation from network delivery so that a short network outage does not block message processing and each delivery has a trackable outcome.

Registration

Webhook configuration

The webhook configuration links a URL to one event type and one source. A local API or a synchronized cloud or gateway source can create and replace registrations, while deletion removes the link for that source. The service accepts HTTPS URLs, plus HTTP URLs for localhost or insecure builds.

The public API exposes list, register, and delete operations through /webhooks. A registration needs an event and a URL. If the request has no ID, the system creates one; if it has an existing ID, the registration is replaced.

References: app/…/WebHooksService.kt• L48-123 app/…/WebHook.kt• L8-15 app/…/swagger.json• L1325-1359 app/…/swagger.json• L3232-3438

Registration review

Incoming SMS registrations receive a user notice because they can expose private messages to an external system. A periodic review repeats that notice when incoming SMS webhooks remain configured. This review is separate from delivery and does not change the registration.

References: app/…/WebHooksService.kt• L105-110 app/…/ReviewWebhooksWorker.kt• L17-63 app/…/webhooks_preferences.xml• L25-29

Event matching

Event sources

The event receiver connects message state changes and device events to the webhook service. Incoming SMS and MMS processing can emit message events, outgoing SMS state changes emit sent, delivered, failed, or cancelled events, device pings emit health events, and application startup emits a SIM-card summary.

For a received event, the service selects only registrations with the same event type. It skips a registration when its source is disabled or its source has no device ID. This keeps local, cloud, and gateway registrations tied to the system that owns them.

References: app/…/EventsReceiver.kt• L17-97 app/…/WebHooksService.kt• L126-151 app/…/OrchestratorService.kt• L61-66 app/…/ReceiverService.kt• L100-116

Event payloads

Each matched registration receives an envelope with a unique event ID, the registration ID, the event name, the device ID, and an event-specific payload. The payload can describe one SMS or MMS, a batch of messages, a system health result, or application startup data.

The event names and payload shapes form the contract for external consumers. See Webhook event payloads for the event taxonomy and the fields of each payload family.

References: app/…/WebHookEventDTO.kt• L3-9 app/…/WebHookEvent.kt• L5-53 app/…/ReceiverService.kt• L127-204 app/…/ReceiverService.kt• L207-247 app/…/swagger.json• L1384-1450

Delivery pipeline

Queue persistence

The delivery path stores each event before it makes the HTTP request. The queue record keeps the destination URL, a payload reference, creation time, next attempt time, retry count, last error, and current status. The payload is kept in app storage and the queue stores a reference to it, with support for older rows that contain the payload inline.

This split lets the queue hold large payloads without making the queue row the payload store. If queue insertion fails, the system uses a temporary storage fallback and schedules the older direct worker path.

References: app/…/WebhookQueueRepository.kt• L17-43 app/…/WebhookQueueEntity.kt• L8-43 app/…/WebhookPayloadStorage.kt• L6-46 app/…/SendWebhookWorker.kt• L212-291

Delivery worker

One unique queue worker reads due records and sends them as JSON POST requests. It processes records in small groups, marks each record as processing before the request, and marks it complete only after a 2xx response. The worker can require an active network connection, and it schedules itself for the next due retry when no record is ready.

The worker also recovers records that stayed in processing for too long. Completed and permanently failed records are later removed with their stored payloads after the retention period.

This diagram shows the normal path from an application event to a recorded delivery result.

flowchart LR
    A[Application or message event] --> B[Match registered webhooks]
    B --> C[Build event envelope]
    C --> D[Persist queue record and payload]
    D --> E[Queue worker selects due record]
    E --> F[POST JSON to destination]
    F -->|2xx| G[Completed]
    F -->|error or non-2xx| H[Failed or retry]
    H --> E
    H --> I[Permanently failed]

References: app/…/WebhookQueueProcessorWorker.kt• L77-115 app/…/WebhookQueueProcessorWorker.kt• L118-212 app/…/WebhookQueueProcessorWorker.kt• L218-324 app/…/WebhookQueueRepository.kt• L67-137

HTTP delivery

The worker sends the stored JSON envelope to the registered URL with an application JSON content type. A 2xx response is success. A non-2xx response, a network error, a missing payload, or invalid JSON causes the queue path to treat the attempt as unsuccessful and pass it to retry handling.

The older direct worker remains as a fallback when the persistent queue cannot accept an event. It uses the same destination and JSON request model, but its retry outcome is held by the work scheduler rather than by the persistent queue record.

References: app/…/WebhookQueueProcessorWorker.kt• L326-404 app/…/SendWebhookWorker.kt• L61-165

Retry policy

Retry scheduling

Failed queue records keep their payload and error until the next attempt or a terminal result. The configured retry count sets the maximum number of retries. The delay grows from a five-second base with exponential backoff, so repeated failures do not cause a tight request loop.

When the retry limit is reached, the record becomes permanently failed and its stored payload is deleted. If retry scheduling itself fails, the worker also moves the record to that terminal state.

References: app/…/WebhookQueueRepository.kt• L82-109 app/…/WebhookQueueRepository.kt• L111-147 app/…/WebhookQueueProcessorWorker.kt• L406-430 app/…/WebhooksSettings.kt• L12-24

Delivery settings

Webhook settings control whether work waits for a network connection, how many retries are allowed, and which key signs the payload. The default behavior requires a connection and allows 15 retries. Retry values must be at least one.

References: app/…/WebhooksSettings.kt• L9-30 app/…/webhooks_preferences.xml• L4-23 app/…/swagger.json• L1208-1224

Payload signing

Signature headers

When a signing key is available, the HTTP client adds a timestamp and a hexadecimal HMAC-SHA256 signature. The signature covers the exact text payload followed by the timestamp, so a receiver can check both message integrity and the freshness value.

The default headers are X-Signature and X-Timestamp. The signing key is a local setting and the API contract states that it must not be used with Cloud Server. Consumers must use the same key and signature input rule to verify requests.

References: app/…/PayloadSingingPlugin.kt• L10-60 app/…/WebhookQueueProcessorWorker.kt• L447-464 app/…/swagger.json• L1214-1221

Delivery status

Queue states

The queue records a delivery state that shows where an event is in its life cycle. Pending and processing states represent active work. Failed means the system will try again when its next attempt time is due. Completed and permanently failed are terminal states.

The queue also exposes aggregate counts for these states and records the last error for failed attempts. Logs add the destination, event or queue ID, response status, and retry count needed to review a delivery without changing the event payload.

StateMeaning
pendingWaiting for its first attempt or for recovery.
processingClaimed by a delivery worker.
failedThe last attempt failed and a retry is scheduled.
completedThe destination returned a 2xx response.
permanently_failedNo more retries are allowed or retry scheduling failed.

References: app/…/WebhookStatus.kt• L8-37 app/…/WebhookQueueDao.kt• L24-33 app/…/WebhookQueueDao.kt• L65-128 app/…/WebhookQueueProcessorWorker.kt• L342-403

Queue review

The queue review view helps an operator inspect recent delivery records without loading the full queue. It shows up to 100 entries at a time, supports an all-status view or one status filter, and provides aggregate counts for the full queue so recent records and overall health can be read together.

The review state follows the repository data. A status change refreshes the bounded list, while the statistics summary refreshes from the queue as a separate operation. This keeps a large queue from becoming an unbounded screen query.

References: app/…/WebhookStatus.kt• L8-37 app/…/WebhookQueueDao.kt• L24-33 app/…/WebhookQueueDao.kt• L65-139 app/…/WebhookQueueViewModel.kt• L14-52

Change checklist

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

  • Keep the registered event names aligned with the event envelope and the public webhook schema. A name mismatch silently prevents a registration from matching an emitted event.
  • Keep payload class changes aligned with the child payload contract and the JSON schema. Consumers depend on the event-specific fields inside the shared envelope.
  • Keep queue status transitions aligned with retry counts, next-attempt times, and payload cleanup. A transition that skips cleanup can leave payload files without a queue record.
  • Keep signing-key, timestamp, and signature-input changes aligned with receiver verification rules. A header change alone can make every signed request fail verification.
  • Keep source enablement and device ID rules aligned with webhook synchronization. A valid registration can still be skipped when its owning source is disabled or not identified.
  • Keep queue review filters and status counts aligned with the status values used by persistence. A new status that is not included in the review queries can disappear from the operator view.

Important failure modes

TriggerConsequenceGuard
A destination returns a non-2xx response or the network request fails.The queue keeps the event as failed and schedules another attempt.The retry limit and backoff move the event to permanently_failed when retries end.
A queue payload reference points to a missing file.The worker cannot send the event and treats the attempt as a failure.Payload storage and queue insertion must remain an atomic-looking pair; the queue fallback covers insertion errors.
A worker stops while a record is processing.The record can remain unavailable until recovery.The next queue run resets processing records older than five minutes to pending.
A registration uses a disallowed URL scheme.Registration is rejected before it can receive events.Use HTTPS, or use the allowed localhost or insecure-build exception.
A source is disabled or has no device ID.Matching registrations are skipped without a request.Enable the owning source and keep its device identity available before relying on delivery.

Key files

FileWhy to read it
app/…/WebHooksService.ktMain boundary for registration, event matching, source checks, and enqueue requests.
app/…/WebHookEvent.ktDefines the event names that bind registrations to emitted payloads.
app/…/WebHookEventDTO.ktDefines the shared envelope sent to external consumers.
app/…/WebhookQueueRepository.ktDescribes persistence, retry decisions, cleanup, and stuck-record recovery.
app/…/WebhookQueueViewModel.ktConnects queue records and full-queue statistics to bounded status review.
app/…/WebhookQueueProcessorWorker.ktShows the worker life cycle from due-record selection to HTTP result and status update.
app/…/PayloadSingingPlugin.ktDefines optional request signing and its verification inputs.
app/…/ReviewWebhooksWorker.ktShows the separate safety review for incoming SMS registrations.
app/…/swagger.jsonDefines the public registration, settings, event, and payload contracts.