import 'dart:convert'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter_callkit_incoming/entities/entities.dart'; import 'package:flutter_callkit_incoming/flutter_callkit_incoming.dart'; import 'package:mohem_flutter_app/app_state/app_state.dart'; import 'package:mohem_flutter_app/classes/utils.dart'; import 'package:mohem_flutter_app/main.dart'; import 'package:mohem_flutter_app/models/chat/get_single_user_chat_list_model.dart'; import 'package:mohem_flutter_app/models/chat/get_user_login_token_model.dart' as ALM; class ChatVoipCall { static final ChatVoipCall _instance = ChatVoipCall._internal(); ChatVoipCall._internal(); factory ChatVoipCall() => _instance; Future showCallkitIncoming({required String uuid, required RemoteMessage data}) async { await ChatVoipCall().listenerEvent(); SingleUserChatModel callerData = SingleUserChatModel.fromJson(jsonDecode(data.data["user_chat_history_response"])); ALM.Response autoLoginData = ALM.Response.fromJson(jsonDecode(data.data["user_token_response"])); var values = { "loginDetails": autoLoginData.toJson(), "callerDetails": callerData.toJson(), }; logger.d(values); CallKitParams params = CallKitParams( id: uuid, nameCaller: callerData.targetUserName, appName: 'Mohemm', handle: '', type: 0, duration: 25000, textAccept: 'Accept', textDecline: 'Decline', textMissedCall: 'Missed call', textCallback: 'Call back', extra: values, android: const AndroidParams( isCustomNotification: true, isShowLogo: true, isShowCallback: false, isShowMissedCallNotification: true, ringtonePath: 'system_ringtone_default', backgroundColor: '#0955fa', backgroundUrl: 'assets/test.png', actionColor: '#4CAF50', ), ios: IOSParams( iconName: 'Mohemm', handleType: '', supportsVideo: true, maximumCallGroups: 2, maximumCallsPerCallGroup: 1, audioSessionMode: 'default', audioSessionActive: true, audioSessionPreferredSampleRate: 38000.0, audioSessionPreferredIOBufferDuration: 0.005, supportsDTMF: true, supportsHolding: true, supportsGrouping: false, supportsUngrouping: false, ringtonePath: 'system_ringtone_default', ), ); if (callerData.chatEventId == 3) { await Utils.saveStringFromPrefs("isIncomingCall", "true"); await Utils.saveStringFromPrefs("inComingCallData",jsonEncode(params.toJson())); await FlutterCallkitIncoming.showCallkitIncoming(params); } } //Function(CallEvent) callback Future listenerEvent() async { try { FlutterCallkitIncoming.onEvent.listen((event) async { switch (event!.event) { case Event.ACTION_CALL_INCOMING: // TODO: received an incoming call break; case Event.ACTION_CALL_START: // TODO: started an outgoing call // TODO: show screen calling in Flutter break; case Event.ACTION_CALL_ACCEPT: break; case Event.ACTION_CALL_DECLINE: Utils.saveStringFromPrefs("isIncomingCall", "false"); Utils.saveStringFromPrefs("inComingCallData", "null"); break; case Event.ACTION_CALL_ENDED: Utils.saveStringFromPrefs("isIncomingCall", "false"); Utils.saveStringFromPrefs("inComingCallData", "null"); // TODO: ended an incoming/outgoing call break; case Event.ACTION_CALL_TIMEOUT: Utils.saveStringFromPrefs("isIncomingCall", "false"); Utils.saveStringFromPrefs("inComingCallData", "null"); break; case Event.ACTION_CALL_CALLBACK: // TODO: only Android - click action `Call back` from missed call notification break; case Event.ACTION_CALL_TOGGLE_HOLD: // TODO: only iOS break; case Event.ACTION_CALL_TOGGLE_MUTE: // TODO: only iOS break; case Event.ACTION_CALL_TOGGLE_DMTF: // TODO: only iOS break; case Event.ACTION_CALL_TOGGLE_GROUP: // TODO: only iOS break; case Event.ACTION_CALL_TOGGLE_AUDIO_SESSION: // TODO: only iOS break; case Event.ACTION_DID_UPDATE_DEVICE_PUSH_TOKEN_VOIP: // TODO: only iOS break; } }); } on Exception {} } }