feat: integrate flutter_local_notifications for enhanced voice call notifications

- Added flutter_local_notifications dependency to manage notifications during voice calls.
- Implemented notification handling in VoiceCallService to update call status and manage user interactions.
- Enabled wake lock functionality to keep the screen on during calls and prevent audio interruptions.
- Updated AndroidManifest.xml to include necessary permissions for Bluetooth and foreground services.
- Enhanced notification actions to allow users to mute, unmute, or end calls directly from notifications.
This commit is contained in:
cogwheel0
2025-10-09 00:01:35 +05:30
parent c99682d6a3
commit e98f5cbf0f
9 changed files with 400 additions and 2 deletions

View File

@@ -0,0 +1,225 @@
import 'dart:io';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
/// Service to manage persistent notifications for voice calls
class VoiceCallNotificationService {
static final VoiceCallNotificationService _instance =
VoiceCallNotificationService._internal();
factory VoiceCallNotificationService() => _instance;
VoiceCallNotificationService._internal();
final FlutterLocalNotificationsPlugin _notifications =
FlutterLocalNotificationsPlugin();
bool _initialized = false;
// Notification IDs and channels
static const String _channelId = 'voice_call_channel';
static const String _channelName = 'Voice Call';
static const String _channelDescription =
'Ongoing voice call notifications';
static const int _notificationId = 2001;
// Action IDs
static const String _actionMute = 'mute_call';
static const String _actionUnmute = 'unmute_call';
static const String _actionEndCall = 'end_call';
// Callback for handling notification actions
void Function(String action)? onActionPressed;
Future<void> initialize() async {
if (_initialized) return;
const androidSettings = AndroidInitializationSettings('@mipmap/ic_launcher');
const iosSettings = DarwinInitializationSettings(
requestAlertPermission: false,
requestBadgePermission: false,
requestSoundPermission: false,
);
const settings = InitializationSettings(
android: androidSettings,
iOS: iosSettings,
);
await _notifications.initialize(
settings,
onDidReceiveNotificationResponse: _handleNotificationResponse,
onDidReceiveBackgroundNotificationResponse:
_handleBackgroundNotificationResponse,
);
// Create notification channel for Android
if (Platform.isAndroid) {
await _createAndroidNotificationChannel();
}
_initialized = true;
}
Future<void> _createAndroidNotificationChannel() async {
const androidChannel = AndroidNotificationChannel(
_channelId,
_channelName,
description: _channelDescription,
importance: Importance.high,
playSound: false,
enableVibration: false,
showBadge: false,
);
await _notifications
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(androidChannel);
}
void _handleNotificationResponse(NotificationResponse response) {
final action = response.actionId;
if (action != null && onActionPressed != null) {
onActionPressed!(action);
}
}
@pragma('vm:entry-point')
static void _handleBackgroundNotificationResponse(
NotificationResponse response,
) {
// Background action handling
// Note: This runs in an isolate, so we can't directly call instance methods
// Actions will be handled when app returns to foreground
}
/// Show ongoing voice call notification
Future<void> showCallNotification({
required String modelName,
required bool isMuted,
required bool isSpeaking,
}) async {
if (!_initialized) {
print('VoiceCallNotification: Initializing...');
await initialize();
}
print('VoiceCallNotification: Showing notification for $modelName (muted: $isMuted, speaking: $isSpeaking)');
final status = isSpeaking ? 'Speaking...' : 'Listening...';
final muteAction = isMuted ? 'Unmute' : 'Mute';
final muteActionId = isMuted ? _actionUnmute : _actionMute;
if (Platform.isAndroid) {
final androidDetails = AndroidNotificationDetails(
_channelId,
_channelName,
channelDescription: _channelDescription,
importance: Importance.high,
priority: Priority.high,
ongoing: true,
autoCancel: false,
playSound: false,
enableVibration: false,
showWhen: true,
usesChronometer: true,
chronometerCountDown: false,
category: AndroidNotificationCategory.call,
visibility: NotificationVisibility.public,
icon: '@mipmap/ic_launcher',
colorized: false,
actions: <AndroidNotificationAction>[
AndroidNotificationAction(
muteActionId,
muteAction,
icon: DrawableResourceAndroidBitmap(
isMuted ? '@drawable/ic_mic_on' : '@drawable/ic_mic_off',
),
showsUserInterface: false,
cancelNotification: false,
),
AndroidNotificationAction(
_actionEndCall,
'End Call',
icon: DrawableResourceAndroidBitmap('@drawable/ic_call_end'),
showsUserInterface: true,
cancelNotification: true,
),
],
);
await _notifications.show(
_notificationId,
'Voice Call with $modelName',
status,
NotificationDetails(android: androidDetails),
);
} else if (Platform.isIOS) {
// iOS doesn't support action buttons for ongoing notifications
// Use a simpler persistent notification
const iosDetails = DarwinNotificationDetails(
presentAlert: true,
presentBadge: false,
presentSound: false,
interruptionLevel: InterruptionLevel.timeSensitive,
);
await _notifications.show(
_notificationId,
'Voice Call with $modelName',
status,
const NotificationDetails(iOS: iosDetails),
);
}
}
/// Update notification status
Future<void> updateCallStatus({
required String modelName,
required bool isMuted,
required bool isSpeaking,
}) async {
await showCallNotification(
modelName: modelName,
isMuted: isMuted,
isSpeaking: isSpeaking,
);
}
/// Cancel the voice call notification
Future<void> cancelNotification() async {
await _notifications.cancel(_notificationId);
}
/// Check if notifications are enabled
Future<bool> areNotificationsEnabled() async {
if (Platform.isAndroid) {
final androidImpl = _notifications.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>();
return await androidImpl?.areNotificationsEnabled() ?? false;
} else if (Platform.isIOS) {
// iOS doesn't have a direct check, assume enabled if initialized
return _initialized;
}
return false;
}
/// Request notification permissions
Future<bool> requestPermissions() async {
if (Platform.isAndroid) {
final androidImpl = _notifications.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>();
final granted = await androidImpl?.requestNotificationsPermission();
return granted ?? false;
} else if (Platform.isIOS) {
final iosImpl = _notifications
.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>();
final granted = await iosImpl?.requestPermissions(
alert: true,
badge: false,
sound: false,
);
return granted ?? false;
}
return false;
}
}

