Skip to main content
Synced from the repo — do not edit here

Canonical source: docs/claude/chat-notifications.md. This page is generated by docs/scripts/sync-handbook.mjs. Edit the source file in the repo; changes appear here on the next build.

Chat & Messaging + Notifications

Chat Architecture

Real-time chat between staff and clients, plus AI chat for client self-service.

Chat Types

TypeParticipantsUse Case
Coach ChatStaff <-> ClientDirect messaging, care coordination
AI ChatClient <-> AISelf-service health questions
Group ChatMultiple staff + clientsCare team communication

Storage Structure

Simple Chat (legacy, single document):

chats/{clientUid}
- name: string
- email: string
- clinicId: string
- messages: Message[]

Scalable Chat (newer, subcollections):

chatsv2/{conversationId}
- type: 'coach' | 'ai' | 'group'
- participants: string[]
- clinicId: string
- createdAt: timestamp
/messages/{messageId}
- content: string
- authorId: string
- authorRole: 'patient' | 'coach' | 'ai'
- timestamp: timestamp
- readBy: string[]
- media?: MediaItem[]

Message Structure

interface ChatMessage {
time: string; // ISO timestamp
content: string;
author: {
id: string;
name: string;
email?: string;
role: 'patient' | 'coach' | 'ai';
};
media?: {
uuid: string;
mimeType: string;
name: string;
url: string;
}[];
hide: boolean; // Soft delete
readReceipts: string[];
isChatSystemMessage: boolean;
threadId?: string; // For replies
replyToTime?: string;
quote?: string; // Quoted text
}

File Sharing

  • Supported formats: PDF, JPG, JPEG, PNG, GIF, WebP
  • Size limit: 25MB per file
  • Storage: Firebase Storage with signed URLs
  • Path: chats/{conversationId}/attachments/{uuid}

Chat Settings

Per-clinic chat configuration (clinicsv2/{clinic} fields):

  • humanChatEnabled - Allow staff <-> client chat
  • humanChatClientReplyEnabled - Clients can send messages
  • aiChatEnabled - Allow AI chat for clients
  • clientAppChat - Chat visible in client app
  • humanChatInboxOnly - Read-only mode

Key Files

FilePurpose
functions_chat.pyChat backend functions
functions_notifications.pyPush notifications for chat
basisflow-web/components/chat/ChatDrawer.tsxStaff chat UI
basisweb/app/portal/chat/page.tsxClient portal chat
basishybrid/.../route_chat.dartMobile client chat
basiscoreui/.../service_chat.dartShared chat service

Notifications

Comprehensive notification system covering email, push, and in-app notifications across the platform.

Notification Types

TypeTriggerEmailPushIn-App
Appointment BookedNew booking+ iCalYesYes
Appointment Reminder24h beforeYesYes-
Appointment CancelledCancellation+ iCalYesYes
Waitlist ConfirmedAdded to waitlistYes--
Waitlist AvailableSpot openedYesYes-
Client InvitedStaff invites clientYes--
Welcome EmailAccount createdYes--
Onboarding FormsForms dueYes--
New Chat MessageStaff/AI message-YesYes
Lab ResultsResults available-Yes-
Protocol ActivityActivity due-YesYes
Health WindowsCoffee/meal/winddown-YesYes
CGM RemindersSensor expiring/scan-YesYes

Email Notifications

File: functions_appointment_emails.py

Templates

TemplatePurpose
APPOINTMENT_BOOKING_TEMPLATEBooking confirmation
APPOINTMENT_CANCELLATION_TEMPLATECancellation notice
WAITLIST_CONFIRMATION_TEMPLATEWaitlist confirmation
WAITLIST_AVAILABLE_TEMPLATESpot available notification

Key Functions

FunctionPurpose
send_appointment_confirmation_emailBooking confirmation + iCal
send_appointment_cancellation_emailCancel notice + iCal update
send_waitlist_confirmation_emailWaitlist add confirmation
resend_welcome_emailRe-send client invite

iCal Integration

Emails include iCal (ICS) attachments:

  • Auto-adds to calendar on open
  • Cancellations include CANCELLED status
  • utils_ical.py - iCal generation utilities

Branded Email Domains

Clinics can use custom sender domains:

  • Configured via clinicsv2/{clinic}/config/email
  • DNS verification via MailerSend
  • verificationStatus: 'verified' + useBranded: true

Push Notifications

Files: functions_appointment_notifications.py, functions_notifications.py

Appointment Push Flow

clinic_service -> send_appointment_booking_notifications()
-> Get device tokens (users/{uid}.deviceToken)
-> messaging.send_all(messages)
-> Push notification on device

Chat Push

Firestore triggers detect new messages:

  • coach_notification_handler - Global chat
  • clinic_coach_notification_handler - Clinic-scoped chat

Logic:

  1. Compare before/after snapshots for new messages
  2. Validate message author (id, role)
  3. Route to opposite party (coach -> user, user -> coaches)
  4. Send via FCM with thread grouping

In-App Notifications (Basis Hybrid)

Files: service_notifications.dart, service_notifications_io.dart, service_notification_manager.dart

Notification Types

TypeDescription
windowCoffeeCoffee window started
windowPeak1 / windowPeak2Energy peak windows
windowWinddownWind-down time
windowMealMeal window
glucoseScanReminderCGM scan reminder
glucoseSensorExpirationCGM sensor expiring
reminderHabit/event reminder
coachCoach message

Scheduling Features

  • iOS 64-notification limit handling
  • Timezone-aware scheduling
  • Cooldown management (prevent spam)
  • Automatic rescheduling on user preference changes

Deep Linking

Notifications link to relevant screens:

  • Health windows -> Calendar view
  • CGM reminders -> CGM manager modal
  • Chat messages -> Chat screen

Device Token Management

// users/{uid}
{
deviceToken: string; // FCM token
devicePlatform: string; // 'ios' | 'android'
lastTokenUpdate: timestamp;
}
  • Updated on app launch and token refresh
  • Validated before sending (invalid tokens removed)
  • Platform-specific handling (APNS for iOS)

APNS Configuration (iOS)

messaging.APNSConfig(
payload=messaging.APNSPayload(
messaging.Aps(
thread_id='appointments', # Group by type
sound='default',
badge=1,
),
**custom_data
)
)

Key Files

FilePurpose
functions_appointment_emails.pyEmail notifications + iCal
functions_appointment_notifications.pyPush for appointments
functions_notifications.pyPush for chat
functions_email.pyEmail sending core
email_templates.pyHTML email templates
utils_ical.pyiCal generation
basishybrid/.../service_notifications.dartIn-app notifications
basishybrid/.../service_notifications_io.dartNative push handling