Skip to content

Commit

Permalink
Update iOS Login Logic
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacakakpo1 committed Aug 1, 2024
1 parent e5e2727 commit 7327a38
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 103 deletions.
66 changes: 33 additions & 33 deletions .flutter-plugins
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
# This is a generated file; do not edit or check into version control.
assets_audio_player=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\assets_audio_player-3.1.1\\
assets_audio_player_web=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\assets_audio_player_web-3.1.1\\
audioplayers=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\audioplayers-5.2.1\\
audioplayers_android=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\audioplayers_android-4.0.3\\
audioplayers_darwin=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\audioplayers_darwin-5.0.2\\
audioplayers_linux=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\audioplayers_linux-3.1.0\\
audioplayers_web=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\audioplayers_web-4.1.0\\
audioplayers_windows=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\audioplayers_windows-3.1.0\\
firebase_core=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\firebase_core-3.2.0\\
firebase_core_web=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\firebase_core_web-2.17.3\\
firebase_messaging=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\firebase_messaging-15.0.3\\
firebase_messaging_web=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\firebase_messaging_web-3.8.11\\
flutter_callkit_incoming=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\flutter_callkit_incoming-2.0.4+1\\
flutter_local_notifications=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\flutter_local_notifications-16.3.3\\
flutter_local_notifications_linux=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\flutter_local_notifications_linux-4.0.1\\
flutter_webrtc=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\flutter_webrtc-0.11.5\\
fluttertoast=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\fluttertoast-8.2.6\\
path_provider=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\path_provider-2.1.3\\
path_provider_android=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\path_provider_android-2.2.7\\
path_provider_foundation=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\path_provider_foundation-2.4.0\\
path_provider_linux=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\path_provider_linux-2.2.1\\
path_provider_windows=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\path_provider_windows-2.3.0\\
permission_handler=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\permission_handler-11.3.1\\
permission_handler_android=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\permission_handler_android-12.0.7\\
permission_handler_apple=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\permission_handler_apple-9.4.5\\
permission_handler_html=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\permission_handler_html-0.1.2\\
permission_handler_windows=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\permission_handler_windows-0.2.1\\
shared_preferences=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\shared_preferences-2.2.3\\
shared_preferences_android=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\shared_preferences_android-2.2.3\\
shared_preferences_foundation=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\shared_preferences_foundation-2.4.0\\
shared_preferences_linux=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\shared_preferences_linux-2.3.2\\
shared_preferences_web=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\shared_preferences_web-2.3.0\\
shared_preferences_windows=C:\\Users\\isaac\\AppData\\Local\\Pub\\Cache\\hosted\\pub.dev\\shared_preferences_windows-2.3.2\\
assets_audio_player=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/assets_audio_player-3.1.1/
assets_audio_player_web=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/assets_audio_player_web-3.1.1/
audioplayers=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/audioplayers-5.2.1/
audioplayers_android=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/audioplayers_android-4.0.3/
audioplayers_darwin=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/audioplayers_darwin-5.0.2/
audioplayers_linux=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/audioplayers_linux-3.1.0/
audioplayers_web=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/audioplayers_web-4.1.0/
audioplayers_windows=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/audioplayers_windows-3.1.0/
firebase_core=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/firebase_core-3.3.0/
firebase_core_web=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/firebase_core_web-2.17.4/
firebase_messaging=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/firebase_messaging-15.0.4/
firebase_messaging_web=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/firebase_messaging_web-3.8.12/
flutter_callkit_incoming=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/flutter_callkit_incoming-2.0.4+1/
flutter_local_notifications=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/flutter_local_notifications-16.3.3/
flutter_local_notifications_linux=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/flutter_local_notifications_linux-4.0.1/
flutter_webrtc=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/flutter_webrtc-0.11.5/
fluttertoast=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/fluttertoast-8.2.6/
path_provider=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/path_provider-2.1.4/
path_provider_android=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/path_provider_android-2.2.9/
path_provider_foundation=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/path_provider_foundation-2.4.0/
path_provider_linux=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/path_provider_linux-2.2.1/
path_provider_windows=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/path_provider_windows-2.3.0/
permission_handler=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/permission_handler-11.3.1/
permission_handler_android=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/permission_handler_android-12.0.7/
permission_handler_apple=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/permission_handler_apple-9.4.5/
permission_handler_html=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/permission_handler_html-0.1.2/
permission_handler_windows=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/permission_handler_windows-0.2.1/
shared_preferences=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/shared_preferences-2.3.0/
shared_preferences_android=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/shared_preferences_android-2.3.0/
shared_preferences_foundation=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/shared_preferences_foundation-2.5.0/
shared_preferences_linux=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/shared_preferences_linux-2.4.0/
shared_preferences_web=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/shared_preferences_web-2.4.0/
shared_preferences_windows=/Users/isaacakakpo/.pub-cache/hosted/pub.dev/shared_preferences_windows-2.4.0/
2 changes: 1 addition & 1 deletion .flutter-plugins-dependencies

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions .lh/packages/telnyx_webrtc/lib/tx_socket.dart.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"sourceFile": "packages/telnyx_webrtc/lib/tx_socket.dart",
"activeCommit": 0,
"commits": [
{
"activePatchIndex": 1,
"patches": [
{
"date": 1722541750187,
"content": "Index: \n===================================================================\n--- \n+++ \n"
},
{
"date": 1722542818509,
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -15,9 +15,9 @@\n late OnOpenCallback onOpen;\n late OnMessageCallback onMessage;\n late OnCloseCallback onClose;\n \n- Future<void> connect() async {\n+ void connect() async {\n try {\n print(\"TxSocket :: connect : $hostAddress\");\n _socket = await WebSocket.connect(hostAddress);\n _logger.i('Connecting to $hostAddress');\n"
}
],
"date": 1722541750187,
"name": "Commit-0",
"content": "import 'dart:io';\nimport 'package:logger/logger.dart';\n\ntypedef OnMessageCallback = void Function(dynamic msg);\ntypedef OnCloseCallback = void Function(int code, String reason);\ntypedef OnOpenCallback = void Function();\n\nclass TxSocket {\n TxSocket(this.hostAddress);\n\n String hostAddress;\n final _logger = Logger();\n\n late WebSocket _socket;\n late OnOpenCallback onOpen;\n late OnMessageCallback onMessage;\n late OnCloseCallback onClose;\n\n Future<void> connect() async {\n try {\n print(\"TxSocket :: connect : $hostAddress\");\n _socket = await WebSocket.connect(hostAddress);\n _logger.i('Connecting to $hostAddress');\n _socket.pingInterval = const Duration(seconds: 10);\n _socket.timeout(const Duration(seconds: 30));\n onOpen.call();\n _socket.listen((dynamic data) {\n onMessage.call(data);\n }, onDone: () {\n onClose.call(_socket.closeCode ?? 0,\n _socket.closeReason ?? \"Closed for unknown reason\");\n });\n } catch (e) {\n onClose.call(500, e.toString());\n }\n }\n\n void send(dynamic data) {\n _socket.add(data);\n _logger.i('TxSocket :: send : \\n\\n$data');\n }\n\n void close() {\n _socket.close();\n }\n}\n"
}
]
}
37 changes: 21 additions & 16 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import 'package:telnyx_webrtc/model/socket_method.dart';
final logger = Logger();
final mainViewModel = MainViewModel();
const MOCK_USER = "<UserName>";
const MOCK_PASSWORD = "<Password>";
const MOCK_PASSWORD = "Password";
// Android Only - Push Notifications
@pragma('vm:entry-point')
Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
Expand Down Expand Up @@ -86,7 +86,7 @@ Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async {

logger.i("iOS notification token :: $token");
}
var credentialConfig = CredentialConfig(MOCK_USER,MOCK_PASSWORD,
var credentialConfig = CredentialConfig(MOCK_USER, MOCK_PASSWORD,
"<caller_id>", "<caller_number>", token, true, "", "");
telnyxClient.handlePushNotification(
pushMetaData, credentialConfig, null);
Expand Down Expand Up @@ -218,7 +218,6 @@ Future<void> main() async {
}

Future<void> askForNotificationPermission() async {

FlutterCallkitIncoming.requestNotificationPermission("notification");
var status = await Permission.notification.status;
if (status.isDenied) {
Expand All @@ -230,10 +229,11 @@ Future<void> askForNotificationPermission() async {
if (await Permission.location.isRestricted) {
// The OS restricts access, for example, because of parental controls.
}

}

Future<void> handlePush(Map<dynamic, dynamic> data) async {
mainViewModel.callFromPush = true;

logger.i("Handle Push Init");
String? token;

Expand All @@ -249,7 +249,6 @@ Future<void> handlePush(Map<dynamic, dynamic> data) async {
}
var credentialConfig = CredentialConfig(MOCK_USER, MOCK_PASSWORD,
"<caller_id>", "<caller_number>", token, true, "", "");
mainViewModel.callFromPush = true;
mainViewModel.handlePushNotification(pushMetaData!, credentialConfig, null);
mainViewModel.observeResponses();
logger.i('actionCallIncoming :: Received Incoming Call! Handle Push');
Expand Down Expand Up @@ -280,17 +279,23 @@ class _MyAppState extends State<MyApp> {
});
}

// Handle Push when app comes from background :: Only for Android
TelnyxClient.getPushData().then((data) {
// whenever you open the app from the terminate state by clicking on Notification message,
if (data != null) {
handlePush(data);
print("getPushData : getInitialMessage :: Notification Message: $data");
} else {
mainViewModel.connect();
print("getPushData : No data");
}
});
if (Platform.isAndroid) {
// Handle Push when app comes from background :: Only for Android
TelnyxClient.getPushData().then((data) {
// whenever you open the app from the terminate state by clicking on Notification message,
if (data != null) {
handlePush(data);
print(
"getPushData : getInitialMessage :: Notification Message: $data");
} else {
mainViewModel.connect();
print("getPushData : No data");
}
});
} else if (Platform.isIOS && !mainViewModel.callFromPush) {
logger.i("iOS :: connect");
mainViewModel.connect();
}
}

// This widget is the root of your application.
Expand Down
6 changes: 3 additions & 3 deletions lib/main_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ class MainViewModel with ChangeNotifier {
case SocketMethod.CLIENT_READY:
{
_registered = true;
logger.i("Registered :: $_registered");
break;
}
case SocketMethod.INVITE:
{

observeCurrentCall();

_incomingInvite = message.message.inviteParams;
Expand Down Expand Up @@ -196,7 +196,7 @@ class MainViewModel with ChangeNotifier {
notifyListeners();
}

void login(CredentialConfig credentialConfig) {
void login(CredentialConfig credentialConfig) async {
_localName = credentialConfig.sipCallerIDName;
_localNumber = credentialConfig.sipCallerIDNumber;
_telnyxClient.credentialLogin(credentialConfig);
Expand Down Expand Up @@ -232,7 +232,7 @@ class MainViewModel with ChangeNotifier {
}

// Hide if not already hidden
if(Platform.isAndroid && !acceptFromNotification) {
if (Platform.isAndroid && !acceptFromNotification) {
CallKitParams callKitParams = CallKitParams(
id: _incomingInvite!.callID,
nameCaller: _incomingInvite!.callerIdName,
Expand Down
1 change: 0 additions & 1 deletion lib/view/screen/login_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ class _LoginScreenState extends State<LoginScreen> with WidgetsBindingObserver {
Widget build(BuildContext context) {
Provider.of<MainViewModel>(context, listen: true).observeResponses();


bool registered =
Provider.of<MainViewModel>(context, listen: true).registered;
if (registered) {
Expand Down
109 changes: 60 additions & 49 deletions packages/telnyx_webrtc/lib/telnyx_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -206,43 +206,40 @@ class TelnyxClient {
void _connectWithCallBack(
PushMetaData? pushMetaData, OnOpenCallback openCallback) {
_logger.i('connect() ${pushMetaData?.toJson()}');
if (pushMetaData?.voice_sdk_id != null) {
try {
if (pushMetaData?.voice_sdk_id != null) {
txSocket.hostAddress =
"$_storedHostAddress?voice_sdk_id=${pushMetaData?.voice_sdk_id}";
_pushMetaData = pushMetaData;
_logger.i(
'Connecting to WebSocket with voice_sdk_id :: ${pushMetaData?.voice_sdk_id}');
print("Connecting to WebSocket :: ${txSocket.hostAddress}");
} else {
txSocket.hostAddress = _storedHostAddress;
_logger.i('connecting to WebSocket $_storedHostAddress');
}
txSocket.connect();

txSocket.onOpen = () {
_closed = false;
_connected = true;
_logger.i('Web Socket is now connected');
_onOpen();
openCallback.call();
};

txSocket.onMessage = (dynamic data) {
_onMessage(data);
};

txSocket.onClose = (int closeCode, String closeReason) {
_logger.i('Closed [$closeCode, $closeReason]!');
_connected = false;
_onClose(true, closeCode, closeReason);
};
} catch (e, s) {
_logger.e(e.toString(), null, s);
_connected = false;
_logger.e('WebSocket $_storedHostAddress error: $e');
try {
if (pushMetaData?.voice_sdk_id != null) {
txSocket.hostAddress =
"$_storedHostAddress?voice_sdk_id=${pushMetaData?.voice_sdk_id}";
_logger.i(
'Connecting to WebSocket with voice_sdk_id :: ${pushMetaData?.voice_sdk_id}');
print("Connecting to WebSocket :: ${txSocket.hostAddress}");
} else {
txSocket.hostAddress = _storedHostAddress;
_logger.i('connecting to WebSocket $_storedHostAddress');
}
txSocket.connect();

txSocket.onOpen = () {
_closed = false;
_connected = true;
_logger.i('Web Socket is now connected');
_onOpen();
openCallback.call();
};

txSocket.onMessage = (dynamic data) {
_onMessage(data);
};

txSocket.onClose = (int closeCode, String closeReason) {
_logger.i('Closed [$closeCode, $closeReason]!');
_connected = false;
_onClose(true, closeCode, closeReason);
};
} catch (e, s) {
_logger.e(e.toString(), null, s);
_connected = false;
_logger.e('WebSocket $_storedHostAddress error: $e');
}
}

Expand All @@ -254,7 +251,7 @@ class TelnyxClient {
}
_logger.i('connecting to WebSocket $_storedHostAddress');
try {
if(_pushMetaData != null){
if (_pushMetaData != null) {
txSocket.hostAddress =
"$_storedHostAddress?voice_sdk_id=${_pushMetaData?.voice_sdk_id}";
_logger.i(
Expand Down Expand Up @@ -331,7 +328,7 @@ class TelnyxClient {
/*
* initialise this callback to handle call state changes on the client side
* */
print("Call state not overridden :Call State Changed to $state");
print("Call state not overridden :Call State Changed to $state");
_logger.i('Call state not overridden :Call State Changed to $state');
}), _callEnded);
return _call!;
Expand Down Expand Up @@ -374,8 +371,13 @@ class TelnyxClient {
jsonrpc: JsonRPCConstant.jsonrpc);

String jsonLoginMessage = jsonEncode(loginMessage);

txSocket.send(jsonLoginMessage);
if (isConnected()) {
txSocket.send(jsonLoginMessage);
} else {
_connectWithCallBack(null, () {
txSocket.send(jsonLoginMessage);
});
}
}

/// Uses the provided [config] to send a token login message to the Telnyx backend.
Expand Down Expand Up @@ -414,7 +416,13 @@ class TelnyxClient {

String jsonLoginMessage = jsonEncode(loginMessage);
_logger.i('Token Login Message $jsonLoginMessage');
txSocket.send(jsonLoginMessage);
if (isConnected()) {
txSocket.send(jsonLoginMessage);
} else {
_connectWithCallBack(null, () {
txSocket.send(jsonLoginMessage);
});
}
}

// Creates an invitation to send to a [destinationNumber] or SIP Destination
Expand Down Expand Up @@ -487,7 +495,7 @@ class TelnyxClient {
void updateCall(Call call) {
if (calls.containsKey(call.callId)) {
calls[call.callId!] = call;
}else{
} else {
calls[call.callId!] = call;
}
}
Expand Down Expand Up @@ -604,18 +612,21 @@ class TelnyxClient {
defaultTargetPlatform == TargetPlatform.android
? "android"
: "ios";
String pushEnvironment = kDebugMode ? "development" : "production";
String pushEnvironment =
kDebugMode ? "development" : "production";
AttachCallMessage attachCallMessage = AttachCallMessage(
method: SocketMethod.ATTACH_CALL,
id: const Uuid().v4(),
params: Params(
userVariables: <dynamic, dynamic>{
"push_notification_environment": pushEnvironment,
"push_notification_provider": platform,
},
),
userVariables: <dynamic, dynamic>{
"push_notification_environment":
pushEnvironment,
"push_notification_provider": platform,
},
),
jsonrpc: "2.0");
print("attachCallMessage :: ${attachCallMessage.toJson()}");
print(
"attachCallMessage :: ${attachCallMessage.toJson()}");
txSocket.send(jsonEncode(attachCallMessage));
_isCallFromPush = false;
}
Expand Down

0 comments on commit 7327a38

Please sign in to comment.