Turning an Android Phone into a Private SMS Gateway Using Kotlin and Firebase
Why pay for an external SMS gateway when you already have a smartphone with a SIM card?
SMS is still an important part of many applications. OTP verification, customer notifications, booking confirmations, transaction alerts, and automated reminders all depend on SMS communication.
Traditionally, developers integrate third-party SMS gateway providers to handle these requirements. While convenient, these services can introduce recurring costs, provider dependencies, API limitations, and additional infrastructure.
I wanted to explore a different approach:
What if an ordinary Android smartphone could act as an SMS gateway, allowing backend applications to send SMS messages through its physical SIM card?
That idea led to the development of Android Private SMS Gateway.
GitHub Repository: https://github.com/Dhanasekaran-zha/sms-gateway
The Problem: SMS Infrastructure Can Be Expensive
When building applications that require SMS functionality, developers usually depend on external SMS service providers.
Although these platforms simplify SMS delivery, they are not always ideal for personal projects, development environments, or small-scale experiments.
Some common challenges include:
- Recurring costs for SMS delivery.
- Dependency on external SMS providers.
- Account registration and configuration requirements.
- Limited flexibility over the SMS delivery infrastructure.
- Additional backend integration and maintenance.
I wanted to experiment with an alternative that uses an existing Android device and its SIM card to handle SMS transmission.
The goal wasn't to replace enterprise SMS infrastructure, but to build a self-hosted solution for controlled and personal use cases.
The Idea: Your Android Phone as an SMS Gateway
The concept is simple.
Instead of sending SMS directly through an external provider, the backend communicates with an Android application using Firebase Cloud Messaging (FCM).
The Android application receives the request and sends the SMS using the device's cellular network and SIM card.
The overall workflow looks like this:
Backend Application
|
|
Firebase FCM
|
|
Android Device
|
SMS Manager
|
SIM Card
|
Mobile Network
|
Recipient PhoneThis architecture allows an existing backend application to trigger SMS delivery without directly integrating with a commercial SMS gateway API.
Technology Stack
I used modern Android development technologies to build the application.
| Technology | Purpose |
|---|---|
| Kotlin | Primary development language |
| Jetpack Compose | Modern UI development |
| Material 3 | UI components and design |
| MVVM | Application architecture |
| Room Database | Local SMS job tracking |
| Hilt | Dependency injection |
| WorkManager | Persistent background tasks |
| Firebase Cloud Messaging | Backend-to-device communication |
| Android SmsManager | Sending SMS through the SIM |
| Retrofit | Backend communication |
The application follows a modular architecture to separate UI, business logic, data management, and Android platform-specific functionality.
How Does It Work?
1. Backend Sends an SMS Request
The backend sends a Firebase Cloud Messaging data message to the registered Android device.
The request contains three important fields:
job_id: A unique identifier for the SMS request.recipient: The destination phone number.message_body: The actual SMS content.
Example payload:
{
"message": {
"token": "DEVICE_FCM_TOKEN",
"data": {
"job_id": "job-12345",
"recipient": "+919876543210",
"message_body": "Your verification code is 482910."
},
"android": {
"priority": "high"
}
}
}Using a data-only FCM message allows the Android application to process the request through its Firebase messaging service without requiring the user to tap a notification.
2. Android Receives the Request
The Android application receives the incoming FCM message and processes the SMS job.
Before attempting transmission, the application validates the request and records its state.
This is important because background SMS processing should not depend entirely on whether the application UI is currently open.
3. Send SMS Using the Physical SIM
Once the request is validated, Android's SmsManager API handles SMS transmission.
The application supports:
- Sending regular SMS messages.
- Sending multipart SMS messages.
- Selecting a SIM slot on dual-SIM devices.
- Tracking submission and delivery results.
This makes the physical SIM card the actual SMS transmission channel.
4. Track Every SMS Job
One of the important engineering decisions in this project was to avoid treating SMS sending as a simple function call.
An SMS request can have several outcomes, and the application needs to understand exactly what happened.
I implemented a state-based job tracking approach using Room.
The job lifecycle follows these states:
QUEUED
|
FETCHED
|
SUBMITTING
|
+------ SUBMITTED
| |
| DELIVERED
|
+------ FAILED
|
+------ SUBMISSION_UNKNOWNThe distinction between SUBMITTED and DELIVERED is particularly important.
SUBMITTED means the device's SMS stack accepted the message and handed it to the carrier.
DELIVERED means an explicit delivery report was received from the carrier.
These two states should never be treated as equivalent.
Preventing Duplicate SMS Messages
Imagine that a backend sends an SMS request, but the device experiences a timeout.
Should the application immediately retry?
Not necessarily.
The original SMS might already have been submitted to the carrier even though the application didn't receive a clear confirmation.
Blindly retrying could result in duplicate SMS messages.
To address this, the project uses:
- Unique job identifiers.
- WorkManager unique work.
- Room-based state tracking.
- State transition validation.
- A
SUBMISSION_UNKNOWNstate for ambiguous outcomes.
This approach helps prevent accidental duplicate dispatches and makes uncertain outcomes explicit rather than hiding them.
It does not promise exactly-once delivery, which cannot be guaranteed by the underlying SMS network.
Handling Background Execution and Offline Scenarios
A gateway application needs to work beyond the normal foreground app lifecycle.
The project uses Firebase Cloud Messaging, partial WakeLocks, and WorkManager to support background processing.
It also includes battery optimization guidance for devices that restrict background execution.
Another important consideration is reporting SMS delivery status back to the backend.
For example, if the device receives a delivery report but has temporarily lost internet connectivity, the status update should not simply disappear.
The application can persist the update and schedule a WorkManager task with a network connectivity constraint.
WorkManager retries the status synchronization using exponential backoff.
This helps maintain consistency between the Android device and the backend.
Smart SMS Segmentation
SMS messages have length limitations that depend on the character encoding.
A message containing regular GSM 7-bit characters has different capacity limits from a message containing Unicode characters.
The application accounts for:
- GSM 7-bit encoding.
- UCS-2 Unicode encoding.
- Single-part SMS messages.
- Multipart SMS messages.
Typical limits are:
| Encoding | Single SMS | Multipart SMS segment |
|---|---|---|
| GSM 7-bit | 160 characters | 153 characters |
| UCS-2 | 70 characters | 67 characters |
This allows longer messages to be divided into multiple SMS segments.
Privacy and Security Considerations
Since the application handles phone numbers and message content, privacy is an important consideration.
The project includes:
- Runtime SMS permission handling.
- Masked phone numbers in the interface.
- Avoidance of plain-text OTP logging.
- No bypassing of Android platform security.
- Local job tracking and audit history.
The backend integration also needs proper authentication, HTTPS communication, and secure management of Firebase credentials and device secrets before production use.
Where Can This Be Useful?
This architecture can be useful for controlled environments and personal automation projects.
Some potential applications include:
- Development and testing: Trigger SMS messages from a backend while testing an application.
- Personal notification systems: Send alerts through an existing SIM card.
- Small automation projects: Integrate SMS functionality into custom backend workflows.
- Prototype applications: Experiment with SMS integration without initially subscribing to a commercial SMS gateway.
- Local infrastructure: Build a self-hosted SMS relay for authorized use cases.
Important Limitations
This project is an experiment in building a private SMS gateway, not a replacement for regulated commercial messaging infrastructure.
There are some important limitations:
- The Android device must be powered on and available.
- FCM delivery depends on network connectivity and platform restrictions.
- Battery optimization and manufacturer-specific background restrictions can affect reliability.
- The SIM card must have an active SMS plan.
- Carrier delivery reports are not universally guaranteed.
- SMS delivery cannot be guaranteed exactly once.
For users in India, commercial, transactional, and promotional SMS traffic is subject to TRAI and DLT requirements. Using a personal SIM for commercial bulk SMS or OTP delivery may violate operator terms and result in restrictions or SIM blocking.
Always verify the applicable regulations and operator conditions before using this approach beyond personal experimentation.
Final Thoughts
This project started with a simple question: can an ordinary Android phone become a programmable SMS gateway?
Exploring that question involved much more than calling Android's SmsManager API.
I had to consider background execution, Firebase communication, duplicate prevention, database state management, multipart messages, delivery callbacks, and offline synchronization.
The interesting part was not simply sending an SMS from an Android device. It was designing a system that could track what happened to every SMS request and communicate those results back to the backend.
There is still room to improve the project, particularly around device reliability, backend integration, and operational monitoring.
If you're interested in Android development, Kotlin, Firebase, or self-hosted infrastructure, feel free to explore the repository.
Explore the source code: https://github.com/Dhanasekaran-zha/sms-gateway
Feedback, suggestions, and contributions are welcome!
Built with Kotlin, Jetpack Compose, Room, WorkManager, Hilt, and Firebase Cloud Messaging.