Skip to content

Commit

Permalink
Is/enhancement/answer push calls (#326)
Browse files Browse the repository at this point in the history
* - Implemented New Attach Calls Method for Push Notifications Calls

- Added `processCallFromPush(txPushIPConfig: TxPushIPConfig)` to be called when device receives Push Notification

- Added `attach_call = true` to LoginParams

* - Updated Process Push Method

* Update for Release v.1.2.4

* Update for Release v.1.2.4

* Update for Release v.1.2.4
  • Loading branch information
isaacakakpo1 authored Sep 8, 2023
1 parent 85666d7 commit dedd0cb
Show file tree
Hide file tree
Showing 16 changed files with 230 additions and 43 deletions.
10 changes: 0 additions & 10 deletions .idea/runConfigurations.xml

This file was deleted.

13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,19 @@ In order to do this you need to:
4. Setup a Push Credential within the Telnyx Portal
5. Generate a Firebase Cloud Messaging instance token
6. Send the token with your login message

Finally, you will need to provide the `connect(..)` method with a `txPushIPConfig` object if the call is from a push notification.
The `txPushIPConfig` is a data class that represents the push notification settings for the client to use. It looks like this:

```kotlin
val txPushIPConfig = TxPushIPConfig(
rtcIP = "",
rtcPort = ""
)

```

The `rtc_port` and `rtc_ip` are the provided in the push notification payload.

For a detailed tutorial, please visit our official [Push Notification Docs](https://developers.telnyx.com/docs/v2/webrtc/push-notifications?type=Android)

Expand Down
12 changes: 12 additions & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
/build
.idea/

# IntelliJ
misc.xml
deploymentTargetDropDown.xml
render.experimental.xml

# Keystore files
*.jks
*.keystore

# Google Services (e.g. APIs or Firebase)
google-services.json
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
id 'com.google.gms.google-services'

}

def localProperties = new Properties()
Expand Down Expand Up @@ -199,4 +200,7 @@ dependencies {

androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation platform('com.google.firebase:firebase-bom:32.2.3')
implementation 'com.google.firebase:firebase-analytics'

}
33 changes: 23 additions & 10 deletions app/src/main/java/com/telnyx/webrtc/sdk/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ import com.telnyx.webrtc.sdk.manager.UserManager
import com.telnyx.webrtc.sdk.model.AudioDevice
import com.telnyx.webrtc.sdk.model.LogLevel
import com.telnyx.webrtc.sdk.model.SocketMethod
import com.telnyx.webrtc.sdk.model.TxPushIPConfig
import com.telnyx.webrtc.sdk.model.TxServerConfiguration
import com.telnyx.webrtc.sdk.ui.wsmessages.WsMessageFragment
import com.telnyx.webrtc.sdk.utilities.parseObject
import com.telnyx.webrtc.sdk.utilities.toJsonString
import com.telnyx.webrtc.sdk.utility.MyFirebaseMessagingService
import com.telnyx.webrtc.sdk.verto.receive.*
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -59,24 +62,26 @@ class MainActivity : AppCompatActivity() {
private var isDev = false
private var isAutomaticLogin = false
private var wsMessageList: ArrayList<String>? = null

private var txPushIPConfig: TxPushIPConfig? = null
// Notification handling
private var notificationAcceptHandling: Boolean? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(findViewById(R.id.toolbar_id))
mainViewModel = ViewModelProvider(this@MainActivity)[MainViewModel::class.java]


// Add environment text
isDev = userManager.isDev
updateEnvText(isDev)

FirebaseApp.initializeApp(this)

mainViewModel = ViewModelProvider(this@MainActivity)[MainViewModel::class.java]

checkPermissions()
handleCallNotification()
initViews()
}

Expand Down Expand Up @@ -148,13 +153,14 @@ class MainActivity : AppCompatActivity() {
} ?: throw IllegalStateException("Activity cannot be null")
}

