feat(app): native system notifications for runs and chats that need you
This commit is contained in:
@@ -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")
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
|
||||
<!-- The client reaches the Pipelines server over the shared socket. -->
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<application
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'notification_bridge_stub.dart'
|
||||
if (dart.library.js_interop) 'notification_bridge_web.dart'
|
||||
if (dart.library.io) 'notification_bridge_io.dart'
|
||||
as impl;
|
||||
import 'notification_models.dart';
|
||||
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
|
||||
import 'notification_bridge.dart';
|
||||
import 'notification_models.dart';
|
||||
|
||||
/// System notifications on a phone: Android's notification shade (and iOS's
|
||||
/// centre). The permission is cached so [environment] stays synchronous; it
|
||||
/// is refreshed when the bridge starts and after every request.
|
||||
class NativeNotificationBridge implements NotificationBridge {
|
||||
NativeNotificationBridge() {
|
||||
_ready = _start();
|
||||
}
|
||||
|
||||
final _plugin = FlutterLocalNotificationsPlugin();
|
||||
final Map<int, void Function()> _taps = {};
|
||||
late final Future<void> _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<void> _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<void> _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<String> 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();
|
||||
@@ -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"
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user