Files
pipelines/app/lib/features/notification_service/notification_service_bridge.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

159 lines
4.9 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'host_platform_stub.dart' if (dart.library.io) 'host_platform_io.dart';
import 'notification_service_events.dart';
import 'notification_service_status.dart';
export 'notification_service_events.dart';
export 'notification_service_status.dart';
/// The app's handle on Android background delivery: a native foreground
/// service (android/…/notify/NotificationService.kt) that keeps its own
/// socket to the server and shows the server's notifications, so the ntfy app
/// is not needed. Other platforms have no service ([supported] is false).
abstract interface class NotificationServiceBridge {
bool get supported;
/// Status pushed by the service whenever its connection changes.
Stream<NotificationServiceStatus> get changes;
Future<NotificationServiceStatus> status();
/// Starts the service if delivery is enabled and a server was handed over.
Future<NotificationServiceStatus> start();
/// Hands over the server the app is connected to: its trusted addresses
/// (tried in order) and the web sign-in cookie, empty when none.
Future<NotificationServiceStatus> configure({
required String serverId,
required List<String> endpoints,
required String cookie,
});
Future<NotificationServiceStatus> setEnabled(bool enabled);
/// Opens Android's "ignore battery optimisations" prompt.
Future<bool> requestBatteryExemption();
/// The service's recent connection events, newest first (at most 50).
Future<List<NotificationConnectionEvent>> connectionEvents();
}
/// The bridge for this platform. One instance owns the channel's handler.
NotificationServiceBridge createNotificationServiceBridge() => runsOnAndroid
? MethodChannelNotificationServiceBridge.instance
: const UnsupportedNotificationServiceBridge();
/// `pipelines/notification_service`, implemented by NotificationServiceChannel.kt.
class MethodChannelNotificationServiceBridge
implements NotificationServiceBridge {
@visibleForTesting
MethodChannelNotificationServiceBridge([
this._channel = const MethodChannel(channelName),
]) {
_channel.setMethodCallHandler(_onCall);
}
static const channelName = 'pipelines/notification_service';
static final instance = MethodChannelNotificationServiceBridge();
final MethodChannel _channel;
final _changes = StreamController<NotificationServiceStatus>.broadcast();
@override
bool get supported => true;
@override
Stream<NotificationServiceStatus> get changes => _changes.stream;
Future<void> _onCall(MethodCall call) async {
if (call.method == 'statusChanged') {
_changes.add(NotificationServiceStatus.fromMap(call.arguments));
}
}
Future<NotificationServiceStatus> _invoke(
String method, [
Map<String, Object?>? arguments,
]) async {
final reply = await _channel.invokeMethod<Object?>(method, arguments);
final status = NotificationServiceStatus.fromMap(reply);
_changes.add(status);
return status;
}
@override
Future<NotificationServiceStatus> status() => _invoke('status');
@override
Future<NotificationServiceStatus> start() => _invoke('start');
@override
Future<NotificationServiceStatus> configure({
required String serverId,
required List<String> endpoints,
required String cookie,
}) => _invoke('configure', {
'serverId': serverId,
'endpoints': endpoints,
'cookie': cookie,
});
@override
Future<NotificationServiceStatus> setEnabled(bool enabled) =>
_invoke('setEnabled', {'enabled': enabled});
@override
Future<bool> requestBatteryExemption() async =>
await _channel.invokeMethod<bool>('requestBatteryExemption') ?? false;
@override
Future<List<NotificationConnectionEvent>> connectionEvents() async {
final reply = await _channel.invokeListMethod<Object?>('connectionEvents');
return [
for (final item in reply ?? const <Object?>[])
if (item is Map<Object?, Object?>)
NotificationConnectionEvent.fromMap(item),
];
}
}
/// Web, iOS and desktop: no background service; nothing is pretended.
class UnsupportedNotificationServiceBridge
implements NotificationServiceBridge {
const UnsupportedNotificationServiceBridge();
@override
bool get supported => false;
@override
Stream<NotificationServiceStatus> get changes => const Stream.empty();
@override
Future<NotificationServiceStatus> status() async =>
NotificationServiceStatus.unsupported;
@override
Future<NotificationServiceStatus> start() => status();
@override
Future<NotificationServiceStatus> configure({
required String serverId,
required List<String> endpoints,
required String cookie,
}) => status();
@override
Future<NotificationServiceStatus> setEnabled(bool enabled) => status();
@override
Future<bool> requestBatteryExemption() async => false;
@override
Future<List<NotificationConnectionEvent>> connectionEvents() async =>
const [];
}