7.3 KiB
Background Services
The app runs various background services to sync photos, perform backups, and maintain data consistency without requiring user interaction.
Background Backup Service
The background backup service automatically uploads photos to the server when the app is not in the foreground.
Platform Channel Communication
The app uses platform-specific channels to communicate with native background services:
Channel Name: immich/foregroundChannel
Methods:
| Method | Direction | Purpose |
|---|---|---|
initialized |
Native → App | Notify app that background service started |
start |
App → Native | Start background backup |
stop |
App → Native | Stop background backup |
updateNotification |
App → Native | Update progress notification |
systemStop |
Native → App | System stopped the service |
Notification Structure
Background backup shows progress notifications:
Notification Data:
{
"title": "Backup Progress",
"content": "Uploading photo 5 of 100",
"progress": 5,
"max": 100,
"indeterminate": false,
"isDetail": true
}
Notification Types:
- Total Progress: Overall backup progress (X of Y files)
- Single Progress: Current file upload progress (percentage)
- Error Notification: Shows when upload fails
Background Service Lifecycle
App Backgrounded
│
▼
┌─────────────────┐
│ Service Starts │
│ (initialized) │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Check Conditions│
│ - WiFi required?│
│ - Charging? │
└────────┬────────┘
│
┌────┴────┐
│ │
▼ ▼
Proceed Wait
│
▼
┌─────────────────┐
│ Find Upload │
│ Candidates │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Upload Files │◄──────┐
│ One by One │ │
└────────┬────────┘ │
│ │
▼ │
┌─────────────────┐ │
│ Update Notif. │───────┘
│ Progress │
└────────┬────────┘
│
┌────┴────┐
│ │
▼ ▼
More Complete
Files │
│ ▼
│ ┌─────────────────┐
└───►│ Service Stops │
└─────────────────┘
Settings
| Setting | Description | Default |
|---|---|---|
backgroundBackup |
Enable/disable background backup | false |
backupRequireWifi |
Only backup on WiFi | true |
backupRequireCharging |
Only backup while charging | false |
backupTriggerDelay |
Minutes before starting backup | 30 |
backgroundBackupTotalProgress |
Show total progress notification | true |
backgroundBackupSingleProgress |
Show per-file progress | false |
Error Handling
Grace Period for Notifications:
- Errors don't immediately show notification
uploadErrorNotificationGracePeriodsetting controls delay (default: 2 uploads)- Prevents notification spam for transient errors
Error Recovery:
- Service tracks
backupFailedSincetimestamp - Retries on next service run
- User notified after grace period exceeded
Sync Service
Keeps local database synchronized with server.
Sync Operations
- Asset Sync: Download remote asset metadata
- Album Sync: Sync album memberships and contents
- User Sync: Sync user and partner information
Sync Flow
┌─────────────────┐
│ Sync Trigger │
│ (App Start / │
│ WebSocket) │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Fetch Remote │
│ Changes (ETag) │
└────────┬────────┘
│
┌────┴────┐
│ │
▼ ▼
Changes No
Found Changes
│ │
▼ │
┌─────────────────┐
│ Process Deltas │
│ - New assets │
│ - Updated assets│
│ - Deleted assets│
└────────┬────────┘
│
▼
┌─────────────────┐
│ Update Local DB │
└────────┬────────┘
│
▼
┌─────────────────┐
│ Refresh UI │
└─────────────────┘
ETag-Based Sync
The app uses ETags to efficiently detect changes:
- Store ETag from last sync
- Send ETag in request header
- If server returns 304 (Not Modified), skip sync
- If server returns new data, update local cache and ETag
Remote Asset Removal
When assets are deleted on server:
- Receive list of deleted asset IDs
- Remove from local database
- Update timeline display
- Keep local-only assets intact (if any)
WebSocket Events for Background Sync
Real-time events that trigger sync operations:
| Event | Action |
|---|---|
on_upload_success |
Add new asset to timeline |
on_asset_delete |
Remove asset from local DB |
on_asset_trash |
Move asset to trash |
on_asset_restore |
Restore asset from trash |
on_asset_update |
Refresh asset metadata |
on_asset_hidden |
Remove from visible timeline |
Foreground Upload Service
For uploads while app is visible:
Features
- Shows upload progress in UI
- Can be paused/cancelled by user
- Uses foreground service notification
- Continues if user switches apps briefly
Progress Tracking
Upload State:
- totalCount: Total files to upload
- completedCount: Successfully uploaded
- failedCount: Failed uploads
- currentFile: File being uploaded
- currentProgress: 0-100 percentage
App Lifecycle Integration
On App Resume
- Reconnect WebSocket
- Refresh authentication
- Trigger quick sync
- Resume any pending uploads
On App Pause
- Start background service (if enabled)
- Save current state
- Disconnect WebSocket (after delay)
On App Terminate
- Complete current upload (if possible)
- Schedule background task
- Clean up resources
Battery Optimization
The background service is designed to be battery-efficient:
- Batching: Groups multiple uploads
- Debouncing: Waits before processing WebSocket events (5 second debounce)
- Conditions: Respects WiFi and charging requirements
- Scheduling: Uses system job scheduler when possible
Debug Logging
Enable advanced troubleshooting in settings to see detailed logs:
| Log Level | What's Logged |
|---|---|
| INFO | Service start/stop, sync complete |
| WARNING | Retry attempts, partial failures |
| SEVERE | Errors, failed uploads |