Sub topic 5
Respecting user preferences and quiet hours
This chapter assumes you know how an event enters the system (see the ingestion chapter), can read short Python snippets, and have used a key-value store or a database table. The goal is to decide which channels a notification may use for a given user at a given moment.
Why preferences belong in the pipeline
A notification system that ignores user choices gets muted, uninstalled, or reported as spam. Preferences are checked after an event is accepted and before anything is queued for a channel worker. Doing this early means you never spend queue capacity or provider fees on a message the user does not want.
Each event carries a type that is used for the preference lookup, as in this event:
from dataclasses import dataclass, field
@dataclass(frozen=True)
class NotificationEvent:
event_id: str
user_id: str
type: str
priority: str = "normal"
payload: dict = field(default_factory=dict)
event = NotificationEvent(
event_id="evt-1001",
user_id="user-42",
type="order_shipped",
payload={"order_id": "A-77"},
)What to store
Keep preferences per user, per notification type, per channel. A small record is enough:
- A map from notification type to the list of enabled channels.
- A global opt-out flag per channel, for example "no SMS at all".
- A quiet-hours window and the user's time zone.
Store the time zone name, such as Europe/Berlin, not a fixed UTC offset. Offsets change with daylight saving time.
Choosing channels
Routing starts from the channels the user enabled for the event type. During quiet hours, it keeps only the non-intrusive in-app channel, unless the event is critical.
def choose_channels(event, prefs, quiet_hours_active):
"""Return the channels to use for this event.
prefs maps a notification type to the channels the user enabled,
for example {"order_shipped": ["push", "email"]}.
"""
allowed = list(prefs.get(event.type, []))
if quiet_hours_active and event.priority != "critical":
allowed = [c for c in allowed if c == "in_app"]
return allowedFor order_shipped with preferences {"order_shipped": ["push", "email"]}, the result is ["push", "email"] normally and [] during quiet hours. An empty list is a valid outcome. It means "send nothing now", and the pipeline should record that decision rather than treat it as an error.
Common errors
- Evaluating quiet hours in server time. Convert the current time to the user's time zone first. A window such as 22:00 to 07:00 crosses midnight, so test
now >= start or now < end, not a simple range. - Treating "critical" as a free pass. Reserve it for security codes and safety alerts. Marketing messages marked critical will cost you user trust.
- Dropping instead of deferring. A non-urgent message blocked by quiet hours may still be worth delivering at 07:00. Whether to drop or defer is a product decision, so make it explicit.
- Reading stale preferences. If you cache them, expire the cache quickly or invalidate it on change, so an opt-out takes effect at once.
- Defaulting to opt-in silently. Decide what an unknown type means. Defaulting to "no channels" is the safer choice.
Exercise
A user has enabled push and inapp for the type "commentreply". It is 23:30 in their time zone, and their quiet hours run from 22:00 to 07:00. What does choose_channels return for a normal-priority event, and what does it return for a critical one?
Show answer
For the normal-priority event it returns ["inapp"], because quiet hours remove push. For the critical event it returns ["push", "inapp"], because critical events skip the quiet-hours filter.