Implementing Notification Logic

1. Designing Notification System Architecture

Producer ──> Notification Service ──> Channel adapters ──> Email / SMS / Push / In-app
                  │
                  ├── User preferences
                  ├── Templates
                  └── Delivery store
  

2. Implementing Email Notifications

Example: Spring JavaMailSender

MimeMessage msg = mailSender.createMimeMessage();
var helper = new MimeMessageHelper(msg, true, "UTF-8");
helper.setTo(user.email());
helper.setSubject(t.subject());
helper.setText(t.text(), t.html());
mailSender.send(msg);

3. Implementing SMS Notifications

ProviderDetail
TwilioGlobal, rich features
AWS SNSNative AWS, pay-per-use
Vonage / MessageBirdAlternatives
PracticeValidate E.164 format; opt-out compliance

4. Implementing Push Notifications

PlatformService
AndroidFCM (Firebase Cloud Messaging)
iOSAPNs (Apple Push)
WebWeb Push (VAPID)
Cross-platformFCM, OneSignal
Token mgmtStore + clean invalid tokens

5. Implementing In-App Notifications

AspectDetail
StorageDB table per user
DeliveryWebSocket / SSE / poll
Stateunread / read / archived
CounterCached unread count

6. Implementing Notification Templates

Example: Thymeleaf email template

Context ctx = new Context(Locale.forLanguageTag(user.locale()));
ctx.setVariables(Map.of("name", user.name(), "orderId", order.id()));
String html = templateEngine.process("order-confirmation", ctx);

7. Implementing Notification Preferences

GranularityDetail
Per channelEmail/SMS/push toggle
Per categoryMarketing / transactional / security
Quiet hoursUser timezone aware
Frequency capDaily digest option

8. Implementing Notification Queuing

ReasonDetail
DecoupleSender doesn't wait for provider
ThrottleRespect provider rate limits
RetryTransient failures
PriorityHigh-priority queue (security alerts)

9. Implementing Delivery Tracking

StateSource
queuedApp
sentProvider accepted
deliveredWebhook from provider
opened / clickedTracking pixel / wrapped link
bounced / failedWebhook
complainedSpam report

10. Implementing Retry Logic for Failed Notifications

FailureAction
Transient (5xx)Retry with exponential backoff
Hard bounceMark address invalid; stop
Soft bounceRetry up to N times
Spam complaintSuppress immediately

11. Implementing Notification Batching

StrategyDetail
DigestCombine multiple events into one email
Window5 min, 1 hour, daily
Reduce noiseGroup by topic/aggregate
User-controllablePer-category cadence

12. Implementing Multi-Channel Notifications

StrategyDetail
Channel cascadeTry push → fall back to email
Multi-sendEmail + in-app together
Event → channels mappingConfig-driven
ServiceSpring NotificationService facade