b91ef04ab0
- Server: chat ntfy messages carry a sequence id derived from their app id; clearing a chat app notification also clears it in ntfy (PUT /<topic>/<seq>/clear, verified on ntfy v2.38.1). - Server: a turn that sent an agent notify does not also send chat_reply. - notify tool and MCP instructions: Pipelines already notifies at reply end and for questions/approvals; notify is for mid-turn or background news. - Shown notify items carry channels (apps/phone); the transcript row says where it went. - App: NotificationBridge.close(tag); web closes tracked Notifications and tells other tabs over a BroadcastChannel; native bridge cancels by tag and now supports macOS. AgentNotices closes on notifications_cleared and on resubscribe cleared ids, and a tap sends clear_notifications. - Docs: skill, chat reference, ntfy/notifications docs, app note. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
158 lines
4.8 KiB
Dart
158 lines
4.8 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
|
|
|
import 'notification_bridge.dart';
|
|
import 'notification_models.dart';
|
|
|
|
/// System notifications from the native app: Android's notification shade (when
|
|
/// its background notification service is off), iOS's and macOS's
|
|
/// notification centre. [close] removes a delivered one by its tag. 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 = {};
|
|
|
|
/// The platform id each shown tag was posted with, so [close] can remove it.
|
|
final Map<String, int> _ids = {};
|
|
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,
|
|
);
|
|
|
|
static const _darwin = DarwinInitializationSettings(
|
|
requestAlertPermission: false,
|
|
requestBadgePermission: false,
|
|
requestSoundPermission: false,
|
|
);
|
|
|
|
Future<void> _start() async {
|
|
try {
|
|
await _plugin.initialize(
|
|
settings: const InitializationSettings(
|
|
android: AndroidInitializationSettings('@mipmap/ic_launcher'),
|
|
iOS: _darwin,
|
|
macOS: _darwin,
|
|
),
|
|
onDidReceiveNotificationResponse: (response) {
|
|
final id = response.id;
|
|
if (id == null) return;
|
|
_ids.removeWhere((_, posted) => posted == id);
|
|
_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';
|
|
} else if (Platform.isMacOS) {
|
|
final options = await _plugin
|
|
.resolvePlatformSpecificImplementation<
|
|
MacOSFlutterLocalNotificationsPlugin
|
|
>()
|
|
?.checkPermissions();
|
|
_permission = options?.isEnabled == true ? 'granted' : 'default';
|
|
}
|
|
}
|
|
|
|
@override
|
|
NotificationEnvironment environment() => NotificationEnvironment(
|
|
supported: Platform.isAndroid || Platform.isIOS || Platform.isMacOS,
|
|
// 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);
|
|
} else if (Platform.isMacOS) {
|
|
granted = await _plugin
|
|
.resolvePlatformSpecificImplementation<
|
|
MacOSFlutterLocalNotificationsPlugin
|
|
>()
|
|
?.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;
|
|
// The same tag reuses its platform id, so the new one replaces the old.
|
|
final id = _ids[message.tag] ??= _nextId++;
|
|
if (onTap != null) {
|
|
_taps[id] = onTap;
|
|
} else {
|
|
_taps.remove(id);
|
|
}
|
|
_plugin.show(
|
|
id: id,
|
|
title: message.title,
|
|
body: message.body,
|
|
notificationDetails: const NotificationDetails(
|
|
android: _channel,
|
|
iOS: DarwinNotificationDetails(),
|
|
macOS: DarwinNotificationDetails(),
|
|
),
|
|
payload: message.tag,
|
|
);
|
|
return true;
|
|
}
|
|
|
|
@override
|
|
void close(String tag) {
|
|
final id = _ids.remove(tag);
|
|
if (id == null || !_started) return;
|
|
_taps.remove(id);
|
|
// Removes the delivered notification (Android shade, Darwin centre).
|
|
_plugin.cancel(id: id).catchError((Object _) {});
|
|
}
|
|
}
|
|
|
|
NotificationBridge createNotificationBridge() => NativeNotificationBridge();
|