Files
pipelines/app/lib/features/notification_service/notification_service_bridge.dart
T
alan fc7dbb0ac0 Remove ntfy: notifications go only through the app notification stream
Server: drop ntfy publishing (stage hooks, run outbox, chat attention, chat
tool notify, auto-switch and schedule notices), the chat push outbox (its
table is dropped at startup), ntfy status/endpoints and PIPELINES_NTFY_*.
The app_notifications stream, subscribe/clear and kinds are unchanged.
Notification settings keep run_finished/chat_needs_you/chat_reply_finished;
ntfy_enabled is accepted and ignored and its stored row is never read.

Stage type=ntfy is rejected: "ntfy stages were removed; notifications go to
the Pipelines apps — use the stage notify hook". The notify hook remains.

Installer: no ntfy build, accounts, config or supervisor child; manifest
gets top-level "mode" (legacy ntfy.mode still read). Reinstall retires an
old ntfy (safe PID-file stop, deletes binary, config, data dir, token, log,
with a note in logs/install.log); uninstall cleans leftovers.

App: ntfy settings section replaced by "Notification events"; texts updated.
Docs: docs/ntfy.md folded into docs/notifications.md; install, docker, cli,
author skill references updated; security review M1 marked resolved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 23:24:24 +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 while the app
/// is closed. 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 [];
}