From 53467596657e33208a3bcec210bd4dead9b28058 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 1 Nov 2026 10:01:16 +0000 Subject: [PATCH] Use native service as Android notification source --- app/lib/application/pipelines_app.dart | 11 +++++ .../notify/notification_controller.dart | 27 ++++++++++-- .../settings_device_delivery_section.dart | 6 ++- .../features/settings/settings_screen.dart | 4 ++ .../notify/notification_delivery_test.dart | 41 +++++++++++++++++++ 5 files changed, 84 insertions(+), 5 deletions(-) diff --git a/app/lib/application/pipelines_app.dart b/app/lib/application/pipelines_app.dart index e78e4a8d..bafe6dd7 100644 --- a/app/lib/application/pipelines_app.dart +++ b/app/lib/application/pipelines_app.dart @@ -14,6 +14,7 @@ import '../features/attention/notify/notification_controller.dart'; import '../features/attention/notify/chat_question_notices.dart'; import '../features/attention/notify/notification_store.dart'; import '../features/notification_service/notification_service_bridge.dart'; +import '../features/notification_service/notification_service_controller.dart'; import '../features/notification_service/notification_service_handoff.dart'; import '../features/attention/runtime/attention_ack_queue.dart'; import '../features/attention/runtime/attention_ack_sync.dart'; @@ -118,6 +119,7 @@ class _PipelinesAppState extends State store: SharedPreferencesNotificationStore( serverId: serverStorageScope(widget.serverId), ), + serviceController: _deliveryController, ); ChatQuestionNotices? _chatQuestionNotices; @@ -152,6 +154,13 @@ class _PipelinesAppState extends State /// Android background delivery (no service elsewhere). final NotificationServiceBridge _deliveryService = createNotificationServiceBridge(); + late final NotificationServiceController _deliveryController = + NotificationServiceController( + _deliveryService, + requestNotificationPermission: () async { + await _notifications.requestPermission(); + }, + ); NotificationServiceHandoff? _deliveryHandoff; @override @@ -250,6 +259,7 @@ class _PipelinesAppState extends State _ackSync.dispose(); _attentionCount.dispose(); _notifications.dispose(); + _deliveryController.dispose(); _chatQuestionNotices?.dispose(); _delegate.removeListener(_routeChanged); _delegate.dispose(); @@ -567,6 +577,7 @@ class _PipelinesAppState extends State onOpenTemplates: _delegate.openTemplates, notifications: _notifications, deliveryService: _deliveryService, + deliveryServiceController: _deliveryController, ), }, ); diff --git a/app/lib/features/attention/notify/notification_controller.dart b/app/lib/features/attention/notify/notification_controller.dart index 84f195d9..b77fee8d 100644 --- a/app/lib/features/attention/notify/notification_controller.dart +++ b/app/lib/features/attention/notify/notification_controller.dart @@ -7,6 +7,7 @@ import 'notification_bridge.dart'; import 'notification_ledger.dart'; import 'notification_models.dart'; import 'notification_store.dart'; +import '../../notification_service/notification_service_controller.dart'; /// The live notification capability behind the `Needs you` surface. /// @@ -31,14 +32,20 @@ import 'notification_store.dart'; /// or retry is a new announcement. The call is idempotent, so passing the whole /// accumulated list is safe. class NotificationController extends ChangeNotifier { - NotificationController({required this.store, NotificationBridge? bridge}) - : bridge = bridge ?? createNotificationBridge(); + NotificationController({ + required this.store, + NotificationBridge? bridge, + this.serviceController, + }) : bridge = bridge ?? createNotificationBridge() { + serviceController?.addListener(_serviceChanged); + } /// The most notifications one publish posts, so a burst cannot flood the tray. static const maxNotificationsPerBatch = 5; final NotificationStore store; final NotificationBridge bridge; + final NotificationServiceController? serviceController; /// The tap seam: the manager sets this to open a run. Nothing here navigates. void Function(int runId)? onOpenRun; @@ -71,14 +78,18 @@ class NotificationController extends ChangeNotifier { /// [publishTransitions]' back. Set get notified => Set.unmodifiable(_ledger.notified); - /// True only when the platform itself reported `granted`. - bool get canNotify => _permission == NotificationPermissionState.granted; + /// True only when the platform itself reported `granted` and Android's + /// background service is not the active notification owner. + bool get canNotify => + _permission == NotificationPermissionState.granted && + !_nativeServiceOwnsNotifications; /// How many notifications this session has actually posted. int get postedCount => _posted; /// Reads the persisted ledger and the platform's real permission facts once. Future load() async { + await serviceController?.load(); NotificationLedger ledger; try { ledger = decodeNotificationLedger(await store.read()); @@ -245,6 +256,14 @@ class NotificationController extends ChangeNotifier { @override void dispose() { _disposed = true; + serviceController?.removeListener(_serviceChanged); super.dispose(); } + + bool get _nativeServiceOwnsNotifications => + serviceController?.status?.enabled == true; + + void _serviceChanged() { + if (!_disposed) notifyListeners(); + } } diff --git a/app/lib/features/settings/settings_device_delivery_section.dart b/app/lib/features/settings/settings_device_delivery_section.dart index 200b5195..c02763fe 100644 --- a/app/lib/features/settings/settings_device_delivery_section.dart +++ b/app/lib/features/settings/settings_device_delivery_section.dart @@ -35,10 +35,12 @@ class SettingsDeviceDeliverySection extends StatefulWidget { const SettingsDeviceDeliverySection({ super.key, required this.bridge, + this.controller, this.requestNotificationPermission, }); final NotificationServiceBridge bridge; + final NotificationServiceController? controller; final Future Function()? requestNotificationPermission; @override @@ -49,10 +51,12 @@ class SettingsDeviceDeliverySection extends StatefulWidget { class _SettingsDeviceDeliverySectionState extends State { late final NotificationServiceController _controller = + widget.controller ?? NotificationServiceController( widget.bridge, requestNotificationPermission: widget.requestNotificationPermission, ); + bool get _ownsController => widget.controller == null; // System prompts (permission, battery) return by resuming the app. late final AppLifecycleListener _lifecycle = AppLifecycleListener( @@ -69,7 +73,7 @@ class _SettingsDeviceDeliverySectionState @override void dispose() { _lifecycle.dispose(); - _controller.dispose(); + if (_ownsController) _controller.dispose(); super.dispose(); } diff --git a/app/lib/features/settings/settings_screen.dart b/app/lib/features/settings/settings_screen.dart index 4911a0ce..9b0b4e2e 100644 --- a/app/lib/features/settings/settings_screen.dart +++ b/app/lib/features/settings/settings_screen.dart @@ -16,6 +16,7 @@ import 'settings_notifications_section.dart'; import 'settings_ntfy_section.dart'; import 'settings_device_delivery_section.dart'; import '../notification_service/notification_service_bridge.dart'; +import '../notification_service/notification_service_controller.dart'; import 'settings_android_download_section.dart'; import 'settings_schedules_section.dart'; import 'settings_sections.dart'; @@ -51,10 +52,12 @@ class SettingsScreen extends StatefulWidget { this.showAndroidDownload = false, this.servers, this.deliveryService, + this.deliveryServiceController, }); /// Android background delivery; hidden when absent or unsupported. final NotificationServiceBridge? deliveryService; + final NotificationServiceController? deliveryServiceController; final transport.PipelinesClient client; @@ -192,6 +195,7 @@ class _SettingsScreenState extends State { if (widget.deliveryService case final service? when service.supported) SettingsDeviceDeliverySection( bridge: service, + controller: widget.deliveryServiceController, requestNotificationPermission: widget.notifications == null ? null : () async { diff --git a/app/test/features/attention/notify/notification_delivery_test.dart b/app/test/features/attention/notify/notification_delivery_test.dart index 5573cc8d..64056e8f 100644 --- a/app/test/features/attention/notify/notification_delivery_test.dart +++ b/app/test/features/attention/notify/notification_delivery_test.dart @@ -4,8 +4,10 @@ import 'package:pipelines_app/features/attention/notify/notification_bridge.dart import 'package:pipelines_app/features/attention/notify/notification_controller.dart'; import 'package:pipelines_app/features/attention/notify/notification_models.dart'; import 'package:pipelines_app/features/attention/notify/notification_store.dart'; +import 'package:pipelines_app/features/notification_service/notification_service_controller.dart'; import 'notification_test_support.dart'; +import '../../notification_service/fake_notification_service.dart'; void main() { group('a dismissed entry never notifies', () { @@ -196,6 +198,45 @@ void main() { ); }); }); + + group('Android background delivery is the single OS source', () { + test( + 'enabled service suppresses bridge posts and toggling takes effect', + () async { + final bridge = FakeNotificationBridge(permission: 'granted'); + final service = FakeNotificationService(); + final serviceController = NotificationServiceController(service); + final notify = NotificationController( + store: MemoryNotificationStore(), + bridge: bridge, + serviceController: serviceController, + ); + addTearDown(notify.dispose); + addTearDown(serviceController.dispose); + await notify.load(); + + expect( + notify.publishTransitions( + [transitionFor(1)], + qualifying: {'1:Failed'}, + ), + 0, + ); + expect(bridge.shown, isEmpty); + + service.push(service.copy(enabled: false)); + await pumpEventQueue(); + expect( + notify.publishTransitions( + [transitionFor(2)], + qualifying: {'2:Failed'}, + ), + 1, + ); + expect(bridge.shown.map((message) => message.runId), [2]); + }, + ); + }); } /// A store whose writes take a tick, so a racing implementation would overlap