From a74a618c94cbb9fe4392a9a22b2f415058f01ef6 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 23 Oct 2026 19:56:14 +0100 Subject: [PATCH] feat(app): native system notifications for runs and chats that need you --- app/android/app/build.gradle.kts | 6 + app/android/app/src/main/AndroidManifest.xml | 1 + .../attention/notify/notification_bridge.dart | 1 + .../notify/notification_bridge_io.dart | 117 ++++++++++++++++++ app/pubspec.lock | 88 +++++++++++++ app/pubspec.yaml | 1 + 6 files changed, 214 insertions(+) create mode 100644 app/lib/features/attention/notify/notification_bridge_io.dart diff --git a/app/android/app/build.gradle.kts b/app/android/app/build.gradle.kts index ca3f5db9..6faa6cdf 100644 --- a/app/android/app/build.gradle.kts +++ b/app/android/app/build.gradle.kts @@ -10,6 +10,8 @@ android { ndkVersion = flutter.ndkVersion compileOptions { + // flutter_local_notifications needs the newer Java APIs on older phones. + isCoreLibraryDesugaringEnabled = true sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } @@ -43,3 +45,7 @@ kotlin { flutter { source = "../.." } + +dependencies { + coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:2.1.4") +} diff --git a/app/android/app/src/main/AndroidManifest.xml b/app/android/app/src/main/AndroidManifest.xml index 824baacd..12b74436 100644 --- a/app/android/app/src/main/AndroidManifest.xml +++ b/app/android/app/src/main/AndroidManifest.xml @@ -1,4 +1,5 @@ + _taps = {}; + late final Future _ready; + String _permission = 'default'; + bool _started = false; + int _nextId = 1; + + static const _channel = AndroidNotificationDetails( + 'pipelines.attention', + 'Runs and chats that need you', + channelDescription: + 'A run waiting for a decision, a failed run, or a ' + 'chat question.', + importance: Importance.high, + priority: Priority.high, + ); + + Future _start() async { + try { + await _plugin.initialize( + settings: const InitializationSettings( + android: AndroidInitializationSettings('@mipmap/ic_launcher'), + iOS: DarwinInitializationSettings( + requestAlertPermission: false, + requestBadgePermission: false, + requestSoundPermission: false, + ), + ), + onDidReceiveNotificationResponse: (response) { + final id = response.id; + if (id != null) _taps.remove(id)?.call(); + }, + ); + _started = true; + await _refresh(); + } catch (_) { + _started = false; + } + } + + Future _refresh() async { + if (Platform.isAndroid) { + final enabled = await _plugin + .resolvePlatformSpecificImplementation< + AndroidFlutterLocalNotificationsPlugin + >() + ?.areNotificationsEnabled(); + _permission = enabled == true ? 'granted' : 'default'; + } + } + + @override + NotificationEnvironment environment() => NotificationEnvironment( + supported: Platform.isAndroid || Platform.isIOS, + // A native app needs no secure origin. + secureContext: true, + permission: _permission, + ); + + @override + Future requestPermission() async { + await _ready; + if (!_started) return 'default'; + bool? granted; + if (Platform.isAndroid) { + granted = await _plugin + .resolvePlatformSpecificImplementation< + AndroidFlutterLocalNotificationsPlugin + >() + ?.requestNotificationsPermission(); + } else if (Platform.isIOS) { + granted = await _plugin + .resolvePlatformSpecificImplementation< + IOSFlutterLocalNotificationsPlugin + >() + ?.requestPermissions(alert: true, badge: true, sound: true); + } + _permission = granted == true + ? 'granted' + : granted == false + ? 'denied' + : 'default'; + return _permission; + } + + @override + bool show(NotificationMessage message, {void Function()? onTap}) { + if (!_started || _permission != 'granted') return false; + final id = _nextId++; + if (onTap != null) _taps[id] = onTap; + _plugin.show( + id: id, + title: message.title, + body: message.body, + notificationDetails: const NotificationDetails(android: _channel), + payload: message.tag, + ); + return true; + } +} + +NotificationBridge createNotificationBridge() => NativeNotificationBridge(); diff --git a/app/pubspec.lock b/app/pubspec.lock index ad8ff099..19c0629e 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -73,6 +73,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.9" + dbus: + dependency: transitive + description: + name: dbus + sha256: f7db8504e715b835f5ebe5fe74604ccd961d504c73389a5d8e81bafc2e2f3612 + url: "https://pub.dev" + source: hosted + version: "0.8.0" fake_async: dependency: transitive description: @@ -110,6 +118,46 @@ packages: url: "https://pub.dev" source: hosted version: "6.0.0" + flutter_local_notifications: + dependency: "direct main" + description: + name: flutter_local_notifications + sha256: "9ee28742343dd13df758bb4185e8ded37ac2d1aeca6648c3ba7862c99797517e" + url: "https://pub.dev" + source: hosted + version: "22.3.2" + flutter_local_notifications_linux: + dependency: transitive + description: + name: flutter_local_notifications_linux + sha256: "3b89a4bf095fa5f8a99beeabf982c5bd506c3ad25e7b9e61da945e994dc5b615" + url: "https://pub.dev" + source: hosted + version: "8.0.2" + flutter_local_notifications_platform_interface: + dependency: transitive + description: + name: flutter_local_notifications_platform_interface + sha256: "43c3761d916c9bd3d5c7ebbc44d82f4990329840c0c5d62ad5260cc1b5d399bd" + url: "https://pub.dev" + source: hosted + version: "12.2.0" + flutter_local_notifications_web: + dependency: transitive + description: + name: flutter_local_notifications_web + sha256: "516afaf97a2d1e67a036c6617321b00d205d72f7a67b6eccf936cd565f985878" + url: "https://pub.dev" + source: hosted + version: "1.0.0" + flutter_local_notifications_windows: + dependency: transitive + description: + name: flutter_local_notifications_windows + sha256: "6f43bdd03b171b7a90f22647506fea33e2bb12294b7c7c7a3d690e960a382945" + url: "https://pub.dev" + source: hosted + version: "3.1.1" flutter_test: dependency: "direct dev" description: flutter @@ -128,6 +176,22 @@ packages: url: "https://pub.dev" source: hosted version: "0.15.7" + http: + dependency: transitive + description: + name: http + sha256: "87721a4a50b19c7f1d49001e51409bddc46303966ce89a65af4f4e6004896412" + url: "https://pub.dev" + source: hosted + version: "1.6.0" + http_parser: + dependency: transitive + description: + name: http_parser + sha256: "178d74305e7866013777bab2c3d8726205dc5a4dd935297175b19a23a2e66571" + url: "https://pub.dev" + source: hosted + version: "4.1.2" jni: dependency: transitive description: @@ -240,6 +304,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.3.0" + petitparser: + dependency: transitive + description: + name: petitparser + sha256: "91bd59303e9f769f108f8df05e371341b15d59e995e6806aefab827b58336675" + url: "https://pub.dev" + source: hosted + version: "7.0.2" platform: dependency: transitive description: @@ -365,6 +437,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.7.11" + timezone: + dependency: transitive + description: + name: timezone + sha256: "9f4e5a1a0e4fef7dd3424842c0575db01bfb7a11730e8d55753fcd5c5c98767b" + url: "https://pub.dev" + source: hosted + version: "0.11.2" typed_data: dependency: transitive description: @@ -493,6 +573,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.1.0" + xml: + dependency: transitive + description: + name: xml + sha256: "67f0aff7be013d107995e9b75bf4e7f2c3ef2dfdb2c8e68024bba0a7fd5756a4" + url: "https://pub.dev" + source: hosted + version: "7.0.1" sdks: dart: ">=3.12.2 <4.0.0" flutter: ">=3.44.0" diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 400616af..0545d1e3 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -41,6 +41,7 @@ dependencies: web: ^1.1.1 video_player: ^2.14.1 webview_flutter: ^4.14.1 + flutter_local_notifications: ^22.3.2 dev_dependencies: flutter_test: