import 'dart:io'; import 'package:flutter/foundation.dart'; 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, and the Linux desktop's notification daemon (the /// freedesktop D-Bus service: mako, dunst, GNOME Shell, Plasma). [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 _taps = {}; /// The platform id each shown tag was posted with, so [close] can remove it. final Map _ids = {}; 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, ); static const _darwin = DarwinInitializationSettings( requestAlertPermission: false, requestBadgePermission: false, requestSoundPermission: false, ); /// The bundle's icon (installed by the Linux runner's CMake beside /// `flutter_assets`), so a portable, uninstalled bundle has one too. static final _linuxSettings = LinuxInitializationSettings( defaultActionName: 'Open', defaultIcon: FilePathLinuxIcon( '${File(Platform.resolvedExecutable).parent.path}/data/app_icon.png', ), ); /// The Linux app, by its target platform rather than the host's, so a test /// on a Linux machine still sees the (Android) test platform. static bool get _linux => defaultTargetPlatform == TargetPlatform.linux; Future _start() async { try { await _plugin.initialize( settings: InitializationSettings( android: AndroidInitializationSettings('@mipmap/ic_launcher'), iOS: _darwin, macOS: _darwin, linux: _linuxSettings, ), 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 _refresh() async { if (_linux) { // A notification daemon asks no permission. _permission = 'granted'; } else 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 || _linux, // 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); } 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();