DEV Community

Nikola Mitrovic
Nikola Mitrovic

Posted on

Android stops your app at 30 SMS a minute. Here is how I paced bulk sending around it

I run SimGate, an SMS gateway where the "infrastructure" is an Android phone on your desk. You pair the phone, call an HTTP API, and the message leaves from your own SIM and your own number.

For a long time the API sent one message per request. Then people started looping over it with lists of 300 customers, and I learned what happens when a phone is asked to behave like a short code. This post is what I found, and the design I ended up with for bulk sending.

Limit 1: Android counts your messages

AOSP ships a class called SmsUsageMonitor. It counts outgoing SMS per app over a sliding window, and the stock defaults are 30 messages per minute. Manufacturers can change the numbers.

Cross the threshold and the phone shows a system dialog asking whether the app may keep sending a large number of messages. Three things make this worse than it sounds:

  • Nothing goes out until someone taps the dialog. For a gateway phone sitting in a drawer, that means sending stops until a human walks over.
  • No server can dismiss it. It is a system dialog, not something your app owns.
  • Multipart messages count once per segment. A 200-character message is two segments, so it spends two of your 30.

So "under 30 a minute" is not the real budget. If half your messages are two segments long, 20 messages already cost 30.

Limit 2: the carrier is watching the pattern

A consumer SIM is sold for person-to-person traffic. The CTIA Messaging Principles describe that traffic as roughly 15 to 60 messages per minute from one number, and no more than about one per second.

Carriers do not publish exact thresholds, but the patterns they act on are well known:

  • sudden bursts from a quiet number
  • identical text to a large list
  • many recipients who have never texted the number
  • complaints

When you cross the line you usually do not get an error. Messages are throttled or silently dropped, and in persistent cases the SIM is suspended.

Limit 3: your own quota, halfway through a list

This one is self-inflicted. With a per-request API, a client looping over 300 numbers with 120 messages left in their plan gets 120 successes and 180 failures. Now they have a half-sent campaign and have to work out who got it.

The design

One message every 3 seconds, per phone

That is 20 a minute. It sits inside the CTIA person-to-person band, and it stays under Android's trigger even when some messages are two segments long. A batch of 500 finishes in about 25 minutes, which is slow by CPaaS standards and perfectly fine for appointment reminders.

It is a default, not a promise about any particular carrier. Fair-use terms differ by plan and country.

Only background sends wait

My first instinct was to rate limit everything going to a phone. That would have been a mistake: an OTP or a monitoring alert would sit behind a 25-minute campaign.

Instead each phone has a pacing lane, and only two senders wait for a slot in it: the bulk runner, and the flush of messages that queued up while the phone was reconnecting. Both are background work whose HTTP request was already answered with 202.

A direct single send is never delayed. It goes to the phone immediately and only records itself in the lane. That record is what makes the batch yield: each single send pushes the batch's next slot back by one interval.

A simplified sketch of the idea:

const PACING_MS = 3000;
const nextSlot = new Map<string, number>(); // deviceId -> timestamp

// Background senders (bulk runner, reconnect flush) wait for a slot.
async function waitForSlot(deviceId: string) {
  const now = Date.now();
  const slot = Math.max(now, nextSlot.get(deviceId) ?? 0);
  nextSlot.set(deviceId, slot + PACING_MS);
  if (slot > now) await sleep(slot - now);
}

// Direct sends never wait. They only push the lane forward.
function recordDirectSend(deviceId: string) {
  const base = Math.max(Date.now(), nextSlot.get(deviceId) ?? 0);
  nextSlot.set(deviceId, base + PACING_MS);
}
Enter fullscreen mode Exit fullscreen mode

The combined rate stays at the pace as long as the single sends alone stay within it.

The honest gap: if you loop over the single-send endpoint faster than 20 a minute, nothing protects you, and the Android dialog can still appear. I decided that was the right trade. Delaying a message a person is waiting for is worse than trusting the caller to use the bulk endpoint for lists.

Quota is reserved up front, all or nothing

The bulk endpoint validates the whole list and reserves quota for all of it before anything is sent. If it does not fit, nothing is taken and the response says how much room there was:

HTTP/1.1 403 Forbidden

{
  "error": "Daily SMS quota exceeded: this batch needs 120 messages but only 50 remain today.",
  "scope": "daily",
  "requested": 120,
  "remaining": 50
}
Enter fullscreen mode Exit fullscreen mode

Unsent messages are refunded if you cancel, and so is any recipient the phone rejects.

A batch you poll, not a request you hold open

A 25-minute HTTP request is not an API. The create call answers at once:

