feat(app): native system notifications for runs and chats that need you
This commit is contained in:
@@ -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();
|
||||
Reference in New Issue
Block a user