Mahir Shahriar Tamim

AI Researcher · Explainable & Responsible AI · Vision, Language & Agentic Systems

Part 6 of Building a Notification Service

Templates, languages and user preferences

Module 6 · 7 min read

Templates, languages and user preferences

TL;DR

  • Events carry facts. Versioned templates owned by the notification service supply the words for each channel and language.
  • SMS is billed per segment, 160/153 characters in GSM-7 and 70/67 in UCS-2. One accented letter like á, or a single emoji, switches the whole message to UCS-2.
  • Transactional messages skip opt-outs and quiet hours. Marketing respects both, plus frequency caps, checked right before sending.

One message, many shapes

Alex sends Sam $5. Sam actually gets eight messages: push, SMS, email and in-app, each in English or Spanish as Sam chose. A push is a short title and a line or two, an SMS is plain text with a hard length limit, an email has a subject, body and footer, and the inbox can show a card with an icon and a button. All of it comes from templates owned by the notification service, so rewording never needs a payment-service deploy.

Facts in, words out

The payment service sends only facts.

{ "event": "money_received", "user_id": 42,
  "data": { "sender": "Alex", "amount": 5 } }

The notification service fills in the money_received template for each channel in the user’s language.

ChannelEnglishSpanish
SMSYou received ${amount} from {sender}.Recibiste ${amount} de {sender}. Más detalles en la app.
Push titleMoney receivedDinero recibido
Push body{sender} sent you ${amount}.{sender} te envió ${amount}.
  • Version templates and record which version each notification used. Then a complaint about last Tuesday’s confusing SMS shows exactly what it said.
  • Fail loudly on missing variables. Refuse to render, log it and fall back to a safe default text, instead of sending “You received ${amount}” with the braces in.
  • Escape for the channel. A name is user input. HTML-escape it in email, and in SMS don’t let it break the encoding.
  • Preview before publishing every channel and language with sample data, including SMS length. The demo below is a tiny version.

Why one accent can double the SMS cost

This one surprised me. SMS was designed in the 1980s and carries 140 bytes, and how many characters fit depends on the encoding.

  • GSM-7 is a 7-bit alphabet of Latin letters, digits, common punctuation and a few accented letters, so 160 characters fit. A handful, such as { } [ ] ~ \ | ^ €, sit in an “extension table” and count as two.
  • UCS-2 uses 2 bytes per character, so 70 characters. Any character outside GSM-7 forces the whole message into UCS-2. Chinese, Arabic or Hindi letters do, and so do Spanish á, í, ó and ú (GSM-7 has é and ñ, but not those).

Longer texts are split into segments, each losing a few bytes to a “part 2 of 3” header, which leaves 153 GSM-7 or 67 UCS-2 characters each. Operators and gateways usually bill per segment, and that á in “Más” quietly drops the Spanish SMS to 70 characters per segment. Add a longer name or a short payment note and it needs two segments, while the same English text still fits in one. The real trap is emoji. A single πŸŽ‰ isn’t in GSM-7, so it pushes English text into UCS-2 too. The $ sign is safe.

Language
Channel
The counter follows the real rules, 160 / 153 characters for GSM-7 and 70 / 67 for UCS-2, with extension characters counting twice. Add an accented word or an emoji to an English SMS and watch the encoding flip.

Keep SMS templates short and test them with long names and large amounts. Free text the user typed (a payment note, a name) can force UCS-2 or an extra segment, so drop it from the SMS or trim it to a safe length after encoding.

Preferences

The other half of “right message, right channel” is respecting what the user asked for. A typical record:

{ "user_id": 42, "language": "es", "timezone": "Asia/Singapore",
  "channels": { "push": true, "sms": true, "email": false },
  "categories": { "marketing": false, "security": true, "transactions": true },
  "quiet_hours": { "start": "22:00", "end": "08:00" } }

The key idea is categories, because not every message is optional.

  • Transactional and security messages (OTPs, payment receipts, “your password was changed”) are part of the service. Users usually can’t switch them off, though they can often choose where a code goes (SMS or an authenticator app). They ignore quiet hours, since a login code that waits until 8 am is useless.
  • Marketing is optional. It respects opt-outs, quiet hours and frequency caps, and offers an easy unsubscribe.

Quiet hours use the user’s time zone. 2 pm in Singapore is 6 am in London and 1 am in New York (in winter). Marketing that lands inside them waits for the window to end, since it may still matter in the morning, but with an expiry, so an offer ending at midnight doesn’t arrive at 8 am the next day. Frequency caps (“at most two promotions a day”) stop three different campaigns each sending “just one” offer on the same evening.

Message
User allows
Quiet hours are 22:00–08:00 in the user’s own time zone, and the frequency cap is two promotions a day. Transactional messages skip both checks.

Reading preferences fast

Preferences are read on every notification and rarely written, a perfect fit for a cache. Source of truth in the database, a copy in Redis (or an in-process cache with a short expiry), invalidated when a setting changes. For a million-user campaign, filter the audience in bulk before the send (module 7) instead of one lookup per user.

An opt-out must apply quickly, even to queued messages, so check preferences again when the worker is about to send. Letting yesterday’s queued campaign through feels like being ignored.

Language is a preference, so don’t guess it. A number from Mexico doesn’t mean Spanish. Plenty of users there prefer English, and plenty elsewhere prefer Spanish. Ask once, store it, and default sensibly when it’s missing.

Quick check

An English SMS template is 120 characters long and costs one segment. Marketing adds a space and a πŸŽ‰ at the end. How many segments now?

Q&A

Should templates live in code or in a database?

Small teams often keep them in the repo, reviewed like code. Once marketing or support need to edit them, a database with versions, a preview screen and an approval step works better. Either way, each notification records which version it used.

How do I count SMS length correctly in code?

Check every character against the GSM-7 basic and extension tables. All GSM-7? Count extension characters twice and use 160/153. Otherwise count UTF-16 code units (an emoji takes two) and use 70/67. Many SMS libraries do this, but test yours with accented letters, non-Latin scripts and emoji.

References

  • 3GPP TS 23.038, Alphabets and language-specific information (the GSM-7 alphabet and UCS-2 in SMS).
  • Alex Xu, System Design Interview, chapter “Design a notification system” (settings, templates, rate limiting).