Telnyx Android Quickstart - the telnyx_common module
The telnyx_common module provides a ready-to-use implementation of the Telnyx WebRTC SDK for Android applications. This module is designed to be a drop-in solution for developers who want to quickly integrate Telnyx’s voice calling capabilities into their Android applications without having to implement the low-level SDK interactions themselves.
The telnyx_common module is currently used in both our Compose and XML sample apps - both of which can be used as references depending on how you are developing your application
Overview
Thetelnyx_common module serves as a bridge between your application and the telnyx_rtc module (the core SDK). It provides:
- A complete implementation of common WebRTC calling features
- User-friendly abstractions for authentication, call management, and notifications
- Ready-to-use UI components and services for handling calls
- Foreground service implementation for maintaining calls when the app is minimized
Key Components
TelnyxViewModel
TheTelnyxViewModel is the primary interface for interacting with the Telnyx WebRTC SDK. It provides methods for authentication, call management, and handling incoming calls.
Authentication Methods
credentialLogin(viewContext, profile, txPushMetaData, autoLogin): Authenticates a user using SIP credentials (username/password)tokenLogin(viewContext, profile, txPushMetaData, autoLogin): Authenticates a user using a SIP tokenconnectWithLastUsedConfig(viewContext, txPushMetaData): Reconnects using the last successful authentication methoddisconnect(viewContext): Disconnects the current session
Call Management Methods
sendInvite(viewContext, destinationNumber): Initiates an outgoing call to the specified numberanswerCall(viewContext, callId, callerIdNumber): Answers an incoming callrejectCall(viewContext, callId): Rejects an incoming callendCall(viewContext): Ends the current active callholdUnholdCurrentCall(viewContext): Toggles hold state for the current calldtmfPressed(key): Sends DTMF tones during an active call
Push Notification Methods
answerIncomingPushCall(viewContext, txPushMetaData): Answers a call received via push notification with the metadata received from the push notificationrejectIncomingPushCall(viewContext, txPushMetaData): Rejects a call received via push notification with the metadata received from the push notificationdisablePushNotifications(context): Disables push notifications for the current device
Profile Management Methods - Saving and Loading Credentials (SIP or Generated) or Tokens
setupProfileList(context): Initializes the list of user profilesaddProfile(context, profile): Adds a new user profiledeleteProfile(context, profile): Deletes an existing user profilesetCurrentConfig(context, profile): Sets the active user profile
CallForegroundService
TheCallForegroundService is a foreground service that keeps your call active when the app is minimized. This is essential for maintaining call audio and preventing the system from killing the call process. The ‘CallForegroundService’ also provides a persistent notification that allows users to control the call directly from the notification. This notification is in line with Android’s guidelines for ongoing calls and uses their official Call Notification style.
Key features:
- Maintains a persistent notification during active calls in the official Call Notification style
- Handles audio priority and routing when the app is in the background. Without this service, the system may kill the call process, causing the call to disconnect or audio to stop
- Provides call control actions directly from the notification
TelnyxCommon.kt which is utilized by the TelnyxViewModel where we set the current call and start the service when a call is active and stop it when the call is ended.
Notifications
The notification folder provides a complete implementation for handling call notifications, including:CallNotificationService
Handles the creation and management of notifications for:- Incoming calls
- Ongoing calls
- Missed calls (When an invitation has been stopped on the callers side)
CallNotificationReceiver
ABroadcastReceiver that handles user interactions with call notifications:
- Answering calls from the notification
- Rejecting calls from the notification
- Ending ongoing calls from the notification
MyFirebaseMessagingService
Handles incoming Firebase Cloud Messaging (FCM) push notifications for calls:- Parses incoming push notifications and extracts the call metadata, which is required when answering or rejecting calls from the notification
- Displays appropriate UI for incoming calls
- Routes call information to the appropriate handlers
Implementation Example
Here’s a very very basic example of how to use thetelnyx_common module in an activity:
Best Practices
- Call Lifecycle Management: Always ensure you properly handle the call lifecycle, especially ending calls when they’re complete to free up resources.
-
Background Processing: Use the
CallForegroundServiceto maintain calls when your app is in the background. -
Error Handling: Observe the
uiStateflow to catch and handle any errors that might occur during calls. - User Permissions: Ensure your app has the necessary permissions (microphone, notifications) before initiating calls.
Troubleshooting
- If calls disconnect when the app is minimized, ensure the
CallForegroundServiceis properly started. - For notification issues, check that notification permissions are granted and channels are properly configured. Also double check the portal to make sure the push credentials are assigned properly.
- If authentication fails, verify the credentials and ensure the device has internet connectivity.
Observe Call State Changes
For eachCall object obtained (e.g., from telnyxClient.newInvite(...) or from an incoming call event), you should observe its callStateFlow to react to state changes:
updateUi and showToast with your actual UI update logic.
Handling Incoming Calls
Recommended approach using SharedFlow: WhenTelnyxClient.socketResponseFlow emits a message with SocketMethod.INVITE, it signifies an incoming call. You would typically display an incoming call UI and provide options to accept or reject.