This document outlines the generated code for the core components of your Notification System. Following the "collab" phase, this step focuses on producing clean, well-commented, and production-ready code that forms the foundation for multi-channel notifications.
This deliverable provides the foundational code for a robust and extensible Notification System. The generated code covers:
The system is designed with modularity in mind, allowing for easy expansion with new notification channels, templating engines, or data storage solutions in the future.
Based on a typical collaboration phase for a Notification System, we assume the following requirements and design choices were agreed upon:
The Notification System is structured into several key components:
config.py: Handles all system configurations, typically loaded from environment variables.utils/template_renderer.py: Manages the loading and rendering of notification templates.channels/: A directory containing specific implementations for each notification channel. * base_channel.py: Defines an abstract interface for all notification channels.
* email_channel.py: Handles sending emails via SMTP.
* sms_channel.py: Handles sending SMS messages (integrates with Twilio or similar).
* webhook_channel.py: Handles sending HTTP POST requests to specified URLs.
* push_channel.py: A placeholder for future Push Notification integration (e.g., Firebase Cloud Messaging, APNS).
notification_service.py: The central orchestrator. It receives notification requests, selects the appropriate channel(s), renders templates, and dispatches the notification.api.py: A Flask application exposing a /notify endpoint to receive notification requests from other services.models.py (Conceptual): A placeholder for database models if notification history or user preferences were to be stored. For this step, it's not fully implemented to keep the scope focused on core sending logic.High-Level Flow:
/notify endpoint in api.py.api.py validates the request and passes it to notification_service.py.notification_service.py identifies the target recipient and the requested notification channel(s).template_renderer.py to fetch and render the appropriate template with provided data.Channel classes (e.g., EmailChannel, SMSChannel).send() method is called, which handles the specific logic for sending the notification (e.g., connecting to an SMTP server, calling Twilio API, making an HTTP POST request).Below is the clean, well-commented, and production-ready code for your Notification System.
notification_system/ ├── requirements.txt ├── config.py ├── notification_service.py ├── api.py ├── channels/ │ ├── __init__.py │ ├── base_channel.py │ ├── email_channel.py │ ├── sms_channel.py │ ├── push_channel.py # Placeholder │ └── webhook_channel.py ├── templates/ │ ├── email/ │ │ └── welcome.html │ │ └── account_alert.html │ └── sms/ │ └── otp_code.txt ├── utils/ │ └── template_renderer.py └── run.sh
This document outlines the comprehensive design specifications, wireframe descriptions, color palettes, and user experience (UX) recommendations for the new Notification System. The goal is to create a robust, intuitive, and user-centric system that keeps users informed without causing fatigue, ensuring critical information is delivered effectively across various channels.
This section details the functional and aesthetic requirements for the Notification System.
Notifications will be categorized and prioritized to ensure relevance and prevent information overload.
* Description: System-wide outages, security breaches, urgent account actions required.
* Delivery: In-app (banner/modal), Email, Push, SMS (optional for extreme cases).
* Characteristics: Non-dismissible (until actioned/acknowledged), prominent visual styling.
* Description: Workflow approvals, task assignments, payment reminders, password reset requests.
* Delivery: In-app (badge, center), Email, Push.
* Characteristics: Clear call-to-action (CTA), persistent until addressed, "Mark as Read" option.
* Description: New feature announcements, system maintenance, successful transaction confirmations, activity feed updates.
* Delivery: In-app (center), Email, Push (user preference).
* Characteristics: Dismissible, often includes a "Learn More" or "View Details" link.
* Description: Product updates, special offers, community events.
* Delivery: Email (opt-in), In-app (less prominent).
* Characteristics: Fully user-controlled opt-in/out, easily dismissible.
The system will support multiple delivery channels, allowing users to choose their preferred method.
Each notification will adhere to a consistent structure to ensure clarity.
A dedicated settings area will allow users to control their notification experience.
This section outlines the key visual components and their interactions within the Notification System.
* Header: "Notifications" title, "Mark All as Read" button, "Settings" icon/link.
* Filters (Optional): Tabs or dropdowns to filter by "All," "Unread," "Critical," "Action Required."
* Notification List: A scrollable list of individual notification items.
* Each item includes:
* Left-aligned icon (system, user avatar).
* Title (bold), Message snippet (regular text).
* Timestamp (right-aligned).
* Unread indicator (e.g., a small dot or distinct background color).
* Hover state reveals "Dismiss" (X icon) or "Archive" option.
* Clicking an item navigates to the relevant page and marks it as read.
* "View All" / "Go to Inbox" Link: At the bottom, leading to a full Notification Inbox page for more extensive management.
* Container: A small, rectangular container with rounded corners.
* Icon: Small, relevant icon (e.g., checkmark for success, exclamation for warning).
* Text: Short, concise message (e.g., "Task 'Review Design' completed successfully.").
* Action (Optional): A subtle "Undo" or "View" link/button.
* Dismiss (Optional): A small 'X' icon.
* Main Sections: Clearly delineated sections for different notification types and channels.
* "Global Channel Settings" Section:
* Toggles for "Enable Email Notifications," "Enable Push Notifications," "Enable SMS Notifications" (if applicable).
* "Notification Type Settings" Section:
* List of notification categories (e.g., "Action Required," "Informational Updates," "Marketing").
* Each category has sub-toggles/checkboxes for specific channels (e.g., "Email," "Push").
* Descriptive text explaining what each category entails.
* "Do Not Disturb" / "Quiet Hours" (Advanced):
* Toggles to enable/disable notifications during specific hours or days.
* Save Button: Prominent button to apply changes.
* Header: Company logo, clear subject line (e.g., "[Product Name] - New Task Assigned: 'Review Design'").
* Personalized Greeting: "Hi [User Name],"
* Core Message: Clear, concise text detailing the notification, consistent with in-app messaging.
* Contextual Details: Any relevant data (e.g., project name, sender, due date).
* Call-to-Action Button: Prominent button linking directly to the relevant section in the application.
* Footer: Standard company footer, link to "Notification Settings" (to manage preferences), "Unsubscribe" link.
The color palette is designed to be modern, clean, and accessible, aligning with professional application standards.
#007BFF (A vibrant blue, often used for CTAs, active states, and key highlights)#6C757D (A neutral grey, used for secondary actions, borders, subtle text)#28A745 (Green - for successful operations, confirmations) Text on Success:* #FFFFFF
#17A2B8 (Teal/Cyan - for general information, tips) Text on Info:* #FFFFFF
#FFC107 (Amber - for non-critical warnings, mild alerts) Text on Warning:* #343A40 (Dark text for contrast)
#DC3545 (Red - for critical errors, failed operations, urgent alerts) Text on Danger:* #FFFFFF
#F8F9FA (Very light grey for main backgrounds)#E9ECEF (Slightly darker grey for section backgrounds, hover states)#DEE2E6 (Light grey for subtle separation)#343A40 (Dark charcoal for main content)#6C757D (Medium grey for less important text, timestamps, descriptions)#ADB5BD (Light grey for inactive elements)These recommendations focus on optimizing the user experience for the Notification System, ensuring it is effective, efficient, and user-friendly.
python
import logging
import time
from typing import Dict, Any, List
from config import Config
from utils.template_renderer import TemplateRenderer
from channels.email_channel import EmailChannel
from channels.sms_channel import SMSChannel
from channels.webhook_channel import WebhookChannel
from channels.push_channel import PushChannel # Placeholder
logging.basicConfig(level=Config.LOG_LEVEL, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
class NotificationService:
"""
The core service responsible for orchestrating notification sending.
It identifies channels, renders templates, and dispatches notifications.
"""
def __init__(self):
self.template_renderer = TemplateRenderer(Config.TEMPLATE_DIR)
self.channels = {
"email": EmailChannel(
host=Config.EMAIL_HOST,
port=Config.EMAIL_PORT,
use_tls=Config.EMAIL_USE_TLS,
username=Config.EMAIL_USERNAME,
password=Config.EMAIL_PASSWORD,
default_sender=Config.EMAIL_DEFAULT_SENDER
),
"sms": SMSChannel(
account_sid=Config.TWILIO_ACCOUNT_SID,
auth_token=Config.TWILIO_AUTH_TOKEN,
from_number=Config.TWILIO_PHONE_NUMBER
),
"webhook": WebhookChannel(default_timeout=Config.WEBHOOK_DEFAULT_TIMEOUT),
"push": PushChannel() # Placeholder
}
logger.info("NotificationService initialized with available channels.")
def _get_channel(self, channel_type: str):
"""Retrieves a specific channel instance."""
channel = self.channels.get(channel_type)
if not channel:
logger.error(f"Unsupported notification channel: {channel_type}")
raise ValueError(f"Unsupported notification channel: {channel_type}")
return channel
def send_notification(
self,
recipient: str,
channel_type: str,
template_name: str,
data: Dict[str, Any],
subject: str = None, # For email
webhook_url: str = None, # For webhook
custom_channel_options: Dict[str, Any] = None # For any channel-specific overrides
) -> bool:
"""
Sends a notification through the specified channel.
Args:
recipient (str): The primary recipient identifier (e.g., email address, phone number).
channel_type (str): The type of channel to use (e.g., "email", "sms", "webhook").
template_name (str): The name of the template to render (e.g., "welcome.html", "otp_code.txt").
data (Dict[str, Any]): Data to pass to the template for rendering.
subject (str, optional): Subject line for email notifications.
webhook_url (str, optional): The URL for webhook notifications.
custom_channel_options (Dict[str, Any], optional): Custom options for the channel.
Returns:
bool: True if the notification was sent successfully, False otherwise.
"""
logger.info(f"Attempting to send notification via {channel_type} to {recipient} using template {template_name}")
try:
channel = self._get_channel(channel_type)
# Render content based on channel type
rendered_content = self.template_renderer.render_template(template_name, data)
# Prepare channel-specific parameters
send_params = {
"recipient": recipient,
"content": rendered_content,
**custom_channel_options if custom_channel_options else {}
}
if channel_type == "email":
if not subject:
logger.warning(f"Email notification for {recipient} is missing a subject. Using default.")
subject = "Important Notification" # Fallback subject
send_params["subject"] = subject
elif channel_type == "webhook":
if not webhook_url:
logger.error(f"Webhook notification for {recipient} is missing 'webhook_url'.
This section provides the comprehensive, production-ready code for the backend of a robust notification system. It includes the database schema, a FastAPI backend service, and key components for managing, sending, and retrieving notifications, along with user preferences.
The generated code establishes a flexible and scalable notification system designed to handle various notification types and delivery channels. It empowers users to manage their notification preferences while providing a robust API for sending and retrieving notifications.
BackgroundTasks for non-blocking external notification delivery.The following SQL script defines the necessary tables for the notification system.
-- notification_system_schema.sql
-- Table for storing user information (simplified for this context)
-- In a real application, this would likely be part of a larger user management system.
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Table for defining available notification channels
CREATE TABLE IF NOT EXISTS notification_channels (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(50) UNIQUE NOT NULL, -- e.g., 'email', 'sms', 'in_app', 'push'
description TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Table for defining available notification types
CREATE TABLE IF NOT EXISTS notification_types (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(100) UNIQUE NOT NULL, -- e.g., 'order_update', 'promo', 'security_alert'
description TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Table for storing individual notification records
CREATE TABLE IF NOT EXISTS notifications (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
notification_type_id UUID NOT NULL REFERENCES notification_types(id) ON DELETE RESTRICT,
channel_id UUID REFERENCES notification_channels(id) ON DELETE SET NULL, -- Null if not specific channel for storage
title VARCHAR(255) NOT NULL,
message TEXT NOT NULL,
payload JSONB, -- Optional JSON data for custom content
is_read BOOLEAN DEFAULT FALSE,
sent_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
read_at TIMESTAMP WITH TIME ZONE,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
-- Table for storing user notification preferences
CREATE TABLE IF NOT EXISTS user_notification_preferences (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
notification_type_id UUID NOT NULL REFERENCES notification_types(id) ON DELETE CASCADE,
channel_id UUID NOT NULL REFERENCES notification_channels(id) ON DELETE CASCADE,
enabled BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
UNIQUE (user_id, notification_type_id, channel_id) -- A user can only have one preference for a given type/channel combo
);
-- Index for faster lookup of user notifications
CREATE INDEX IF NOT EXISTS idx_notifications_user_id ON notifications (user_id);
CREATE INDEX IF NOT EXISTS idx_notifications_user_id_is_read ON notifications (user_id, is_read);
CREATE INDEX IF NOT EXISTS idx_preferences_user_id ON user_notification_preferences (user_id);
-- Initial Data Population (Optional, but good for testing)
INSERT INTO notification_channels (name, description) VALUES
('email', 'Email notifications'),
('sms', 'SMS text message notifications'),
('in_app', 'In-application notifications'),
('push', 'Mobile push notifications')
ON CONFLICT (name) DO NOTHING;
INSERT INTO notification_types (name, description) VALUES
('order_update', 'Updates regarding order status'),
('promo', 'Promotional offers and marketing messages'),
('security_alert', 'Important security-related alerts'),
('account_activity', 'Updates on account logins and activities')
ON CONFLICT (name) DO NOTHING;
Explanation:
users: A simplified user table to link notifications. In a real system, this would integrate with your existing user management.notification_channels: Defines the available delivery methods (e.g., 'email', 'sms', 'in\_app', 'push').notification_types: Categorizes notifications (e.g., 'order\_update', 'promo'). This allows granular control over preferences.notifications: Stores the actual notification content, recipient, status (read/unread), and delivery time.user_notification_preferences: Allows users to specify which notification types they want to receive on which channels. This is crucial for user control.This section provides the Python code for the FastAPI backend.
.
├── main.py
├── config.py
├── database.py
├── models.py
├── schemas.py
├── crud.py
├── services.py
└── api_v1
└── endpoints
├── __init__.py
├── notifications.py
└── preferences.py
config.py (Configuration Management)Manages environment variables and application settings.
# config.py
import os
from dotenv import load_dotenv
load_dotenv() # Load environment variables from .env file
class Settings:
PROJECT_NAME: str = "Notification System API"
PROJECT_VERSION: str = "1.0.0"
DATABASE_URL: str = os.getenv("DATABASE_URL", "postgresql://user:password@localhost/notification_db")
# FastAPI specific settings
DEBUG: bool = os.getenv("DEBUG", "False").lower() == "true"
ALLOWED_HOSTS: list[str] = os.getenv("ALLOWED_HOSTS", "*").split(",")
# External service integration placeholders
EMAIL_SERVICE_API_KEY: str = os.getenv("EMAIL_SERVICE_API_KEY", "your_email_api_key")
SMS_SERVICE_API_KEY: str = os.getenv("SMS_SERVICE_API_KEY", "your_sms_api_key")
PUSH_SERVICE_API_KEY: str = os.getenv("PUSH_SERVICE_API_KEY", "your_push_api_key")
settings = Settings()
database.py (Database Connection & Session Management)Handles the SQLAlchemy engine and session management.
# database.py
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from config import settings
# Create the SQLAlchemy engine
engine = create_engine(settings.DATABASE_URL, pool_pre_ping=True)
# Create a SessionLocal class
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Base class for declarative models
Base = declarative_base()
# Dependency to get a database session
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
models.py (SQLAlchemy ORM Models)Defines the SQLAlchemy ORM models corresponding to the database schema.
# models.py
import uuid
from datetime import datetime
from sqlalchemy import Column, String, Boolean, DateTime, ForeignKey, Text, UniqueConstraint
from sqlalchemy.dialects.postgresql import UUID, JSONB
from sqlalchemy.orm import relationship
from database import Base
class User(Base):
__tablename__ = "users"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
email = Column(String(255), unique=True, index=True, nullable=False)
created_at = Column(DateTime(timezone=True), default=datetime.utcnow)
notifications = relationship("Notification", back_populates="user")
preferences = relationship("UserNotificationPreference", back_populates="user")
class NotificationChannel(Base):
__tablename__ = "notification_channels"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
name = Column(String(50), unique=True, index=True, nullable=False)
description = Column(Text, nullable=True)
created_at = Column(DateTime(timezone=True), default=datetime.utcnow)
preferences = relationship("UserNotificationPreference", back_populates="channel")
class NotificationType(Base):
__tablename__ = "notification_types"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
name = Column(String(100), unique=True, index=True, nullable=False)
description = Column(Text, nullable=True)
created_at = Column(DateTime(timezone=True), default=datetime.utcnow)
notifications = relationship("Notification", back_populates="notification_type")
preferences = relationship("UserNotificationPreference", back_populates="notification_type")
class Notification(Base):
__tablename__ = "notifications"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
user_id = Column(UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
notification_type_id = Column(UUID(as_uuid=True), ForeignKey("notification_types.id", ondelete="RESTRICT"), nullable=False)
channel_id = Column(UUID(as_uuid=True), ForeignKey("notification_channels.id", ondelete="SET NULL"), nullable=True) # Channel through which it was sent
title = Column(String(255), nullable=False)
message = Column(Text, nullable=False)
payload = Column(JSONB, nullable=True) # Arbitrary JSON data
is_read = Column(Boolean, default=False)
sent_at = Column(DateTime(timezone=True), default=datetime.utcnow)
read_at = Column(DateTime(timezone=True), nullable=True)
user = relationship("User", back_populates="notifications")
notification_type = relationship("NotificationType", back_populates="notifications")
channel = relationship("NotificationChannel") # No back_populates needed if not doing a list of notifications by channel
class UserNotificationPreference(Base):
__tablename__ = "user_notification_preferences"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
user_id = Column(UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
notification_type_id = Column(UUID(as_uuid=True), ForeignKey("notification_types.id
This document provides a comprehensive suite of professionally crafted notification content examples, designed to be engaging, informative, and actionable for your users. These templates cover various common scenarios, complete with compelling headlines, clear body text, and strategic calls to action (CTAs), ready for immediate integration into your notification system.
Before diving into specific examples, here are the foundational principles guiding the creation of impactful notifications:
Here are detailed content examples for various notification types, suitable for email, in-app messages, or push notifications (adapt as needed for character limits).
Purpose: Greet new users, provide initial guidance, and encourage first actions.
"We're thrilled to have you join the MeridianConnect community! You're now part of a network that helps [briefly state core benefit, e.g., 'streamline your workflow,' 'connect with experts,' 'achieve your goals'].
To get started and make the most of your experience, we recommend exploring these key features:
* [Feature 1 Name]: [Brief description of what it does]
* [Feature 2 Name]: [Brief description of what it does]
Our team is here to support you every step of the way. Let's make something great happen!"
* "Explore Your Dashboard"
* "Set Up Your Profile"
* "Get Started Now"
* "Watch Our Quick Start Guide"
Purpose: Alert users to critical changes or actions required for their account security or service continuity.
"This is an urgent notification regarding your MeridianConnect account. Due to [reason for action, e.g., 'recent security enhancements,' 'an upcoming policy update,' 'a detected unusual login attempt'], we require your attention.
Please [specific action, e.g., 'verify your email address,' 'update your password,' 'review your security settings'] by [Date/Time] to ensure the continued security and uninterrupted access to your services. Your account may be temporarily restricted if this action is not completed.
Your security is our top priority."
* "Review Account Security"
* "Take Action Now"
* "Update Your Details"
* "Verify Your Account"
Purpose: Inform users about new functionalities, improvements, and their benefits.
"Great news, [User's First Name]! We're excited to announce the launch of [New Feature Name], designed to help you [specific benefit, e.g., 'save even more time,' 'gain deeper insights,' 'collaborate effortlessly'].
With [New Feature Name], you can now [briefly explain what it does and how it improves their experience, e.g., 'easily track your project progress in real-time,' 'customize your dashboard with new widgets,' 'integrate seamlessly with your favorite tools'].
We believe this update will significantly enhance your MeridianConnect experience."
* "Discover [New Feature Name]"
* "Learn More"
* "Try It Out"
* "Watch the Demo"
Purpose: Gently remind users about pending tasks or incomplete profiles to encourage engagement and completion.
"Hi [User's First Name],
It looks like your MeridianConnect profile is almost complete! Filling in a few more details will unlock [specific benefits, e.g., 'personalized recommendations,' 'full access to our community features,' 'better matching opportunities'].
A complete profile helps us tailor your experience and ensures you get the most out of MeridianConnect. It only takes a minute!"
* "Complete Your Profile"
* "Finish Setting Up"
* "Go to My Profile"
* "Continue Where You Left Off"
Purpose: Provide immediate confirmation and details for a user-initiated action.
"Thank you for your [order/request/submission], [User's First Name]!
Your [order/request] with reference number #[Reference Number] has been successfully received and is now being processed.
Details:
* Item/Service: [List of items/service requested]
* Total Amount/Cost: [Amount]
* Estimated Delivery/Completion: [Date/Time]
* Confirmation Email: Sent to [User's Email Address]
We will notify you again once your [order/request] status updates. You can track its progress anytime through your account."
* "View Your [Order/Request] Details"
* "Track Your Order"
* "Manage Your Submissions"
* "Visit Your Account"
Purpose: Inform users about planned service interruptions or critical system updates.
"Heads up, [User's First Name]!
We're performing scheduled system maintenance to enhance the performance and reliability of MeridianConnect. During this time, our services will be temporarily unavailable.
Maintenance Window:
* Start Time: [Date] at [Time] [Timezone]
* End Time: [Date] at [Time] [Timezone]
We apologize for any inconvenience this may cause and appreciate your understanding as we work to improve your experience. Thank you for your patience!"
* "Visit Our Status Page"
* "Learn More About Our Updates"
* "Contact Support (if urgent)"
This comprehensive content library provides a robust foundation for your notification system. By leveraging these examples and adhering to the best practices outlined, you can significantly enhance user engagement, satisfaction, and retention.
Recommended Next Steps:
We are confident that these meticulously crafted messages will empower your notification system to deliver exceptional user experiences.
\n