• ShipSaaSDocumentation
  • Introduction
  • Getting Started
    • Installation
    • Environment Setup
    • Database Setup
    • Development Guide
  • Database
    • Repository Pattern
    • Drizzle Queries
    • Migration Workflow
    • Drizzle Studio
    • Best Practices
  • File Management
    • File Upload Component
    • Server Actions
    • Storage Configuration
  • Authentication Methods
    • Credentials Authentication
    • Magic Link Authentication
    • Two-Factor Authentication (2FA)
    • Social Authentication
    • API Keys & Bearer Tokens
    • Organizations (Multi-tenant)
  • Authorization & Permissions
    • Roles & Hierarchy
    • Route Protection
    • API Protection
    • CASL Permissions
  • Email & Notifications
    • Resend Setup
    • React Email Templates
    • Notification System
    • Email Workflows
    • Customization & Extension
  • Stripe & Payments
    • Stripe Setup
    • Better Auth Integration
    • Checkout Methods
    • Subscription Management
    • Admin Dashboard
    • Webhook Events
  • Architecture & Development
    • Presentation Layer
    • Service Layer
    • Persistence Layer
    • Helpers Architecture
    • Project Structure
    • Development Workflow
  • Testing
    • General Testing
    • Unit Testing
    • UI Component Testing
    • Repository Testing
    • Service Testing
    • End-to-End Testing
  • Deployment
    • Vercel Deployment
    • GitHub Actions CI/CD
    • Database Backup & Restore
    • Database Migrations
    • Troubleshooting Deployment
  • AI Agents Integration
  • Blog Management
    • Blog Setup
    • MDX Blog
    • Database Blog
  • Async Tasks
    • Setup
    • Email Workflows
    • Payment Processing
    • Maintenance Jobs
  1. Documentation
  2. Email & Notifications
  3. Notification System
Buydocschangelogsdemocommunity

Notification System

PreviousReact Email TemplatesNextEmail Workflows
On this page
Core Service FunctionsMain Notification CreationAvailable Service FunctionsCritical vs Non-Critical Notifications🚨 Critical Notifications (Always Send Email)📢 Non-Critical Notifications (Respect User Preferences)Notification TypesSmart Email IntegrationAutomatic Email Decision LogicUser Preference IntegrationMulti-Language SupportAuthentication IntegrationAuthentication Hooks ConfigurationAuthentication Notification TypesUsage ExamplesCreating a Basic NotificationCreating a Critical NotificationCreating with Custom Title/MessageSpecific Email IntegrationsAuthentication Email HandlingSubscription IntegrationError HandlingUser InterfaceIn-App Notifications Display

The notification service provides intelligent notification management with automatic email sending based on notification criticality and user preferences.

Perfect for: User notifications, system alerts, authentication flows, and subscription communications with smart email delivery.

Core Service Functions

The notification service is located in src/services/notification-service.ts and provides several key functions:

Main Notification Creation

typescript
// From src/services/notification-service.ts
export const createTypedNotificationService = async <
  T extends NotificationType,
>(
  notificationParams: CreateTypedNotification<T>
)

This is the main function for creating notifications with automatic email handling.

Available Service Functions

typescript
// Typed notification with automatic email handling
createTypedNotificationService(notificationParams)

// Basic notification creation
createNotificationService(notificationParams, options)
typescript
// Get specific notification
getNotificationByIdService(id)

// Get user's notifications with pagination
getNotificationsByUserIdService(userId, pagination)

// Get unread notifications
getUnreadNotificationsByUserIdService(userId, pagination)

// Count unread notifications
countUnreadNotificationsByUserIdService(userId)
typescript
// Mark as read
markNotificationAsReadService(id)

// Mark all as read for user
markAllNotificationsAsReadService(userId)

// Update notification
updateNotificationService(updateParams)
typescript
// Delete specific notification
deleteNotificationService(id)

// Delete all read notifications for user
deleteReadNotificationsByUserIdService(userId)

Critical vs Non-Critical Notifications

The system automatically classifies notifications into two categories:

🚨 Critical Notifications (Always Send Email)

Critical notifications bypass user preferences and always send emails:

typescript
// From src/services/notification-service.ts
const criticalTypes: NotificationType[] = [
  'reset_password',
  'email_verification',
  'change_email_verification',
  'magic_link',
  'otp_code',
  'security_alert',
  'password_changed',
  'organization_invitation',
  'subscription_created',
  'subscription_updated',
  'subscription_canceled',
  'subscription_deleted',
]

Why critical: These notifications are essential for security, authentication, or billing and must reach the user regardless of their notification preferences.

📢 Non-Critical Notifications (Respect User Preferences)

All other notification types respect the user's email notification settings:

  • User must have enableEmailNotifications: true
  • Notification channel must be 'email' or 'both'
  • User can opt out of these notifications

Notification Types

The system supports 25+ notification types covering different areas:

Critical authentication notifications:

  • reset_password - Password reset requests
  • email_verification - Email address verification
  • change_email_verification - Email change confirmation
  • magic_link - Passwordless authentication
  • otp_code - Two-factor authentication codes
  • password_changed - Password change confirmation
  • security_alert - Security-related alerts

Billing and subscription notifications:

  • subscription_created - New subscription confirmation
  • subscription_updated - Subscription changes
  • subscription_canceled - Cancellation confirmation
  • subscription_deleted - Subscription deletion
  • payment_succeeded - Successful payments
  • payment_failed - Failed payments

Team collaboration notifications:

  • organization_invitation - Team invitations
  • Plus other organization-related notifications

Other notification types:

  • user_banned - Account suspension
  • Plus 15+ additional notification types for various features

