Skip to main content

Crate tauri_plugin_background_service

Crate tauri_plugin_background_service 

Source
Expand description

§tauri-plugin-background-service

A Tauri v2 plugin that manages long-lived background service lifecycle across Android, iOS, and Desktop.

Users implement the BackgroundService trait; the plugin handles OS-specific keepalive (Android foreground service, iOS BGTaskScheduler), cancellation via CancellationToken, and state management through an actor pattern.

§Quick Start

use tauri_plugin_background_service::{
    BackgroundService, ServiceContext, ServiceError, init_with_service,
};

struct MyService;

#[async_trait::async_trait]
impl<R: tauri::Runtime> BackgroundService<R> for MyService {
    async fn init(&mut self, _ctx: &ServiceContext<R>) -> Result<(), ServiceError> {
        Ok(())
    }

    async fn run(&mut self, ctx: &ServiceContext<R>) -> Result<(), ServiceError> {
        tokio::select! {
            _ = ctx.shutdown.cancelled() => Ok(()),
            _ = do_work(ctx) => Ok(()),
        }
    }
}

tauri::Builder::default()
    .plugin(init_with_service(|| MyService))

§Platform Behavior

PlatformKeepalive MechanismAuto-restart
AndroidForeground service with persistent notification (START_STICKY)Yes
iOSBGTaskScheduler with expiration handlerNo
DesktopPlain tokio::spawnNo

§iOS Setup

Add the following entries to your app’s Info.plist:

<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
    <string>$(BUNDLE_ID).bg-refresh</string>
    <string>$(BUNDLE_ID).bg-processing</string>
</array>

<key>UIBackgroundModes</key>
<array>
    <string>background-processing</string>
    <string>background-fetch</string>
</array>

Replace $(BUNDLE_ID) with your app’s bundle identifier. Without these entries, BGTaskScheduler.shared.submit(_:) will throw at runtime.

See the project repository for detailed platform guides and API documentation.

Re-exports§

pub use error::ServiceError;
pub use models::PluginConfig;
pub use models::PluginEvent;
pub use models::ServiceContext;
pub use models::ServiceState;
pub use models::ServiceStatus;
pub use models::StartConfig;
pub use notifier::Notifier;
pub use service_trait::BackgroundService;
pub use desktop::headless::headless_main;

Modules§

desktop
Desktop OS service management.
error
Error types returned by background service operations.
manager
Actor-based service manager.
models
Data types shared between the plugin’s Rust core and the JS/Tauri layer.
notifier
Thin wrapper around tauri_plugin_notification for fire-and-forget local notifications.
service_trait
The BackgroundService trait — the single entry point for user code.

Functions§

init_with_service
Create the Tauri plugin with your service factory.