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
| Provider | Detail |
|---|---|
| Twilio | Global, rich features |
| AWS SNS | Native AWS, pay-per-use |
| Vonage / MessageBird | Alternatives |
| Practice | Validate E.164 format; opt-out compliance |
4. Implementing Push Notifications
| Platform | Service |
|---|---|
| Android | FCM (Firebase Cloud Messaging) |
| iOS | APNs (Apple Push) |
| Web | Web Push (VAPID) |
| Cross-platform | FCM, OneSignal |
| Token mgmt | Store + clean invalid tokens |
5. Implementing In-App Notifications
| Aspect | Detail |
|---|---|
| Storage | DB table per user |
| Delivery | WebSocket / SSE / poll |
| State | unread / read / archived |
| Counter | Cached 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
| Granularity | Detail |
|---|---|
| Per channel | Email/SMS/push toggle |
| Per category | Marketing / transactional / security |
| Quiet hours | User timezone aware |
| Frequency cap | Daily digest option |
8. Implementing Notification Queuing
| Reason | Detail |
|---|---|
| Decouple | Sender doesn't wait for provider |
| Throttle | Respect provider rate limits |
| Retry | Transient failures |
| Priority | High-priority queue (security alerts) |
9. Implementing Delivery Tracking
| State | Source |
|---|---|
| queued | App |
| sent | Provider accepted |
| delivered | Webhook from provider |
| opened / clicked | Tracking pixel / wrapped link |
| bounced / failed | Webhook |
| complained | Spam report |
10. Implementing Retry Logic for Failed Notifications
| Failure | Action |
|---|---|
| Transient (5xx) | Retry with exponential backoff |
| Hard bounce | Mark address invalid; stop |
| Soft bounce | Retry up to N times |
| Spam complaint | Suppress immediately |
11. Implementing Notification Batching
| Strategy | Detail |
|---|---|
| Digest | Combine multiple events into one email |
| Window | 5 min, 1 hour, daily |
| Reduce noise | Group by topic/aggregate |
| User-controllable | Per-category cadence |
12. Implementing Multi-Channel Notifications
| Strategy | Detail |
|---|---|
| Channel cascade | Try push → fall back to email |
| Multi-send | Email + in-app together |
| Event → channels mapping | Config-driven |
| Service | Spring NotificationService facade |