Sinch API in an Angular 2 project times out on onCallProgressing Sinch API in an Angular 2 project times out on onCallProgressing angular angular

Sinch API in an Angular 2 project times out on onCallProgressing


---------User Presence System For a Calling App-

In MainActivity onCreate, start an instance of the Sinch client using your app key and secret from the Sinch dashboard. In addition, define the hangup and pickup buttons:

//declare globally within MainActivityprivate SinchClient sinchClient;private Button pickupButton;private Button hangupButton;private Call currentCall;//in onCreatesinchClient = Sinch.getSinchClientBuilder()    .context(this)    .userId(username)    .applicationKey("your-sinch-key")    .applicationSecret("your-sinch-secret")    .environmentHost("clientapi.sinch.com")    .build();//use the calling featuresinchClient.setSupportCalling(true);//start listening for incoming callssinchClient.startListeningOnActiveConnection();sinchClient.start();pickupButton = (Button) findViewById(R.id.pickupButton);hangupButton = (Button) findViewById(R.id.hangupButton);

Next, define listeners for the Sinch call client, as well as for individual calls. Do this inside of the MainActivity class:

//you'll attach an instance of this listener to individual callsprivate class SinchCallListener implements CallListener {    //when the call ends for any reason    @Override    public void onCallEnded(Call call) {        //no current call        currentCall = null;        hangupButton.setText("No call to hang up right now...");        pickupButton.setText("No call to pick up right now...");        //volume buttons go back to controlling ringtone volume        setVolumeControlStream(AudioManager.USE_DEFAULT_STREAM_TYPE);    }    //recipient picks up the call    @Override    public void onCallEstablished(Call call) {        hangupButton.setText("Hang up call with " + call.getRemoteUserId());        pickupButton.setText("No call to pick up right now...");        //ringtone volume buttons now control the speaker volume        setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);    }    //when call is "ringing"    @Override    public void onCallProgressing(Call call) {        hangupButton.setText("Ringing");    }    //don't worry about this for now    @Override    public void onShouldSendPushNotification(Call call, List<PushPair> pushPairs) {}}//you'll attach an instance of this to the Sinch clientprivate class SinchCallClientListener implements CallClientListener {    //when there is an incoming call    @Override    public void onIncomingCall(CallClient callClient, Call incomingCall) {        //only react if there is no current call        if (currentCall == null) {            currentCall = incomingCall;            currentCall.addCallListener(new SinchCallListener());            pickupButton.setText("Pick up call from " + incomingCall.getRemoteUserId());            hangupButton.setText("Ignore call from " + incomingCall.getRemoteUserId());        }    }}

Go back to where you start the Sinch client, and right after you start it, add an instance of SinchCallClientListener. Now your users will be notified when they have an incoming call:

sinchClient.getCallClient().addCallClientListener(new SinchCallClientListener());

Next, make the pickup and hangup buttons functional in MainActivity onResume:

//if there is an incoming call, answer it and stop listening for more incoming callspickupButton.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {        if (currentCall != null) {            currentCall.answer();        }    }});//if there is a current call, hang it uphangupButton.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {        if (currentCall != null) {            currentCall.hangup();        }    }});

Now that you’re prepared to accept incoming calls, you should let your users actually make calls.

//in onResume

usersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {    @Override    public void onItemClick(AdapterView<?> parent, View view, int i, long id) {        if (currentCall == null) {            //call the user that was clicked            currentCall = sinchClient.getCallClient().callUser(users.get(i).toString());            //add a listener to the call            currentCall.addCallListener(new SinchCallListener());            //change hangup button text            hangupButton.setText("Hang Up Call with " + users.get(i));        } else {            //clicking names won't do anything if there is a current call            Toast.makeText(getApplicationContext(),                "Can't call " + users.get(i) + " while on another call.",                Toast.LENGTH_SHORT).show();        }    }});

And there you have it! Try launching the app on two different devices or emulators, log in as two different users, and call each other. Try playing around with killing one app and restarting it as another user; it’s cool to see the “online” list update in real time on the other device.