Skip to main content
During this guide you will learn how to setup and use the Telnyx RTC Native SDKs into your app to receive calls through push notifications.

Requirements

| iOS | Android |

Build the iOS app

Configure the project

  1. Setup the TelnyxRTC iOS SDK into your project.
  2. Enable the following capabillities into your app:
    • Background mode: Audio, Airplay and Picture in picture
    • Background mode: Voice over IP
    • Push Notifications

Setup the Telnyx client

  1. Import the SDK into your ViewController
  1. Create and instance of the SDK
  1. Setup the delegate

Setup CallKit

Use CallKit to integrate your calling services with other call-related apps on the system. CallKit provides the calling interface, and you handle the back-end communication with your VoIP service. For incoming and outgoing calls, CallKit displays the same interfaces as the Phone app, giving your app a more native look and feel. CallKit also responds appropriately to system-level behaviors such as Do Not Disturb. For more information check the CallKit official documentation.
  1. Setup the CallKit provider and Controller
  1. Implement the CXProviderDelegate

Setup PushKit

If your app provides Voice-over-IP (VoIP) phone services, you may use PushKit to handle incoming calls on user devices. PushKit provides an efficient way to manage calls that doesn’t require your app to be running to receive calls. Upon receiving the notification, the device wakes up your app and gives it time to notify the user and connect to your call service. For more information check the PushKit official documentation.
  1. Init PushKit to receive VoIP push notifications:
  1. Implement the PKPushRegistryDelegate. Inside this delegate you will receive updates from APNS of the device Push Token and the
  1. Inside the didUpdate credentials delegate method it’s required to:
    • Store your APNS token
    • Register the APNS device token into our backend by connecting the TelnyxClient as follows:
  1. Process the incoming push notification. In this step we need to:
    • Decode the push payload
    • Send the push notificaiton payload to the SDK to get the call
    • Register the call into the system using CallKit.

Code flow summarized

  1. Initialize push kit to get an APNS token
  2. Send the APNS token to register the device into Telnyx backend by login in through the SDK using the user’s SIP credentials.
  3. When a PN is received through PushKit, two main actions must be executed: a. Warn the Telnyx SDK about the incoming push notification by calling processVoIPNotification in order to get the call connected. b. Register the incoming call into CallKit. This will trigger the Incoming call system notification to be displayed.
  4. The SDK will fire the delegate method onPushCall with the new instance of the call that can be answered or rejected.

Build the Android app

Configure the project

  1. Setup the TelnyxRTC Android SDK into your project. The SDK is delivered through Jitpack.
  2. Add the following permissions into your project manifest:

Setup the Telnyx client

  1. Initialize the client: To initialize the TelnyxClient you will have to provide the application context. Once an instance is created, you can call the .connect() method to connect to the socket. An error will appear as a socket response if there is no network available:
  1. Logging into Telnyx Client: To log into the Telnyx WebRTC client, you’ll need to authenticate using a Telnyx SIP Connection. Follow this guide to create JWTs (JSON Web Tokens) to authenticate. To log in with a token we use the tokinLogin() method. You can also authenticate directly with the SIP Connection username and password with the credentialLogin() method:
Note: tokenConfig and credentialConfig are data classes that represent login settings for the client to use. They look like this:
  1. Creating a call invitation: In order to make a call invitation, you need to provide your callerName, callerNumber, the destinationNumber (or SIP credential), and your clientState (any String value).
  1. Accepting a call: In order to be able to accept a call, we first need to listen for invitations. We do this by getting the Telnyx Socket Response as LiveData:
We can then use this method to create a listener that listens for an invitation - in this example, we assume getSocketResponse is a method within a ViewModel.
When we receive a call we will receive an InviteResponse data class that contains the details we need to accept the call. We can then call the acceptCall method in TelnyxClient from our ViewModel:

Adding Push Notifications

Logging in with a Push Notification Token

With the token received from the firebase servers, we can now tell Telnyx which device to push notifications to that are associated with your connection. To do so we simply include the firebase token with the initial login message:
Once this log in is successful, and you have received the CLIENT_READY socket message, you have successfully assigned this device to the credential and any calls received while not connected to the socket will result in a Push Notification.

Receiving a Push Notification

In order to actually receive a Push Notification sent to this device, the final step is to implement a MessagingService class which will be able to receive incoming messages sent by Firebase and display them as a notification. First create a class that implements the FirebaseMessagingService() class. Below is a barebones boilerplate implementation:
There are a few methods we can override from this class, but the important ones for this use case are onMessageRecieved, which will fire when we are sent a Push Notification and will contain the contents of the Telnyx Push notification inside the RemoteMessage in the following format:
The other method is the onNewToken method, which fires when our token changes for whatever reason. This new token can be used to re-register. You can use this method to persist tokens with third-party servers.

Displaying a Push Notification

Now that we can receive messages from Firebase, we can flesh out the onMessageReceived override method to show a notification when we receive a messages. Applications that are targeting SDK 6 or above (Android O) must implement notification channels and add its notifications to at lease one of them. The idea behind notification channels is that apps can group different types of notifications into “channels.” Each channel can then be turned on or off by the user. This all happens in the Android settings. For each channel, you can set the visual and auditory behavior that is applied to all notifications in that channel. So first, within onMessageReceived, let’s setup our Telnyx Notification Channel, providing a name, description and some features we would like to include with the channel such as whether or not to vibrate, LED colour of notifications, etc.
Now that we have successfully created a dedicated channel to receive Telnyx related Notifications, next we create two Result Intents for 2 actions on the notification we are going to build. The first is the answerResultIntent, for when the call notification answer button is clicked, and rejectResultIntent, for when the call notification reject button is clicked. You can read more about Result Intents here, but essentially these intents will tell our MainActivity to do something when clicked
Note: you can add certain flags to explain how the interaction with the MainActivity operates. In this case we have added FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP. With these two result intents setup, we can now show a notification with them applied, and when they are clicked we can open the MainActivity and run specific functionality based on the action (ACT_ANSWER_CALL, ACT_REJECT_CALL) contained in each intent. Let’s build or notification, applying an icon, title, content text, priority and vibrate pattern:
Now let’s finally register this new service within the application tags in our manifest so that the methods can be called.
We have now done everything we need to receive a push notification, if we receive a call while not connected to a socket, a notification will appear in our system tray with the details set within the notification builder.

Reacting to a notification

Previously we created two result intents, one for accepting a call and the other to reject the call. How do we turn these intents into functional code that actually does something when pressed? These actions are sent as intents containing actions in our MainActivity when pressed. A rudimentary implementation could be like this within MainActivity:
We are overriding onResume and calling a method to handle call notifications. The handleCallNotification() method grabs the chosen action pressed in the notification, checks which action it is (ACT_ANSWER_CALL or ACT_REJECT_CALL) and does some logic based on the chosen action.