private fun connectToSocketAndObserve() {
private fun connectToSocketAndObserve(txPushIPConfig: TxPushIPConfig? = null) {
if (!isDev) {
mainViewModel.initConnection(applicationContext, null)
mainViewModel.initConnection(applicationContext, null,txPushIPConfig)
} else {
mainViewModel.initConnection(
applicationContext,
TxServerConfiguration(host = "rtcdev.telnyx.com")
TxServerConfiguration(host = "rtcdev.telnyx.com"),
txPushIPConfig
)
}
observeSocketResponses()
Expand Down Expand Up @@ -211,6 +217,7 @@ class MainActivity : AppCompatActivity() {
}

override fun onError(message: String?) {
Timber.e("onError: %s", message)
Toast.makeText(
this@MainActivity,
message ?: "Socket Connection Error",
Expand Down Expand Up @@ -382,7 +389,14 @@ class MainActivity : AppCompatActivity() {

private fun connectButtonPressed() {
progress_indicator_id.visibility = View.VISIBLE
connectToSocketAndObserve()
if (notificationAcceptHandling == true) {
Timber.d("notificationAcceptHandling is true ${txPushIPConfig?.toJsonString()}")
if (txPushIPConfig != null) {
connectToSocketAndObserve(txPushIPConfig)
}
}else {
connectToSocketAndObserve()
}
}

private fun doLogin(isAuto: Boolean) {
Expand Down Expand Up @@ -455,6 +469,7 @@ class MainActivity : AppCompatActivity() {
Timber.d("FCM TOKEN RECEIVED: $token")
}
fcmToken = token

}
}

Expand Down Expand Up @@ -601,6 +616,7 @@ class MainActivity : AppCompatActivity() {
val action = intent.extras?.get(MyFirebaseMessagingService.EXT_KEY_DO_ACTION) as String?

action?.let {
txPushIPConfig = intent.extras?.get(MyFirebaseMessagingService.TX_IP_CONFIG)?.toString()?.parseObject()
if (action == MyFirebaseMessagingService.ACT_ANSWER_CALL) {
// Handle Answer
notificationAcceptHandling = true
Expand All @@ -611,8 +627,5 @@ class MainActivity : AppCompatActivity() {
}
}

override fun onResume() {
super.onResume()
handleCallNotification()
}

}
16 changes: 11 additions & 5 deletions app/src/main/java/com/telnyx/webrtc/sdk/ui/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.telnyx.webrtc.sdk.TokenConfig
import com.telnyx.webrtc.sdk.manager.UserManager
import com.telnyx.webrtc.sdk.model.AudioDevice
import com.telnyx.webrtc.sdk.model.CallState
import com.telnyx.webrtc.sdk.model.TxPushIPConfig
import com.telnyx.webrtc.sdk.model.TxServerConfiguration
import com.telnyx.webrtc.sdk.verto.receive.ReceivedMessageBody
import com.telnyx.webrtc.sdk.verto.receive.SocketResponse
Expand All @@ -34,12 +35,16 @@ class MainViewModel @Inject constructor(

private var calls: Map<UUID, Call> = mapOf()

fun initConnection(context: Context, providedServerConfig: TxServerConfiguration?) {
fun initConnection(
context: Context,
providedServerConfig: TxServerConfiguration?,
txPushIPConfig: TxPushIPConfig? = null
) {
telnyxClient = TelnyxClient(context)
providedServerConfig?.let {
telnyxClient?.connect(it)
telnyxClient?.connect(it, txPushIPConfig = txPushIPConfig)
} ?: run {
telnyxClient?.connect()
telnyxClient?.connect(txPushIPConfig = txPushIPConfig)
}
}

Expand Down Expand Up @@ -101,10 +106,11 @@ class MainViewModel @Inject constructor(
telnyxClient?.call?.acceptCall(callId, destinationNumber)
}

fun disablePushNotifications(sipUserName:String,fcmToken: String) {
telnyxClient?.disablePushNotification(sipUserName,null, fcmToken)
fun disablePushNotifications(sipUserName: String, fcmToken: String) {
telnyxClient?.disablePushNotification(sipUserName, null, fcmToken)
}


fun endCall(callId: UUID? = null) {
callId?.let {
telnyxClient?.call?.endCall(callId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import com.google.firebase.messaging.RemoteMessage
import com.google.gson.Gson
import com.telnyx.webrtc.sdk.R
import com.telnyx.webrtc.sdk.model.PushMetaData
import com.telnyx.webrtc.sdk.model.TxPushIPConfig
import com.telnyx.webrtc.sdk.ui.MainActivity
import com.telnyx.webrtc.sdk.utilities.toJsonString
import org.json.JSONObject
import timber.log.Timber
import java.util.*
Expand All @@ -46,6 +48,11 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notificationID = Random().nextInt(3000)

// Save the IP and Port for the call
val txPushIPConfigData = TxPushIPConfig(
telnyxPushMetadata.rtcIP,
telnyxPushMetadata.rtcPort
).toJsonString()
/*
Apps targeting SDK 26 or above (Android O) must implement notification channels and add its notifications
to at least one of them.
Expand All @@ -71,6 +78,9 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
answerResultIntent.action = Intent.ACTION_VIEW
answerResultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
answerResultIntent.putExtra(EXT_KEY_DO_ACTION, ACT_ANSWER_CALL)

answerResultIntent.putExtra(TX_IP_CONFIG, txPushIPConfigData)

val answerPendingIntent = PendingIntent.getActivity(
this,
ANSWER_REQUEST_CODE,
Expand All @@ -83,7 +93,7 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
.setSmallIcon(R.drawable.ic_stat_contact_phone)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentTitle(remoteMessage.data["title"])
.setContentText(telnyxPushMetadata.caller_name + " - " + telnyxPushMetadata.caller_number)
.setContentText(telnyxPushMetadata.callerName + " - " + telnyxPushMetadata.callerNumber)
.setVibrate(longArrayOf(1000, 1000, 1000, 1000, 1000))
.addAction(R.drawable.ic_call_white, ACT_ANSWER_CALL, answerPendingIntent)
.addAction(R.drawable.ic_call_end_white, ACT_REJECT_CALL, rejectPendingIntent)
Expand Down Expand Up @@ -143,6 +153,8 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
private const val ANSWER_REQUEST_CODE = 0
private const val REJECT_REQUEST_CODE = 1

const val TX_IP_CONFIG = "tx_push_ip_config"

const val EXT_KEY_DO_ACTION = "ext_key_do_action"
const val EXT_CALL_ID = "ext_call_id"
const val EXT_DESTINATION_NUMBER = "ext_destination_number"
Expand Down
2 changes: 1 addition & 1 deletion telnyx_rtc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ apply plugin: 'maven-publish'
apply plugin: "com.bugsnag.android.gradle"

def getVersionName = { ->
return "v1.2.16-alpha"
return "v.1.2.4"
}

def getArtifactId = { ->
Expand Down
Loading

0 comments on commit dedd0cb

Please sign in to comment.