Files
pipelines/app/lib/features/notification_service/notification_service_controller.dart
T
alan 5c41474e6a Make Android notification delivery survive idle
Schedule reconnects with idle-tolerant alarms, keep a watchdog and wake lock, and restart after task removal so delivery continues while the app is closed. Request battery exemption, stop retrying on expired sessions, persist connection diagnostics, and expose the status in Settings.
2026-11-12 02:21:48 +00:00

127 lines
3.6 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'notification_service_bridge.dart';
/// Settings' view of background delivery: the switch, the live status and the
/// battery exemption, all read from the native service (never guessed).
class NotificationServiceController extends ChangeNotifier {
NotificationServiceController(
this.bridge, {
this.requestNotificationPermission,
}) {
_changes = bridge.changes.listen(_apply);
}
final NotificationServiceBridge bridge;
/// Asks Android 13+ for POST_NOTIFICATIONS (the app's existing request).
final Future<void> Function()? requestNotificationPermission;
late final StreamSubscription<NotificationServiceStatus> _changes;
NotificationServiceStatus? _status;
List<NotificationConnectionEvent> _events = const [];
bool _busy = false;
String _error = '';
bool _disposed = false;
NotificationServiceStatus? get status => _status;
/// The service's connection log, newest first.
List<NotificationConnectionEvent> get events => _events;
bool get busy => _busy;
String get error => _error;
bool _askedForExemption = false;
Future<void> load() => _run(bridge.status);
/// Opens Settings: reads the status and, when delivery is already on but the
/// battery exemption is missing (existing users, upgrades), asks for it once
/// per controller. The warning row stays if the prompt is declined.
Future<void> open() async {
await load();
final current = _status;
if (current != null &&
current.enabled &&
!current.batteryExempt &&
!_askedForExemption) {
_askedForExemption = true;
await allowBackgroundDelivery();
}
}
/// Re-reads after returning from a system screen (permission, battery).
/// Never prompts again: a declined prompt is shown as the warning row.
Future<void> refresh() => load();
/// Switching delivery on first asks for the battery exemption, so the
/// service starts with the prompt answered: delivery should not pause while
/// the phone is idle.
Future<void> setEnabled(bool enabled) => _run(() async {
if (enabled) {
if (_status?.notificationsAllowed == false) {
await requestNotificationPermission?.call();
}
if (_status?.batteryExempt == false) {
await bridge.requestBatteryExemption();
}
}
return bridge.setEnabled(enabled);
});
Future<void> allowNotifications() => _run(() async {
await requestNotificationPermission?.call();
return bridge.status();
});
Future<void> allowBackgroundDelivery() => _run(() async {
await bridge.requestBatteryExemption();
return bridge.status();
});
Future<void> _run(Future<NotificationServiceStatus> Function() action) async {
if (_busy) return;
_busy = true;
_error = '';
_notify();
try {
_status = await action();
_events = await bridge.connectionEvents();
} catch (error) {
_error = 'Background delivery is unavailable: $error';
} finally {
_busy = false;
_notify();
}
}
void _apply(NotificationServiceStatus status) {
_status = status;
_notify();
// The service logs its own transitions; re-read so the log stays current.
unawaited(_refreshEvents());
}
Future<void> _refreshEvents() async {
try {
_events = await bridge.connectionEvents();
} catch (error) {
_error = 'Background delivery is unavailable: $error';
}
_notify();
}
void _notify() {
if (!_disposed) notifyListeners();
}
@override
void dispose() {
_disposed = true;
unawaited(_changes.cancel());
super.dispose();
}
}