Reference code snippet - How to initiate or accept PSTN Call
Sample PSTN call implementation.
Create an application class to initialize the SDK
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Rtc555Sdk.initializeLibrary(this);
}
}
Link the application class in AndroidManifest.xml
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rtcandroiddemo">
<application
android:name=".MyApplication"
...
>
...
</application>
</manifest>
Set SDK configuration after successful login
//After successful login
class PstnCallActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pstn_call);
init();
}
private void init(){
Rtc555Sdk.setConfig(configObject(getApplication()));
}
private Rtc555Config configObject() {
Rtc555Config configObject = Rtc555Sdk.getInstance().getConfig();
configObject.phoneNumber = <YOUR_PHONE_UNMBER>; // Source Telephone Number i.e. phone number for the account you are logged in with.
configObject.routingId = <ROUTING_ID>; // Unique id for the user logged in 555 platform. (Identity manager response)
configObject.token = <555_JWT_TOKEN> // JWT token for accessing the 555 SDK/backend APIs. Token can be access from the 555 login response.
configObject.url = <EVM_URL>; // Event manager URL
configObject.domain = <DOMAIN> // Unique domain name
configObject.notificationManagerUrl = <NTM_URL> //(Optional) For mobile notifications from 555 Platform
return configObject;
}
}
Implement SDK Voice callbacks
public class PstnCallActivity extends AppCompatActivity implements Rtc555Voice.Rtc555VoiceObserver{
...
Boolean hasActiveCall = false;
@Override
public void onStatus(CallStatus callStatus, String callId) {
switch (callStatus) {
case initializing:
Log.d(TAG, "Initializing..");
break;
case connecting:
Log.d(TAG, "Connecting..");
break;
case reconnecting:
Log.d(TAG, "Reconnecting..");
break;
case connected:
Log.d(TAG, "Active Call..");
hasActiveCall = true;
break;
case disconnected:
Log.d(TAG, "Disconnected..");
hasActiveCall = false;
break;
}
}
@Override
public void onError(Rtc555Exception e, String callId) {
Log.d(TAG,"Error: "+e.getMessage()+ "for callId: "+callId);
}
@Override
public void onCallStats(Integer callQuality, String bitrate) {
System.out.println("Call quality "+callQuality);
}
}
Initial PSTN call
private void initiatePSTNCall(){
Rtc555Voice.dial(<DIALLED_UNMBER>, buildOutboundPayload(), this, new Rtc555Result() {
@Override
public void onSuccess(String callId) {
Log.d(TAG, "DIAL CALL SUCCESS AND CALLID IS " + callId);
}
@Override
public void onError(Rtc555Exception e) {
Log.d(TAG, "Dial Call Error: " + e.getMessage());
}
});
}
private String buildOutboundPayload() {
JSONObject userdata = new JSONObject();
try {
JSONObject data = new JSONObject();
data.put("cname", "User Name");
data.put("cid", "userName123");
data.put("tar", <DIALLED_UNMBER>);
JSONObject notification = new JSONObject();
notification.put("type","pstn");
notification.put("topic", "federation/pstn");
userdata.put("data", data);
userdata.put("notification", notification);
} catch (Exception e) {
e.printStackTrace();
}
Log.i(TAG, userdata.toString());
return userdata.toString();
}
Accept and Reject Call
public class PstnCallActivity extends AppCompatActivity implements Rtc555Voice.Rtc555VoiceObserver, Rtc555SdkObserver {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init(){
...
Rtc555Sdk.setNotificationObserver(this);
}
...
//SDK onNotification callback
@Override
public void onNotification(HashMap hashMap) {
String type = (String) hashMap.get("type");
switch (type){
case "notify":
Log.d(TAG, "xmpp notify");
//After user accepts incoming call invite, call SDK accept API with received payload
acceptCall(buildNotificationData(hashMap));
break;
case "cancel":
Log.d(TAG, "xmpp cancel");
//If user rejects incoming call invite
rejectCall(buildNotificationData(hashMap));
break;
case "chat":
Log.d(TAG, "xmpp chat");
break;
default:
Log.d(TAG,"Failed to determine notification type");
}
}
@Override
public void onConnectionStateChange(String s) {
}
private void acceptCall(HashMap data){
Rtc555Voice.accept(data, this, new Rtc555Result() {
@Override
public void onSuccess(String callId) {
Log.d(TAG, "START CALL SUCCESS AND CALLID IS " + callId);
}
@Override
public void onError(Rtc555Exception e) {
Log.d(TAG, "Accept Call Error: " + e.getMessage());
}
});
}
private void rejectCall(HashMap data){
Rtc555Voice.reject(data, new Rtc555Result() {
@Override
public void onSuccess(String callId) {
Log.d(TAG, "REJECT CALL SUCCESS AND CALLID IS " + callId);
}
@Override
public void onError(Rtc555Exception e) {
Log.d(TAG, "Reject Call Error: " + e.getMessage());
}
});
}
private HashMap buildNotificationData(HashMap data){
HashMap<String, String> notifyData = new HashMap();
notifyData.put("room_id", (String) data.get("roomid"));
notifyData.put("trace_id", (String) data.get("traceid"));
notifyData.put("room_token", (String) data.get("roomtoken"));
notifyData.put("room_token_expiry_time", (String) data.get("roomtokenexpirytime"));
notifyData.put("rtc_server", (String) data.get("rtcserver"));
notifyData.put("to_routing_id", (String) data.get("routingid"));
return notifyData;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// User tapped on mute call button
findViewById(R.id.your_mute_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Rtc555Voice.mute(callId);
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// User tapped on unmute call button
findViewById(R.id.your_unmute_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Rtc555Voice.unMute(callId);
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// User tapped on end call button
findViewById(R.id.your_hangup_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Rtc555Voice.hangup(callId);
}
});
}
@Override
protected void onStop() {
super.onStop();
////No active PSTN call then
if(!hasActiveCall)
Rtc555Sdk.cleanup();
}