tauri-plugin-background-service 0.7.1

Background service lifecycle plugin for Tauri v2 — run long-lived tasks on Android, iOS, and desktop
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
import UIKit
import BackgroundTasks
import UserNotifications
import WebKit
import os.log

/**
 Manages background service lifecycle on iOS using `BGTaskScheduler`.

 ## Required Info.plist Entries

 Add the following entries to your app's `Info.plist` to enable background task scheduling:

 ### BGTaskSchedulerPermittedIdentifiers

 A string array listing the task identifiers this plugin registers. The plugin uses
 two identifiers derived from your bundle identifier:

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

 Replace `$(BUNDLE_ID)` with your app's actual bundle identifier (e.g. `com.example.myapp`).
 Omitting this key causes `BGTaskScheduler.shared.submit(_:)` to throw an error at runtime.

 ### UIBackgroundModes

 Include both `background-processing` and `background-fetch` modes:

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

 - `background-fetch` enables `BGAppRefreshTask` scheduling (~30s budget).
 - `background-processing` enables `BGProcessingTask` scheduling (minutes/hours,
   requires device idle).

 ## Task Behavior

 | Task Type | Budget | Safety Timer | Use Case |
 |-----------|--------|-------------|----------|
 | BGAppRefreshTask | ~30s | 28s (default) | Short periodic work |
 | BGProcessingTask | Minutes/hours | Optional | Long maintenance tasks |

 - Note: Force-quitting the app kills **all** background tasks. iOS will not relaunch
   force-killed apps. Only location/audio/VoIP background modes can relaunch after kill
   (App Store validates legitimate use).
*/
@objc public class BackgroundServicePlugin: Plugin {

    // MARK: - Task Identifiers

    private var refreshTaskId: String {
        "\(Bundle.main.bundleIdentifier ?? "app").bg-refresh"
    }

    private var processingTaskId: String {
        "\(Bundle.main.bundleIdentifier ?? "app").bg-processing"
    }

    // MARK: - State for BGTask lifecycle management

    /// Currently active BGAppRefreshTask, if any.
    private var currentRefreshTask: BGAppRefreshTask?

    /// Currently active BGProcessingTask, if any.
    /// iOS guarantees at most one BGTask is active at a time, so only one of
    /// `currentRefreshTask` or `currentProcessingTask` will be non-nil.
    private var currentProcessingTask: BGProcessingTask?

    /// Pending cancel invoke — shared between both task types since iOS runs at most one.
    private var pendingCancelInvoke: Invoke?

    /// Safety timer — shared between both task types.
    private var safetyTimer: Timer?

    /// iOS safety timeout for BGAppRefreshTask (default: 28.0s).
    /// Set via `startKeepalive` args from Rust (PluginConfig).
    private var safetyTimeout: TimeInterval = 28.0

    /// Optional safety timeout for BGProcessingTask.
    /// When `nil` or `0`, no safety timer is started for processing tasks — only the
    /// iOS expiration handler terminates them. Set via `startKeepalive` args from Rust.
    private var processingSafetyTimeoutSecs: Double?

    /// BGAppRefreshTask earliest begin date in minutes from now (default: 15.0).
    /// Controls how soon iOS can launch the refresh task.
    private var earliestRefreshBeginMinutes: Double = 15.0

    /// BGProcessingTask earliest begin date in minutes from now (default: 15.0).
    /// Controls how soon iOS can launch the processing task.
    private var earliestProcessingBeginMinutes: Double = 15.0

    /// BGProcessingTask requires external power (default: false).
    private var requiresExternalPower: Bool = false

    /// BGProcessingTask requires network connectivity (default: false).
    private var requiresNetworkConnectivity: Bool = false

    // MARK: - Pending Task Info

    /// Information about a BGTask that launched the app in the background.
    /// Queried by Rust on iOS setup to implement auto-start.
    private struct PendingTaskInfo {
        let taskKind: String       // "refresh" or "processing"
        let identifier: String     // BGTask identifier
        let receivedAt: TimeInterval // Date().timeIntervalSince1970
    }

