Skip to content

Commit

Permalink
Is/feature/disable push (#319)
Browse files Browse the repository at this point in the history
* Added Disable Push Notifications Functionality for Credential Login, To disable push notifications for the current user

* Added Disable Push Notifications Functionality for Token Login

* Fix Style for New Implementation

* Mock Username and Password Reset
  • Loading branch information
isaacakakpo1 authored Aug 24, 2023
1 parent a4646e5 commit 85666d7
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ captures/
.idea/assetWizardSettings.xml
.idea/dictionaries
.idea/libraries
.idea/
# Android Studio 3 in .gitignore file.
.idea/caches
.idea/modules.xml
Expand Down
9 changes: 0 additions & 9 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion app/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/build
/build
.idea/
4 changes: 4 additions & 0 deletions app/src/main/java/com/telnyx/webrtc/sdk/ui/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ class MainViewModel @Inject constructor(
telnyxClient?.call?.acceptCall(callId, destinationNumber)
}

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

fun endCall(callId: UUID? = null) {
callId?.let {
telnyxClient?.call?.endCall(callId)
Expand Down
4 changes: 4 additions & 0 deletions telnyx_rtc/src/main/java/com/telnyx/webrtc/sdk/Call.kt
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,10 @@ class Call(
)
}

override fun onDisablePushReceived(jsonObject: JsonObject) {
// Noop
}

override fun onAttachReceived(jsonObject: JsonObject) {
Timber.d("[%s] :: onAttachReceived [%s]", this@Call.javaClass.simpleName, jsonObject)
val params = jsonObject.getAsJsonObject("params")
Expand Down
81 changes: 81 additions & 0 deletions telnyx_rtc/src/main/java/com/telnyx/webrtc/sdk/TelnyxClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import android.media.AudioManager
import android.media.MediaPlayer
import android.net.ConnectivityManager
import android.os.PowerManager
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.*
import com.bugsnag.android.Bugsnag
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.telnyx.webrtc.sdk.model.*
import com.telnyx.webrtc.sdk.socket.TxSocket
Expand Down Expand Up @@ -341,6 +343,53 @@ class TelnyxClient(
socket.send(loginMessage)
}


/**
* Disables push notifications for current user
*
* Takes :
* @param sipUserName : sip username of the current user or
* @param loginToken : fcm token of the device
* @param fcmToken : fcm token of the device
* NB : Push Notifications are enabled by default after login
*
* returns : {"jsonrpc":"2.0","id":"","result":{"message":"disable push notification success"}}
* */
fun disablePushNotification(sipUserName: String?, loginToken: String?, fcmToken: String) {

sipUserName ?: loginToken ?: return

val params = when {
sipUserName == null -> {
TokenDisablePushParams(
loginToken = loginToken!!,
userVariables = UserVariables(fcmToken)
)
}

loginToken == null -> {
DisablePushParams(
user = sipUserName,
userVariables = UserVariables(fcmToken)
)
}

else -> {
return
}
}

val disablePushMessage = SendingMessageBody(
id = UUID.randomUUID().toString(),
method = SocketMethod.DISABLE_PUSH.methodName,
params = params
)
val message = Gson().toJson(disablePushMessage)
Log.d("disablePushMessage", message)
socket.send(disablePushMessage)
}


/**
* Logs the user in with credentials provided via TokenConfig
*
Expand Down Expand Up @@ -429,13 +478,15 @@ class TelnyxClient(
)
}
}

AudioDevice.PHONE_EARPIECE -> {
// For phone ear piece
audioManager?.mode = AudioManager.MODE_IN_COMMUNICATION
audioManager?.stopBluetoothSco()
audioManager?.isBluetoothScoOn = false
audioManager?.isSpeakerphoneOn = false
}

AudioDevice.LOUDSPEAKER -> {
// For phone speaker(loudspeaker)
audioManager?.mode = AudioManager.MODE_NORMAL
Expand Down Expand Up @@ -605,14 +656,17 @@ class TelnyxClient(
onLoginSuccessful(sessid)
}
}

GatewayState.NOREG.state -> {
invalidateGatewayResponseTimer()
socketResponseLiveData.postValue(SocketResponse.error("Gateway registration has timed out"))
}

GatewayState.FAILED.state -> {
invalidateGatewayResponseTimer()
socketResponseLiveData.postValue(SocketResponse.error("Gateway registration has failed"))
}

GatewayState.FAIL_WAIT.state -> {
if (autoReconnectLogin && connectRetryCounter < RETRY_CONNECT_TIME) {
connectRetryCounter++
Expand All @@ -626,22 +680,28 @@ class TelnyxClient(
socketResponseLiveData.postValue(SocketResponse.error("Gateway registration has received fail wait response"))
}
}

GatewayState.EXPIRED.state -> {
invalidateGatewayResponseTimer()
socketResponseLiveData.postValue(SocketResponse.error("Gateway registration has timed out"))
}

GatewayState.UNREGED.state -> {
// NOOP - logged within TxSocket
}

GatewayState.TRYING.state -> {
// NOOP - logged within TxSocket
}

GatewayState.REGISTER.state -> {
// NOOP - logged within TxSocket
}

GatewayState.UNREGISTER.state -> {
// NOOP - logged within TxSocket
}

else -> {
invalidateGatewayResponseTimer()
socketResponseLiveData.postValue(SocketResponse.error("Gateway registration has failed with an unknown error"))
Expand Down Expand Up @@ -709,6 +769,27 @@ class TelnyxClient(
call?.onIceCandidateReceived(iceCandidate)
}

override fun onDisablePushReceived(jsonObject: JsonObject) {
Timber.d(
"[%s] :: onDisablePushReceived [%s]",
this@TelnyxClient.javaClass.simpleName,
jsonObject
)
val errorMessage = jsonObject.get("result").asJsonObject.get("message").asString
val disablePushResponse = DisablePushResponse(
errorMessage.contains(DisablePushResponse.SUCCESS_KEY),
errorMessage
)
socketResponseLiveData.postValue(
SocketResponse.messageReceived(
ReceivedMessageBody(
SocketMethod.RINGING.methodName,
disablePushResponse
)
)
)
}

override fun onAttachReceived(jsonObject: JsonObject) {
call?.onAttachReceived(jsonObject)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ enum class SocketMethod(var methodName: String) {
PINGPONG("telnyx_rtc.ping"),
CLIENT_READY("telnyx_rtc.clientReady"),
GATEWAY_STATE("telnyx_rtc.gatewayState"),
DISABLE_PUSH("telnyx_rtc.disable_push_notification"),
LOGIN("login")
}
14 changes: 14 additions & 0 deletions telnyx_rtc/src/main/java/com/telnyx/webrtc/sdk/socket/TxSocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,15 @@ class TxSocket(
}
}
}

params !== null && params.asJsonObject.has("state") -> {
params = jsonObject.get("params").asJsonObject
if (params.asJsonObject.has("state")) {
val gatewayState = params.get("state").asString
listener.onGatewayStateReceived(gatewayState, null)
}
}

jsonObject.has("method") -> {
Timber.tag("VERTO").d(
"[%s] Received Method [%s]",
Expand All @@ -144,39 +146,50 @@ class TxSocket(
CLIENT_READY.methodName -> {
listener.onClientReady(jsonObject)
}

ATTACH.methodName -> {
listener.onAttachReceived(jsonObject)
}

INVITE.methodName -> {
listener.onOfferReceived(jsonObject)
}

ANSWER.methodName -> {
listener.onAnswerReceived(jsonObject)
}

MEDIA.methodName -> {
listener.onMediaReceived(jsonObject)
}

BYE.methodName -> {
val params =
jsonObject.getAsJsonObject("params")
val callId =
UUID.fromString(params.get("callID").asString)
listener.onByeReceived(callId)
}

INVITE.methodName -> {
listener.onOfferReceived(jsonObject)
}

RINGING.methodName -> {
listener.onRingingReceived(jsonObject)
}

DISABLE_PUSH.methodName -> {
listener.onDisablePushReceived(jsonObject)
}
PINGPONG.methodName -> {
isPing = true
webSocket.send(text)
listener.pingPong()
}
}
}

jsonObject.has("error") -> {
if (jsonObject.get("error").asJsonObject.has("code")) {
val errorCode =
Expand All @@ -191,6 +204,7 @@ class TxSocket(
CREDENTIAL_ERROR.errorCode -> {
listener.onErrorReceived(jsonObject)
}

TOKEN_ERROR.errorCode -> {
listener.onErrorReceived(jsonObject)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ interface TxSocketListener {
*/
fun onIceCandidateReceived(iceCandidate: IceCandidate)

/**
* Fires when a disablePush response is recieved
* @param jsonObject, the socket response in a jsonObject format
* @see [IceCandidate]
*/
fun onDisablePushReceived(jsonObject: JsonObject)

/**
* Fires once a connection has been reestablished during an ongoing call and a session
* is being reattached
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package com.telnyx.webrtc.sdk.verto.receive

import android.os.Parcel
import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import kotlinx.android.parcel.Parcelize
Expand All @@ -14,6 +15,21 @@ import java.util.*
*/
sealed class ReceivedResult


@Parcelize
data class DisablePushResponse(
@SerializedName("message")
val success: Boolean,
@SerializedName("message")
val message: String
) : ReceivedResult(), Parcelable {

companion object {
// Refactor for backend to send a boolean instead of a string
const val SUCCESS_KEY = "success"
}
}

/**
* A login response received by the socket connection
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ data class ByeParams(
val dialogParams: ByeDialogParams
) : ParamRequest()


data class DisablePushParams(
@SerializedName("user")
val user: String,
@SerializedName("User-Agent")
val userVariables: UserVariables
) : ParamRequest()

data class TokenDisablePushParams(
@SerializedName("login_token")
val loginToken: String,
@SerializedName("User-Agent")
val userVariables: UserVariables
) : ParamRequest()

data class UserVariables(
@SerializedName("push_device_token")
val pushDeviceToken:String,
@SerializedName("push_notification_provider")
val pushNotificationProvider:String = "android",
)

data class ModifyParams(
val sessid: String,
val action: String,
Expand Down

0 comments on commit 85666d7

Please sign in to comment.