I want to add app to app calling feature in my android app. I am using sinch SDK for that. I have set up correctly and can receive and accept calls in a very basic manner.
Now I have created a background service so that I can have notification for incoming calls along with playing ringtone and also showing notifications for missed calls if the call is hung up from the caller side.
PROBLEM : When I tap the notification it opens another activity which have the UI for incoming calls and two buttons for accepting and rejecting the calls. I am not able to access the call on there. I can neither accept the call nor reject the call.
here is the code for by background service:
private DatabseReferenct ref;
Uri ring = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
Ringtone r ;
public boolean onStartJob(JobParameters jobParameters) {
ref=FirebaseDatabase.getInstance().getReference().child("Users");
r = RingtoneManager.getRingtone(getApplicationContext(), ring);
if(FirebaseAuth.getInstance().getCurrentUser()!=null)
{
dobackgroundworkd(jobParameters);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
r.setLooping(true);
}
return true;
}
private void dobackgroundworkd(JobParameters jobParameters) {
notificationManager=NotificationManagerCompat.from(this);
sinchClient= Sinch.getSinchClientBuilder().context(getApplicationContext())
.userId(auth)
.applicationKey(MY_KEY)
.applicationSecret(MY SECRET)
.environmentHost("clientapi.sinch.com")
.build();
sinchClient.setSupportCalling(true);
sinchClient.setSupportActiveConnectionInBackground(true);
sinchClient.startListeningOnActiveConnection();
sinchClient.start();
sinchClient.getCallClient().addCallClientListener(new CallClientListener() {
@Override
public void onIncomingCall(CallClient callClient, Call call) {
callerName=null;
// JUST TO GET THE NAME OF THE CALLER FROM FIREBASE DATABASE
ref.child(call.getRemoteUserId()).child("Name").addValueEventListener(new
ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists())
{
callerName=dataSnapshot.getValue().toString();
notificaionChannel1(call);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
// TO LISTEN FOR CALL STATUS
call.addCallListener(new CallListener() {
@Override
public void onCallProgressing(Call call) {
giveNotification(call);
}
@Override
public void onCallEstablished(Call call) {
r.stop();
notificationManager.cancel(INCOMING_CALL_ID);
}
@Override
public void onCallEnded(Call call) { // TO GIVE THE MISSED CALL NOTIFICATION
Notification notification = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID_1)
.setSmallIcon(R.drawable.incoming_call)
.setContentTitle("Missed Call")
.setContentText(callerName + " was calling you")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setWhen(System.currentTimeMillis())
.setAutoCancel(false)
.build();
notificationManager.notify(MISSED_CALL, notification);
}
r.stop();
call.hangup();
call = null;
}
}
});
}
****THIS FUNCTION IS FOR THE MAIN NOTIFICATION OF INCOMING CALLS****
void giveNotification ( Call call) {
Intent intent=new Intent(getApplicationContext(),Incoming_call_activity.class);
intent.putExtra("UID",call.getRemoteUserId());
intent.putExtra("calltype","incoming");
PendingIntent =pendingIntent=PendingIntent.getActivity(getApplicationContext(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification=new NotificationCompat.Builder(getApplicationContext(),CHANNEL_ID_1)
.setSmallIcon(R.drawable.incoming_call)
.setContentTitle("Incoming Call")
.setContentText(callerName+" is calling you")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setAutoCancel(false)
.build();
r.play();
notificationManager.notify(INCOMING_CALL_ID,notification);
}
And this is the code of other activity Incoming_call_activity.class file:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_incoming_call_activity);
**GETTING THE INTENT VALUES FROM BACKGROUND ACTIVITY**
uid=getIntent().getStringExtra("UID");
incall=getIntent().getStringExtra("calltype");
sinchClient= Sinch.getSinchClientBuilder().context(getApplicationContext())
.userId(FirebaseAuth.getInstance().getCurrentUser().getUid())
.applicationKey(MY_KEY)
.applicationSecret(MY_SECRET)
.environmentHost("clientapi.sinch.com")
.build();
sinchClient.setSupportCalling(true);
sinchClient.startListeningOnActiveConnection();
sinchClient.start();
sinchClient.getCallClient().addCallClientListener(new SinchCallClientListener());
takeCall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(currentCall!=null)
currentCall.answer();
}
});
}
**** THESE ARE THE SINCH CALL HANDLERS MENTIONED ABOVE****
private class SinchCallClientListener implements CallClientListener{
@Override
public void onIncomingCall(CallClient callClient, Call call) {
if(currentCall==null)
{
currentCall=call;
currentCall.addCallListener(new SinchCallListener());
}
}
}
private class SinchCallListener implements CallListener {
//when the call ends for any reason
@Override
public void onCallEnded(Call call) {
//no current call
currentCall = null;
//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) {
setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
}
//when call is "ringing"
@Override
public void onCallProgressing(Call call) {
}
//don't worry about this for now
@Override
public void onShouldSendPushNotification(Call call, List<PushPair> pushPairs) {}
}