Use native service as Android notification source
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user