    /// Currently pending BGTask info, set when a BGTask launches the app.
    /// Cleared by Rust after processing the auto-start.
    private var pendingTaskInfo: PendingTaskInfo?

    /// Whether `setTaskCompleted` has been called for the current BGTask.
    /// Prevents double-completion across all terminal paths (expiration, safety
    /// timer, explicit stop, natural completion).
    private var taskCompleted: Bool = false

    // MARK: - Desired State Keys

    /// UserDefaults keys for iOS desired-state persistence.
    private enum DesiredStateKeys {
        static let desiredRunning = "ios_desired_running"
        static let lastStartConfig = "ios_last_start_config"
        static let lastScheduleError = "ios_last_schedule_error"
        static let lastTaskKind = "ios_last_task_kind"
        static let lastTaskStartedAt = "ios_last_task_started_at"
        static let lastTaskCompletedAt = "ios_last_task_completed_at"
    }

    // MARK: - Pending Task Keys

    /// UserDefaults keys for iOS pending BGTask persistence.
    /// Survives timing gaps between BGTask handler and Rust setup.
    private enum PendingTaskKeys {
        static let kind = "ios_pending_task_kind"
        static let identifier = "ios_pending_task_identifier"
        static let receivedAt = "ios_pending_task_received_at"
        static let consumedAt = "ios_pending_task_consumed_at"
    }

    // MARK: - Scheduling Result

    /// Result of submitting BGTaskScheduler requests.
    private struct SchedulingResult {
        let refreshScheduled: Bool
        let processingScheduled: Bool
        let refreshError: String?
        let processingError: String?
    }

    // MARK: - UserDefaults Helpers

    private func persistDesiredRunning(_ running: Bool) {
        UserDefaults.standard.set(running, forKey: DesiredStateKeys.desiredRunning)
    }

    private func persistStartConfig(_ args: [String: Any]) {
        if let data = try? JSONSerialization.data(withJSONObject: args, options: []),
           let json = String(data: data, encoding: .utf8) {
            UserDefaults.standard.set(json, forKey: DesiredStateKeys.lastStartConfig)
        }
    }

    private func persistScheduleError(_ error: String?) {
        if let error = error {
            UserDefaults.standard.set(error, forKey: DesiredStateKeys.lastScheduleError)
        } else {
            UserDefaults.standard.removeObject(forKey: DesiredStateKeys.lastScheduleError)
        }
    }

    private func persistTaskKind(_ kind: String) {
        UserDefaults.standard.set(kind, forKey: DesiredStateKeys.lastTaskKind)
    }

    private func persistTaskStartedAt() {
        UserDefaults.standard.set(Date().timeIntervalSince1970, forKey: DesiredStateKeys.lastTaskStartedAt)
    }

    private func persistTaskCompletedAt() {
        UserDefaults.standard.set(Date().timeIntervalSince1970, forKey: DesiredStateKeys.lastTaskCompletedAt)
    }

    /// Persist pending BGTask info to UserDefaults.
    /// Called when a BGTask handler fires so the info survives timing gaps
    /// between the native handler and Rust setup.
    private func persistPendingTaskInfo(kind: String, identifier: String, receivedAt: TimeInterval) {
        let defaults = UserDefaults.standard
        defaults.set(kind, forKey: PendingTaskKeys.kind)
        defaults.set(identifier, forKey: PendingTaskKeys.identifier)
        defaults.set(receivedAt, forKey: PendingTaskKeys.receivedAt)
        defaults.set(nil, forKey: PendingTaskKeys.consumedAt)
    }

    // MARK: - Plugin Lifecycle

