Use native service as Android notification source
This commit is contained in:
@@ -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<PipelinesApp>
|
||||
store: SharedPreferencesNotificationStore(
|
||||
serverId: serverStorageScope(widget.serverId),
|
||||
),
|
||||
serviceController: _deliveryController,
|
||||
);
|
||||
ChatQuestionNotices? _chatQuestionNotices;
|
||||
|
||||
@@ -152,6 +154,13 @@ class _PipelinesAppState extends State<PipelinesApp>
|
||||
/// 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<PipelinesApp>
|
||||
_ackSync.dispose();
|
||||
_attentionCount.dispose();
|
||||
_notifications.dispose();
|
||||
_deliveryController.dispose();
|
||||
_chatQuestionNotices?.dispose();
|
||||
_delegate.removeListener(_routeChanged);
|
||||
_delegate.dispose();
|
||||
@@ -567,6 +577,7 @@ class _PipelinesAppState extends State<PipelinesApp>
|
||||
onOpenTemplates: _delegate.openTemplates,
|
||||
notifications: _notifications,
|
||||
deliveryService: _deliveryService,
|
||||
deliveryServiceController: _deliveryController,
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
@@ -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<String> get notified => Set<String>.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<void> 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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<void> Function()? requestNotificationPermission;
|
||||
|
||||
@override
|
||||
@@ -49,10 +51,12 @@ class SettingsDeviceDeliverySection extends StatefulWidget {
|
||||
class _SettingsDeviceDeliverySectionState
|
||||
extends State<SettingsDeviceDeliverySection> {
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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<SettingsScreen> {
|
||||
if (widget.deliveryService case final service? when service.supported)
|
||||
SettingsDeviceDeliverySection(
|
||||
bridge: service,
|
||||
controller: widget.deliveryServiceController,
|
||||
requestNotificationPermission: widget.notifications == null
|
||||
? null
|
||||
: () async {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user