Files
pipelines/app/lib/features/notification_service/notification_service_bridge.dart
T
alan 826e420423 Deliver server notifications from the Android app itself
A native foreground service keeps an authenticated websocket to the
connected server (handed over by the app on every connect), subscribes with
a replay cursor, shows each notification with kind-based channels and
grouping, opens pipelines:// links on tap, and clears taps, dismissals and
cross-device clears. It starts at boot and app launch, backs off and follows
network changes. Settings gains the delivery switch, status, notification
and battery-exemption actions, and the server's optional ntfy_enabled
toggle.
2026-11-01 02:21:22 +00:00

140 lines
4.3 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_status.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 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;
}
/// 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;
}