    public override func load(webView: WKWebView) {
        super.load(webView)

        // Request notification permission once.
        // After this, Rust's Notifier can post notifications freely.
        UNUserNotificationCenter.current()
            .requestAuthorization(options: [.alert, .sound, .badge]) { _, _ in }

        // Register both BGTask handlers before the app finishes launching.
        let refreshId = refreshTaskId
        let processingId = processingTaskId

        BGTaskScheduler.shared.register(forTaskWithIdentifier: refreshId, using: .main) {
            [weak self] task in
            if let bgTask = task as? BGAppRefreshTask {
                self?.handleBackgroundTask(bgTask)
            } else {
                (task as? BGTask)?.setTaskCompleted(success: false)
            }
        }

        BGTaskScheduler.shared.register(forTaskWithIdentifier: processingId, using: .main) {
            [weak self] task in
            if let bgTask = task as? BGProcessingTask {
                self?.handleProcessingTask(bgTask)
            } else {
                (task as? BGTask)?.setTaskCompleted(success: false)
            }
        }

        // Foreground/background transition observers.
        // When going to background with desired_running=true and no active BGTask,
        // ensure BGTasks are scheduled so iOS can manage the lifecycle.
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(appDidEnterBackground),
            name: UIApplication.didEnterBackgroundNotification,
            object: nil
        )
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(appWillEnterForeground),
            name: UIApplication.willEnterForegroundNotification,
            object: nil
        )
    }

    // MARK: - Completion Safety

    /// Safely complete the active BGTask exactly once.
    ///
    /// iOS requires `setTaskCompleted` to be called exactly once per BGTask.
    /// This method guards against double-completion by checking the `taskCompleted`
    /// flag before calling `setTaskCompleted`. The flag is reset in `cleanup()`
    /// and when a new BGTask handler fires.
    ///
    /// - Returns: `true` if a task was completed, `false` if already completed or no task.
    @discardableResult
    private func completeActiveTask(success: Bool) -> Bool {
        guard !taskCompleted else { return false }

        if let task = currentRefreshTask {
            taskCompleted = true
            currentRefreshTask = nil
            task.setTaskCompleted(success: success)
            return true
        } else if let task = currentProcessingTask {
            taskCompleted = true
            currentProcessingTask = nil
            task.setTaskCompleted(success: success)
            return true
        }
        return false
    }

    // MARK: - BGAppRefreshTask Handler

    private func handleBackgroundTask(_ task: BGAppRefreshTask) {
        self.currentRefreshTask = task
        self.taskCompleted = false

        let now = Date().timeIntervalSince1970

        // Store pending task info for Rust auto-start on BGTask launch.
        self.pendingTaskInfo = PendingTaskInfo(
            taskKind: "refresh",
            identifier: refreshTaskId,
            receivedAt: now
        )

        // Persist to UserDefaults so info survives timing gaps.
        persistPendingTaskInfo(kind: "refresh", identifier: refreshTaskId, receivedAt: now)

        persistTaskKind("refresh")
        persistTaskStartedAt()

        task.expirationHandler = { [weak self] in
            self?.handleExpiration()
        }

        // Always start safety timer for refresh tasks (default: 28s)
        startSafetyTimer(with: safetyTimeout)
    }

    // MARK: - BGProcessingTask Handler

    private func handleProcessingTask(_ task: BGProcessingTask) {
        self.currentProcessingTask = task
        self.taskCompleted = false

        let now = Date().timeIntervalSince1970

        // Store pending task info for Rust auto-start on BGTask launch.
        self.pendingTaskInfo = PendingTaskInfo(
            taskKind: "processing",
            identifier: processingTaskId,
            receivedAt: now
        )

        // Persist to UserDefaults so info survives timing gaps.
        persistPendingTaskInfo(kind: "processing", identifier: processingTaskId, receivedAt: now)

        persistTaskKind("processing")
        persistTaskStartedAt()

        task.expirationHandler = { [weak self] in
            self?.handleExpiration()
        }

        // Only start safety timer for processing tasks if an explicit timeout was configured
        if let timeout = processingSafetyTimeoutSecs, timeout > 0 {
            startSafetyTimer(with: timeout)
        }
    }

    // MARK: - Expiration Handler (signals Rust to cancel)

    private func handleExpiration() {
        persistTaskCompletedAt()

        // Resolve pending cancel invoke (unblocks Rust thread)
        if let invoke = pendingCancelInvoke {
            invoke.resolve()
            pendingCancelInvoke = nil
        }

        // Complete the active task exactly once
        completeActiveTask(success: false)

        // Schedule next tasks
        scheduleNext()

        // Clear remaining state
        cleanup()
    }

    // MARK: - Safety Timer

    private func startSafetyTimer(with interval: TimeInterval) {
        safetyTimer?.invalidate()
        safetyTimer = Timer.scheduledTimer(withTimeInterval: interval, repeats: false) { [weak self] _ in
            self?.handleSafetyTimerExpiration()
        }
    }

    private func handleSafetyTimerExpiration() {
        persistTaskCompletedAt()

        // Force-complete task if Rust never called completeBgTask
        if currentRefreshTask != nil || currentProcessingTask != nil {
            // Resolve pending cancel invoke (unblocks Rust thread)
            if let invoke = pendingCancelInvoke {
                invoke.resolve()
                pendingCancelInvoke = nil
            }

            // Complete the active task exactly once
            completeActiveTask(success: false)

            // Schedule next tasks
            scheduleNext()

            // Clear remaining state
            cleanup()
        }
    }

    // MARK: - Cleanup

    private func cleanup() {
        currentRefreshTask = nil
        currentProcessingTask = nil
        pendingCancelInvoke = nil
        safetyTimer?.invalidate()
        safetyTimer = nil
        taskCompleted = false
    }

    // MARK: - waitForCancel (Pending Invoke pattern)

    @objc public func waitForCancel(_ invoke: Invoke) {
        // Always store invoke — it will be resolved by expiration/completion
        // or rejected by stopKeepalive, regardless of BGTask state.
        pendingCancelInvoke = invoke
    }

    // MARK: - cancelCancelListener (timeout unblock)

    /// Reject the pending cancel invoke to unblock the Rust `spawn_blocking` thread.
    ///
    /// Called from Rust when the cancel listener timeout fires (default: 4h).
    /// This ensures the `wait_for_cancel` thread does not leak indefinitely
    /// when iOS never resolves the invoke (e.g., app killed in background).
    @objc public func cancelCancelListener(_ invoke: Invoke) {
        if let cancelInvoke = pendingCancelInvoke {
            cancelInvoke.reject(error: nil)
            pendingCancelInvoke = nil
        }
        invoke.resolve()
    }

    // MARK: - completeBgTask (Rust→Swift completion signal)

    @objc public func completeBgTask(_ invoke: Invoke) {
        // Extract success value from invoke arguments
        let success = invoke.args(as: [String: Bool].self)?["success"] ?? true

        // Track whether we had an active BGTask before completion.
        // Prevents spurious rescheduling when completeBgTask is called
        // after expiration or explicit stop already cleaned up the task.
        let hadActiveTask = currentRefreshTask != nil || currentProcessingTask != nil

        // Complete the active task exactly once
        completeActiveTask(success: success)

        // Reject pending cancel invoke (unblocks Rust thread)
        if let cancelInvoke = pendingCancelInvoke {
            cancelInvoke.reject(error: nil)
            pendingCancelInvoke = nil
        }

        // Only reschedule if we actually completed a background task.
        // Avoids scheduling when called after expiration or stop already handled it.
        if hadActiveTask {
            scheduleNext()
        }

        // Clear remaining state
        cleanup()

        // Resolve this invoke
        invoke.resolve()
    }

    // MARK: - startKeepalive (configurable iOS safety timers)

    @objc public func startKeepalive(_ invoke: Invoke) {
        let args = invoke.args(as: [String: Any].self)
        if let args = args {
            // BGAppRefreshTask safety timeout (default: 28.0s via PluginConfig)
            if let timeout = args["iosSafetyTimeoutSecs"] as? Double {
                safetyTimeout = timeout
            }
            // BGProcessingTask safety timeout (default: nil = no cap)
            if let processingTimeout = args["iosProcessingSafetyTimeoutSecs"] as? Double {
                processingSafetyTimeoutSecs = processingTimeout
            }
            // BGAppRefreshTask earliest begin date in minutes
            if let minutes = args["iosEarliestRefreshBeginMinutes"] as? Double {
                earliestRefreshBeginMinutes = minutes
            }
            // BGProcessingTask earliest begin date in minutes
            if let minutes = args["iosEarliestProcessingBeginMinutes"] as? Double {
                earliestProcessingBeginMinutes = minutes
            }
            // BGProcessingTask requires external power
            if let power = args["iosRequiresExternalPower"] as? Bool {
                requiresExternalPower = power
            }
            // BGProcessingTask requires network connectivity
            if let network = args["iosRequiresNetworkConnectivity"] as? Bool {
                requiresNetworkConnectivity = network
            }
        }

        let result = scheduleNext()

        // Persist desired state
        persistDesiredRunning(true)
        if let args = args {
            persistStartConfig(args)
        }
        persistScheduleError(result.refreshError ?? result.processingError)
        UserDefaults.standard.removeObject(forKey: DesiredStateKeys.lastTaskCompletedAt)

        // If both scheduling attempts failed, reject with schedulerUnavailable
        if !result.refreshScheduled && !result.processingScheduled {
            invoke.reject(error: "schedulerUnavailable")
            return
        }

        // Return structured scheduling result
        invoke.resolve([
            "refreshScheduled": result.refreshScheduled,
            "processingScheduled": result.processingScheduled,
            "refreshError": result.refreshError ?? NSNull(),
            "processingError": result.processingError ?? NSNull()
        ] as [String: Any])
    }

    // MARK: - stopKeepalive (clean up active task)

    @objc public func stopKeepalive(_ invoke: Invoke) {
        // Persist desired state
        persistDesiredRunning(false)
        persistTaskCompletedAt()

        // Cancel any pending schedules for both task types
        BGTaskScheduler.shared.cancel(taskRequestWithIdentifier: refreshTaskId)
        BGTaskScheduler.shared.cancel(taskRequestWithIdentifier: processingTaskId)

        // Reject pending cancel invoke unconditionally (unblocks Rust thread)
        // This must happen even when no BGTask is active (foreground stop).
        if let cancelInvoke = pendingCancelInvoke {
            cancelInvoke.reject(error: nil)
            pendingCancelInvoke = nil
        }

        // Complete the active task exactly once
        completeActiveTask(success: false)

        // Clear remaining state
        cleanup()

        invoke.resolve()
    }

    // MARK: - getSchedulingStatus (query scheduling state from UserDefaults)

    @objc public func getSchedulingStatus(_ invoke: Invoke) {
        let defaults = UserDefaults.standard
        invoke.resolve([
            "desiredRunning": defaults.object(forKey: DesiredStateKeys.desiredRunning) as? Bool ?? false,
            "lastStartConfig": defaults.string(forKey: DesiredStateKeys.lastStartConfig) ?? NSNull(),
            "lastScheduleError": defaults.string(forKey: DesiredStateKeys.lastScheduleError) ?? NSNull(),
            "lastTaskKind": defaults.string(forKey: DesiredStateKeys.lastTaskKind) ?? NSNull(),
            "lastTaskStartedAt": defaults.object(forKey: DesiredStateKeys.lastTaskStartedAt) ?? NSNull(),
            "lastTaskCompletedAt": defaults.object(forKey: DesiredStateKeys.lastTaskCompletedAt) ?? NSNull()
        ] as [String: Any])
    }

    // MARK: - Pending BGTask Query (for Rust auto-start)

    /// Return the pending BGTask info that launched the app in the background.
    ///
    /// Called by Rust during iOS plugin setup to detect whether the app was
    /// launched by iOS for a background task. If a pending task exists and
    /// `desired_running` is true in UserDefaults, Rust auto-starts the service.
    ///
    /// Reads from UserDefaults as the source of truth so the info survives
    /// timing gaps between the BGTask handler and Rust setup.
    @objc public func getPendingBgTask(_ invoke: Invoke) {
        let defaults = UserDefaults.standard
        let kind = defaults.string(forKey: PendingTaskKeys.kind)
        let identifier = defaults.string(forKey: PendingTaskKeys.identifier)
        let receivedAt = defaults.object(forKey: PendingTaskKeys.receivedAt) as? TimeInterval
        let consumedAt = defaults.object(forKey: PendingTaskKeys.consumedAt) as? TimeInterval

        if let kind = kind, let identifier = identifier {
            invoke.resolve([
                "taskKind": kind,
                "identifier": identifier,
                "receivedAt": receivedAt ?? 0,
                "consumedAt": consumedAt ?? NSNull()
            ] as [String: Any])
        } else {
            invoke.resolve([
                "taskKind": NSNull(),
                "identifier": NSNull(),
                "receivedAt": NSNull(),
                "consumedAt": NSNull()
            ] as [String: Any])
        }
    }

    /// Mark the pending BGTask info as consumed by setting the consumed_at
    /// timestamp in UserDefaults. The in-memory property is also cleared.
    @objc public func clearPendingBgTask(_ invoke: Invoke) {
        UserDefaults.standard.set(Date().timeIntervalSince1970, forKey: PendingTaskKeys.consumedAt)
        pendingTaskInfo = nil
        invoke.resolve()
    }

    // MARK: - Foreground/Background Transitions

    /// When the app transitions to background, ensure BGTasks are scheduled
    /// if desired_running is true and no BGTask is currently active.
    /// This covers the case where the user started the service in the foreground
    /// and then backgrounds the app — iOS needs scheduled BGTasks to potentially
    /// relaunch the app later.
    @objc private func appDidEnterBackground() {
        let desired = UserDefaults.standard.bool(forKey: DesiredStateKeys.desiredRunning)
        if desired && currentRefreshTask == nil && currentProcessingTask == nil {
            scheduleNext()
        }
    }

    /// No special action on foreground transition — the service keeps running.
    @objc private func appWillEnterForeground() {
        // Intentionally empty. Service runs continuously while app is active.
    }

    // MARK: - Scheduling

    private let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "app.tauri.backgroundservice", category: "BGTaskScheduler")

    @discardableResult
    private func scheduleNext() -> SchedulingResult {
        var refreshScheduled = false
        var refreshError: String?
        var processingScheduled = false
        var processingError: String?

        // BGAppRefreshTask — runs opportunistically, ~30s budget
        let refreshReq = BGAppRefreshTaskRequest(identifier: refreshTaskId)
        refreshReq.earliestBeginDate = Date(timeIntervalSinceNow: earliestRefreshBeginMinutes * 60)
        do {
            try BGTaskScheduler.shared.submit(refreshReq)
            refreshScheduled = true
        } catch {
            refreshError = error.localizedDescription
            logger.error("Failed to submit BGAppRefreshTask '\(self.refreshTaskId)': \(error.localizedDescription)")
        }

        // BGProcessingTask — runs when device idle, minutes budget
        let processingReq = BGProcessingTaskRequest(identifier: processingTaskId)
        processingReq.earliestBeginDate = Date(timeIntervalSinceNow: earliestProcessingBeginMinutes * 60)
        processingReq.requiresExternalPower = requiresExternalPower
        processingReq.requiresNetworkConnectivity = requiresNetworkConnectivity
        do {
            try BGTaskScheduler.shared.submit(processingReq)
            processingScheduled = true
        } catch {
            processingError = error.localizedDescription
            logger.error("Failed to submit BGProcessingTask '\(self.processingTaskId)': \(error.localizedDescription)")
        }

        return SchedulingResult(
            refreshScheduled: refreshScheduled,
            processingScheduled: processingScheduled,
            refreshError: refreshError,
            processingError: processingError
        )
    }
}