import 'dart:io' show Platform; import 'package:flutter/services.dart'; import '../utils/logger.dart'; /// Канал к нативному сервису FKM (foreground qlyra messaging). class FkmBridge { FkmBridge._(); static final FkmBridge instance = FkmBridge._(); static const _method = MethodChannel('ru.qlyra.app/fkm'); VoidCallback? _onDisabled; bool _handlerSet = false; bool get isSupported { try { return Platform.isAndroid; } catch (_) { return false; } } /// Вызывается, когда пользователь выключил FKM кнопкой в самом уведомлении. void setDisabledCallback(VoidCallback callback) { _onDisabled = callback; if (_handlerSet || !isSupported) return; _handlerSet = true; _method.setMethodCallHandler((call) async { if (call.method == 'disabled') _onDisabled?.call(); return null; }); } Future isEnabled() async { if (!isSupported) return false; return await _invoke('isEnabled') ?? false; } Future setEnabled(bool enabled) => _invoke('setEnabled', {'enabled': enabled}); Future setConnected(bool connected) => _invoke('setConnected', {'connected': connected}); Future showMessage(Map data) => _invoke('showMessage', {'data': data}); Future showCall(Map data) => _invoke('showCall', {'data': data}); Future editMessage(Map data) => _invoke('editMessage', {'data': data}); Future removeMessage(Map data) => _invoke('removeMessage', {'data': data}); Future hasNotificationPermission() async { if (!isSupported) return false; return await _invoke('hasNotificationPermission') ?? false; } Future requestNotificationPermission() async { if (!isSupported) return false; return await _invoke('requestNotificationPermission') ?? false; } Future isIgnoringBatteryOptimizations() async { if (!isSupported) return true; return await _invoke('isIgnoringBatteryOptimizations') ?? true; } Future requestIgnoreBatteryOptimizations() => _invoke('requestIgnoreBatteryOptimizations'); Future _invoke(String method, [Map? args]) async { if (!isSupported) return null; try { return await _method.invokeMethod(method, args); } catch (e) { logger.w('FkmBridge.$method: $e'); return null; } } }