tauri-plugin-background-service
A Tauri v2 plugin that manages long-lived background service lifecycle across all platforms (Android, iOS, Windows, macOS, Linux).
You implement a single BackgroundService trait on your own struct. The plugin spawns it in a Tokio task, keeps the OS from killing it on mobile, and provides helpers for notifications and event emission. No business logic lives in the plugin — only lifecycle management.
Platform Support
| Capability | Android | iOS | Desktop (Win/macOS/Linux) |
|---|---|---|---|
| Service runs in background | Foreground Service | BGAppRefreshTask + BGProcessingTask | Standard Tokio task |
| OS service mode | — | — | systemd / launchd (desktop-service feature) |
| Service survives app close | START_STICKY |
No | In-process: No; OS service: Yes |
| Local notifications | Yes | Yes | Yes |
Installation
Rust
Add the plugin to your app's Cargo.toml:
[]
= { = "2" }
= "2"
= "0.2"
npm (TypeScript API)
Rust Usage
1. Implement the BackgroundService trait
Create a struct and implement BackgroundService<R> with init() and run() methods:
use async_trait;
use Runtime;
use ;
2. Register the plugin
In your main.rs, register tauri-plugin-notification before the background-service plugin:
ServiceContext
The ServiceContext<R> passed to init() and run() provides:
notifier— Fire local notifications viactx.notifier.show("Title", "Body")app— Emit events to JS viactx.app.emit("my-event", &payload)shutdown— ACancellationTokenthat resolves whenstopService()is called. Always include it intokio::select!
TypeScript Usage
import {
startService,
stopService,
isServiceRunning,
onPluginEvent,
} from 'tauri-plugin-background-service';
// Start the service (optionally configure the Android notification label)
await startService({ serviceLabel: 'Syncing data' });
// Check status
const running = await isServiceRunning();
// Listen to lifecycle events
const unlisten = await onPluginEvent((event) => {
switch (event.type) {
case 'started':
console.log('Service started');
break;
case 'stopped':
console.log('Service stopped:', event.reason);
break;
case 'error':
console.error('Service error:', event.message);
break;
}
});
// Stop the service
await stopService();
// Clean up listener
unlisten();
Permissions
Add these to your app's capability configuration:
Platform Notes
Android
The plugin uses a Foreground Service with a persistent notification to keep the process alive. Required additions to your app's AndroidManifest.xml (the plugin's manifest already declares these):
FOREGROUND_SERVICEandFOREGROUND_SERVICE_DATA_SYNCpermissionsPOST_NOTIFICATIONSruntime permission (requested automatically on Android 13+)foregroundServiceType="dataSync"on the service declarationstopWithTask="false"ensures the service survives when the user swipes the app awaySTART_STICKYcauses the OS to restart the service if killed under memory pressure
When the service is restarted by the OS, the Rust process is new. Persist any state you need to restore in run() and reload it in init().
iOS
iOS background execution is best-effort. The plugin uses BGTaskScheduler to request periodic execution windows (~30 seconds every 15+ minutes). Required Info.plist additions:
BGTaskSchedulerPermittedIdentifiers
$(PRODUCT_BUNDLE_IDENTIFIER).bg-refresh
UIBackgroundModes
fetch
processing
While the app is foregrounded, your run() loop executes continuously. When backgrounded, Tokio freezes after ~30 seconds. Design your service to handle intermittent execution windows gracefully.
Desktop (Windows, macOS, Linux)
No special OS integration is needed. The service runs as a standard Tokio task and continues as long as the app process is alive.
Links
Documentation (relative paths — works on GitHub and crates.io):
Community (absolute URLs — required for crates.io compatibility):
License
SPDX-License-Identifier: MIT OR Apache-2.0