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 get changes; Future status(); /// Starts the service if delivery is enabled and a server was handed over. Future 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 configure({ required String serverId, required List endpoints, required String cookie, }); Future setEnabled(bool enabled); /// Opens Android's "ignore battery optimisations" prompt. Future requestBatteryExemption(); /// The service's recent connection events, newest first (at most 50). Future> 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.broadcast(); @override bool get supported => true; @override Stream get changes => _changes.stream; Future _onCall(MethodCall call) async { if (call.method == 'statusChanged') { _changes.add(NotificationServiceStatus.fromMap(call.arguments)); } } Future _invoke( String method, [ Map? arguments, ]) async { final reply = await _channel.invokeMethod(method, arguments); final status = NotificationServiceStatus.fromMap(reply); _changes.add(status); return status; } @override Future status() => _invoke('status'); @override Future start() => _invoke('start'); @override Future configure({ required String serverId, required List endpoints, required String cookie, }) => _invoke('configure', { 'serverId': serverId, 'endpoints': endpoints, 'cookie': cookie, }); @override Future setEnabled(bool enabled) => _invoke('setEnabled', {'enabled': enabled}); @override Future requestBatteryExemption() async => await _channel.invokeMethod('requestBatteryExemption') ?? false; @override Future> connectionEvents() async { final reply = await _channel.invokeListMethod('connectionEvents'); return [ for (final item in reply ?? const []) if (item is Map) 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 get changes => const Stream.empty(); @override Future status() async => NotificationServiceStatus.unsupported; @override Future start() => status(); @override Future configure({ required String serverId, required List endpoints, required String cookie, }) => status(); @override Future setEnabled(bool enabled) => status(); @override Future requestBatteryExemption() async => false; @override Future> connectionEvents() async => const []; }