118 lines
3.4 KiB
Dart
118 lines
3.4 KiB
Dart
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();
|