Scheduled Notifications Flow
Flow Overview
These diagrams outline the process for creating scheduled notifications based on user intentions and subsequently sending them out.
1. Notification Creation/Scheduling Flow
2. Notification Sending Flow
Process Description
-
Notification Creation/Scheduling:
- This process is typically triggered when a user updates their profile or, more significantly, changes their selected
Intention(s). TheIntentionScheduledNotificationsService::createUserScheduledNotificationsmethod is called. - The service first checks if the necessary user profile and notification settings exist (
checkRequirements). If settings are missing, default ones might be created viaUserService. - If requirements are met, the service dispatches a batch of jobs to the
highpriority queue. Depending on the$typeparameter passed to the service, it might dispatch jobs for 'all' notification types or just a specific one. - The dispatched jobs (
CreateUserAffirmationsJob,CreateUserLettersJob, etc.) are responsible for the actual creation logic for their respective content types:- Each job likely fetches the relevant content items (e.g.,
Affirmations associated with the user's intention). - It retrieves the user's notification settings (start/end time, frequency) to determine appropriate times.
- It calculates specific future
date_timevalues for each notification to be scheduled. - It creates records in the
scheduled_notificationstable, linking theuser_id, the content (messageable_id,messageable_type), the calculateddate_time, and potentially the relatedintention_id.
- Each job likely fetches the relevant content items (e.g.,
- This process is typically triggered when a user updates their profile or, more significantly, changes their selected
-
Notification Sending:
- The
HandleIntentionScheduledNotificationsJobis configured to run every five minutes through Laravel's Task Scheduling inbootstrap/app.php, where it's registered with$schedule->job(new HandleIntentionScheduledNotificationsJob())->everyFiveMinutes();. - This central job acts as a dispatcher. It immediately dispatches the
ProcessAffirmationScheduledNotificationsJoband then subsequently dispatches the jobs for other notification types (ProcessLetter...,ProcessAudioAffirmation..., etc.) with increasing delays (40s, 80s, 120s, 160s). This staggering prevents sending all notification types at the exact same moment. - Each individual
Process...Job(e.g.,ProcessAffirmationScheduledNotificationsJob) executes the sending logic for its type:- It queries the
scheduled_notificationstable for records of its specifictypethat are due to be sent (likely checking ifdate_timeis in the past andlast_sent_atis null or older than a certain threshold, although the exact query logic isn't shown). - For each due notification found, it retrieves the user and the associated content (
messageable). - It calls Firebase Cloud Messaging (FCM) to deliver the push notification content to the user's device.
- Upon successful sending (or attempted sending), it updates the
last_sent_attimestamp on the correspondingscheduled_notificationsrecord to prevent it from being sent again immediately. Note: TheIntentionScheduledNotificationsService::updateLastSentTimemethod exists but its usage isn't shown directly in the provided job code; theProcess...Jobs likely updatelast_sent_atdirectly.
- It queries the
- The
Key Models & Services
- IntentionScheduledNotificationsService: Service responsible for initiating the creation of scheduled notifications based on user profile/intentions.
- HandleIntentionScheduledNotificationsJob: Central job triggered by the scheduler to dispatch individual sending jobs with delays.
- CreateUser[Type]Job (e.g.,
CreateUserAffirmationsJob): Jobs responsible for querying content and creatingScheduledNotificationrecords in the database. - Process[Type]ScheduledNotificationsJob (e.g.,
ProcessAffirmationScheduledNotificationsJob): Jobs responsible for querying due notifications, triggering the actual sending, and updating thelast_sent_atstatus. - ScheduledNotification: Eloquent model representing a single scheduled notification entry in the database, linking user, content, and time.
- User: The recipient of the notification.
- Intention: May be linked to a notification.
- [Content Models] (
Affirmation,Letter, etc.): The actual content associated with a notification via themessageablerelationship. - (Implicit) System Scheduler: Cron or Laravel Scheduler that runs
HandleIntentionScheduledNotificationsJobperiodically. - (Implicit) Notification Sending Service: The service used to send push notifications (e.g., FCM) or other notification types.
- (Implicit) Queue System: Laravel's queue system manages the execution of dispatched jobs.