background.ts
Purpose
This module defines the Android foreground service task that runs the heat risk prediction pipeline continuously while a monitoring session is active. It also provides helpers for requesting the permissions required to keep a foreground service alive on Android 13+.
Its responsibilities include:
- Defining the
veryIntensiveTaskloop that runs insidereact-native-background-actions - Fetching heart rate in each loop iteration before running the prediction
- Invoking
makeHeatRiskPredictionand persisting each result to the sensor history database - Notifying all registered prediction listeners after each successful result
- Running the heatstroke monitor check on each predicted core temperature
- Requesting Android notification permission required to keep a foreground service alive
- Exporting the
optionsconfiguration object consumed byBackgroundService.start
Invariants
Loop Controlled by BackgroundService.isRunning()
The veryIntensiveTask while loop exits as soon as BackgroundService.isRunning() returns false.
Calling BackgroundService.stop() from sessionMonitor.ts is the sole mechanism to terminate the loop.
Heart Rate Failure is Non-Fatal
If fetchAndroidHRBackground throws, hrResult is set to null and the prediction proceeds.
A missing heart rate causes makeHeatRiskPrediction to select the phone-only model variant rather than aborting.
Prediction Failure is Non-Fatal
If makeHeatRiskPrediction throws, the error is logged and the loop continues after the sleep delay.
Core temperature persistence and listener notification are skipped for that iteration.
Sleep Delay is Configurable
The task receives a { delay } parameter from the options.parameters.delay value.
The default delay is 60,000 ms (1 minute), making this equivalent to the iOS minimum interval.
Constants
| Constant | Value | Description |
|---|---|---|
options.parameters.delay | 60,000 ms | Milliseconds between prediction loop iterations |
options.taskName | "Heat Safe Monitoring" | Background service task identifier |
options.notificationChannelId | "heat_monitor_channel" | Android notification channel |
Exports
veryIntensiveTask
const veryIntensiveTask: (taskData: { delay: number }) => Promise<void>
The background task function passed to BackgroundService.start. Runs the prediction pipeline in a loop until the service is stopped.
requestPersistenceNotificationPermission()
async function requestPersistenceNotificationPermission(): Promise<void>
Requests Android POST_NOTIFICATIONS permission on API 33+ and then calls requestHealthPermissions. Required before starting the foreground service.
requestHealthPermissions()
async function requestHealthPermissions(): Promise<void>
Requests Android notification permission on API 33+. Logs an error if denied.
options
const options: BackgroundServiceOptions
Configuration object for react-native-background-actions. Includes task name, notification title/description, icon, color, delay parameter, and notification channel identifiers.