Smart Email Integration

The notification service automatically determines when to send emails:

Automatic Email Decision Logic

typescript
// From src/services/notification-service.ts
// Determine automatically if email is critical
const forceEmail = isCriticalNotification(notificationParams.type)

// Create notification with forced email option
return await createNotificationService(finalNotificationParams, {
  forceEmail,
})

User Preference Integration

For non-critical notifications, the system checks user settings:

typescript
// From src/services/notification-service.ts
const shouldSendEmail =
  options.forceEmail ||
  (userSettings?.enableEmailNotifications &&
    (userSettings?.notificationChannel === 'email' ||
     userSettings?.notificationChannel === 'both'))

Multi-Language Support

The service automatically loads translations based on user language preferences:

typescript
// From src/services/notification-service.ts
const userSettings = await getUserSettingsByUserIdDao(notificationParams.userId)
const userLanguage = userSettings?.language || 'fr'

const translations = await getTranslations({
  locale: userLanguage,
  namespace: `services.domain.notification.${notificationParams.type}`,
})

Authentication Integration

Better Auth seamlessly integrates with the notification service, creating typed notifications instead of sending emails directly. This ensures all authentication emails go through the centralized notification system.

Authentication Hooks Configuration

Better Auth is configured in src/lib/better-auth/auth.ts with hooks that create notifications:

typescript
// From src/lib/better-auth/auth.ts
emailAndPassword: {
  enabled: true,
  requireEmailVerification: AuthAppConfig.requireEmailVerification,
  sendResetPassword: async ({ user, url }) => {
    await createTypedNotificationService({
      userId: user.id,
      type: NotificationTypeConst.reset_password,
      metadata: { url },
    })
  },
},

emailVerification: {
  sendVerificationEmail: async ({ user, url }) => {
    await createTypedNotificationService({
      userId: user.id,
      type: NotificationTypeConst.email_verification,
      metadata: { url },
    })
  },
  sendOnSignUp: true,
  autoSignInAfterVerification: true,
  expiresIn: 3600, // 1 hour
},

user: {
  changeEmail: {
    enabled: AuthAppConfig.changeEmail,
    sendChangeEmailVerification: async ({ user, newEmail, url }) => {
      await createTypedNotificationService({
        userId: user.id,
        type: NotificationTypeConst.change_email_verification,
        metadata: {
          url,
          newEmail, // New email address included in metadata
        },
      })
    },
  },
}

Authentication Notification Types

Better Auth creates these critical notification types automatically:

  • reset_password - Password reset requests
  • email_verification - Email address verification
  • change_email_verification - Email change confirmation
  • magic_link - Passwordless authentication
  • otp_code - Two-factor authentication codes

Centralized approach: All authentication emails go through the notification service, ensuring consistent handling, user preferences respect, and centralized logging.

Usage Examples

Creating a Basic Notification

typescript
// Use the typed notification service for automatic handling
await createTypedNotificationService({
  userId: user.id,
  type: NotificationTypeConst.payment_succeeded,
  metadata: {
    amount: '$29.99',
    planName: 'Pro Plan'
  }
})

Creating a Critical Notification

typescript
// Security alert (critical - always sends email)
await createTypedNotificationService({
  userId: user.id,
  type: NotificationTypeConst.security_alert,
  title: 'Suspicious login detected',
  message: 'We detected a login from a new device',
  metadata: {
    ipAddress: '192.168.1.1',
    location: 'Paris, France'
  }
})

Creating with Custom Title/Message

typescript
// Override automatic translations
await createTypedNotificationService({
  userId: user.id,
  type: NotificationTypeConst.payment_succeeded,
  title: 'Custom payment confirmation',
  message: 'Your payment has been processed successfully',
})

Specific Email Integrations

The service handles specific email integrations for different notification types:

Authentication Email Handling

typescript
// Password reset emails
if (parsed.data.type === 'reset_password' && parsed.data.metadata?.url) {
  await sendResetPasswordLinkEmailService({
    email: targetUser.email,
    url: parsed.data.metadata.url as string,
  })
}

// Email verification
if (parsed.data.type === 'email_verification' && parsed.data.metadata?.url) {
  await sendVerificationEmailService({
    email: targetUser.email,
    url: parsed.data.metadata.url,
  })
}

Subscription Integration

typescript
// Subscription emails use rich subscription data
if (parsed.data.type === 'subscription_created' && parsed.data.metadata?.subscription) {
  await sendSubscriptionCompletedEmailService(
    parsed.data.metadata.subscription
  )
}

Error Handling

The service includes graceful error handling for email failures:

typescript
// From src/services/notification-service.ts
try {
  // Email sending logic...
} catch (emailError) {
  // Don't fail notification creation if email fails
  console.error("Erreur lors de l'envoi de l'email de notification:", emailError)
}

Graceful degradation: Notifications are still created even if email sending fails, ensuring the system remains functional.

User Interface

In-App Notifications Display

The application includes a built-in notification center that displays all user notifications with proper categorization and read/unread states.

In-App Notifications

Interface Features:

  • Real-time updates - Notifications appear instantly
  • Categorized display - Grouped by type and importance
  • Read/unread status - Clear visual indicators
  • Action buttons - Direct actions from notifications
  • Responsive design - Works on all device sizes

Notification Components:

  • Bell icon indicator - Shows unread count
  • Dropdown panel - Expandable notification list
  • Notification cards - Individual notification display
  • Timestamp display - Relative time formatting
  • Action buttons - Context-specific actions

Smart notifications ready! Your application has intelligent notification management with automatic email integration and user preference respect.