View File

@@ -1,12 +1,14 @@
import 'dart:async';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:wakelock_plus/wakelock_plus.dart';
import '../../../core/providers/app_providers.dart';
import '../../../core/services/socket_service.dart';
import '../providers/chat_providers.dart';
import 'text_to_speech_service.dart';
import 'voice_input_service.dart';
import 'voice_call_notification_service.dart';
part 'voice_call_service.g.dart';
@@ -25,13 +27,17 @@ class VoiceCallService {
final TextToSpeechService _tts;
final SocketService _socketService;
final Ref _ref;
final VoiceCallNotificationService _notificationService =
VoiceCallNotificationService();
VoiceCallState _state = VoiceCallState.idle;
String? _sessionId;
String? _activeConversationId;
StreamSubscription<String>? _transcriptSubscription;
StreamSubscription<int>? _intensitySubscription;
String _accumulatedTranscript = '';
bool _isDisposed = false;
bool _isMuted = false;
SocketEventSubscription? _socketSubscription;
final StreamController<VoiceCallState> _stateController =
@@ -57,6 +63,9 @@ class VoiceCallService {
onComplete: _handleTtsComplete,
onError: _handleTtsError,
);
// Set up notification action handler
_notificationService.onActionPressed = _handleNotificationAction;
}
VoiceCallState get state => _state;
@@ -68,6 +77,16 @@ class VoiceCallService {
Future<void> initialize() async {
if (_isDisposed) return;
// Initialize notification service
await _notificationService.initialize();
// Request notification permissions if needed
final notificationsEnabled =
await _notificationService.areNotificationsEnabled();
if (!notificationsEnabled) {
await _notificationService.requestPermissions();
}
// Initialize voice input
final voiceInitialized = await _voiceInput.initialize();
if (!voiceInitialized) {
@@ -97,8 +116,15 @@ class VoiceCallService {
if (_isDisposed) return;
try {
// Set conversation ID first before updating state
_activeConversationId = conversationId;
// Update state (this will trigger notification)
_updateState(VoiceCallState.connecting);
// Enable wake lock to keep screen on and prevent audio interruption
await WakelockPlus.enable();
// Ensure socket connection
await _socketService.ensureConnected();
_sessionId = _socketService.sessionId;
@@ -119,6 +145,8 @@ class VoiceCallService {
await _startListening();
} catch (e) {
_updateState(VoiceCallState.error);
await WakelockPlus.disable();
await _notificationService.cancelNotification();
rethrow;
}
}
@@ -298,8 +326,16 @@ class VoiceCallService {
await _voiceInput.stopListening();
await _tts.stop();
// Cancel notification
await _notificationService.cancelNotification();
// Disable wake lock when call ends
await WakelockPlus.disable();
_sessionId = null;
_activeConversationId = null;
_accumulatedTranscript = '';
_isMuted = false;
_updateState(VoiceCallState.disconnected);
}
@@ -326,6 +362,60 @@ class VoiceCallService {
if (_isDisposed) return;
_state = newState;
_stateController.add(newState);
// Update notification when state changes (fire and forget)
_updateNotification().catchError((e) {
// Ignore notification errors
});
}
Future<void> _updateNotification() async {
// Skip notification for idle, error, and disconnected states
if (_state == VoiceCallState.idle ||
_state == VoiceCallState.error ||
_state == VoiceCallState.disconnected) {
print('VoiceCall: Skipping notification - state: $_state');
return;
}
try {
final selectedModel = _ref.read(selectedModelProvider);
final modelName = selectedModel?.name ?? 'Assistant';
print('VoiceCall: Updating notification - model: $modelName, muted: $_isMuted, state: $_state');
await _notificationService.updateCallStatus(
modelName: modelName,
isMuted: _isMuted,
isSpeaking: _state == VoiceCallState.speaking,
);
} catch (e) {
print('VoiceCall: Failed to update notification: $e');
}
}
void _handleNotificationAction(String action) {
switch (action) {
case 'mute_call':
_toggleMute();
break;
case 'unmute_call':
_toggleMute();
break;
case 'end_call':
stopCall();
break;
}
}
void _toggleMute() {
_isMuted = !_isMuted;
if (_isMuted) {
pauseListening();
} else {
resumeListening();
}
_updateNotification();
}
Future<void> dispose() async {
@@ -338,6 +428,12 @@ class VoiceCallService {
_voiceInput.dispose();
await _tts.dispose();
// Cancel notification
await _notificationService.cancelNotification();
// Ensure wake lock is disabled on dispose
await WakelockPlus.disable();
await _stateController.close();
await _transcriptController.close();
await _responseController.close();