curl -X POST https://api.simgate.app/v1/sms/bulk \
  -H "x-api-key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "deviceId": "YOUR_DEVICE_ID",
    "message": "Reminder: the clinic opens at 8 tomorrow.",
    "recipients": [
      "+14155550123",
      "+14155550124",
      { "phoneNumber": "+447700900123", "message": "Hi Sam, your slot is 8:30." }
    ]
  }'
Enter fullscreen mode Exit fullscreen mode
{
  "success": true,
  "batchId": "b5c1a7e2-0d33-4a0c-9d6b-2e7f1c4a9f10",
  "status": "queued",
  "total": 3,
  "duplicatesRemoved": 0,
  "pacingMs": 3000,
  "estimatedDurationMs": 6000,
  "statusUrl": "/v1/sms/bulk/b5c1a7e2-0d33-4a0c-9d6b-2e7f1c4a9f10"
}
Enter fullscreen mode Exit fullscreen mode

Then you poll until terminal is true:

import time, requests

API = "https://api.simgate.app/v1"
HEADERS = {"x-api-key": "YOUR_API_KEY"}

batch = requests.post(f"{API}/sms/bulk", headers=HEADERS, json={
    "deviceId": "YOUR_DEVICE_ID",
    "message": "Reminder: the clinic opens at 8 tomorrow.",
    "recipients": ["+14155550123", "+14155550124"],
}).json()

while True:
    status = requests.get(f"{API}/sms/bulk/{batch['batchId']}", headers=HEADERS).json()
    print(status["progress"]["done"], "of", status["total"], status["status"])
    if status["terminal"]:
        break
    time.sleep(5)

failed = [i for i in status["items"] if i["status"] in ("failed", "expired")]
Enter fullscreen mode Exit fullscreen mode

Phones go offline, so batches pause

A phone is not a server. It loses Wi-Fi, it gets rebooted, Android puts the app to sleep. If the phone drops mid-batch, the batch moves to paused with a pausedReason (device_offline, device_reconnecting or queue_full) and resumes from the next unsent recipient when the phone is back. The client does nothing.

What I would not use this for

Being clear about the ceiling has saved me more support time than any feature:

  • Fits: appointment reminders, order updates, shift notices, club announcements. Dozens to a few hundred messages at a time, to people who gave you their number for this.
  • Fits with care: a few thousand a month to a steady list, spread over days.
  • Does not fit: cold marketing, purchased lists, tens of thousands a day. That is what a CPaaS provider and a short code are for, and pretending otherwise costs you the SIM.

What is still missing

  • No idempotency key yet. A repeated POST creates a second batch, so keep the batchId and poll it.
  • "Accepted" means the phone sent the message. There are no carrier delivery receipts yet.
  • The pace is fixed per phone. It does not look at segment counts and slow down for long messages.

If you want to try it

The Android app is on Google Play, so there is no sideloading, and the free plan is enough to send a small batch and watch it leave at the paced rate.

If you have run into SmsUsageMonitor on a device where the manufacturer changed the threshold, I would like to hear which phone and what the number was.

Top comments (3)

Collapse
 
userxnom profile image
Abdullah Naseer •

The segment-count point is the one that would have caught me out — 20 two-segment messages spending the whole 30-a-minute budget changes the math completely. The pacing-lane split is the decision I'd steal: a direct send never waits, only the bulk runner does. Since the pace is fixed per phone and doesn't slow down for long messages, have you seen a manufacturer actually change the 30-per-minute threshold, or has stock AOSP been the norm on the phones you've used?

Collapse
 
nikola_mitrovic_922e2665f profile image
Nikola Mitrovic •

Thanks, and you have spotted the weak point. The 3-second pace is safe when
only some messages are long. If every message in a batch is two segments,
20 a minute is 40 segments, which is over the stock threshold. So the honest
answer is that a list of long messages can still trigger the dialog today,
and the fix is to pace by segment count instead of by message. On the threshold itself: I have not caught a phone in the wild with a
different number, so stock AOSP (30 per 60 seconds) has been the norm on the
ones I have used. I would not rely on that, though. The values are read from
global settings (sms_outgoing_check_max_count and
sms_outgoing_check_interval_ms), so a manufacturer or a carrier build can
ship different defaults without it being visible anywhere in the UI. That is also why I picked a pace with headroom rather than sitting at 29 a
minute. If you have a device where it fires earlier, I would like to know
which one.

Collapse
 
userxnom profile image
Abdullah Naseer •

I don't — I haven't seen it fire earlier on any phone I've used, so I can't give you a model to add to the list. The segment-count fix is the part I'm taking away from this: pacing by message count alone clearly isn't the real limit.