I'm Developing VoIP App for call app to app. I am using Sinch API, FCM for receiving incoming in background. It is working fine, but when the app is killed in the background I am not getting incoming calls.
My code is as follows:
FCM-
public class FireBaseMsgService extends FirebaseMessagingService {
public static SinchClient sinchClient=null;
public static CallClient callClient=null;
String username;
@Override
public void onCreate() {
username=Common.getSavedUserData(this,"user_name");
super.onCreate();
if (username != null) {
initsinch();
}
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
//initsinch();
if (foregrounded()) {
return;
}
if (SinchHelpers.isSinchPushPayload(remoteMessage.getData())) {
initsinch();
NotificationResult result = sinchClient.relayRemotePushNotificationPayload(remoteMessage.getData()); // relay the background incoming call
}
}
// To check if the app is in foreground
public static boolean foregrounded() {
ActivityManager.RunningAppProcessInfo appProcessInfo =
new ActivityManager.RunningAppProcessInfo();
ActivityManager.getMyMemoryState(appProcessInfo);
return (appProcessInfo.importance == IMPORTANCE_FOREGROUND
|| appProcessInfo.importance == IMPORTANCE_VISIBLE);
}
private void initsinch() {
if (sinchClient == null) {
android.content.Context context = this.getApplicationContext();
sinchClient = Sinch.getSinchClientBuilder().context(context)
.applicationKey(APP_KEY)
.applicationSecret(APP_SECRET)
.environmentHost(ENVIORNMENT)
.userId(username).build();
sinchClient.setSupportCalling(true);
sinchClient.setSupportActiveConnectionInBackground(true);
sinchClient.startListeningOnActiveConnection();
sinchClient.setSupportManagedPush(true);
sinchClient.setPushNotificationDisplayName("my display name");
sinchClient.addSinchClientListener(new SinchClientListener() {
public void onClientStarted(SinchClient client) {
}
public void onClientStopped(SinchClient client) {
}
public void onClientFailed(SinchClient client, SinchError error) {
}
public void onRegistrationCredentialsRequired(SinchClient client, ClientRegistration registrationCallback) {
}
public void onLogMessage(int level, String area, String message) {
}
});
callClient = sinchClient.getCallClient();
callClient.setRespectNativeCalls(true);
callClient.addCallClientListener(new CallClientListener() {
@Override
public void onIncomingCall(CallClient callClient, Call INCOMINGCALL) {
Intent it=new Intent(getApplicationContext(),IncomingCallScreenActivity.class);
it.putExtra("mCall", INCOMINGCALL.getCallId());
it.putExtra("mCall_caller", INCOMINGCALL.getRemoteUserId());
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(it);
}
});
}
if (sinchClient != null && !sinchClient.isStarted()) {
sinchClient.start();
}
}
}
The Username is the login username which is intialized in Sinch User. I have created mCall object in IncomingCallActivity.call and receiving the call object in Onservice Connected() and calling sinchCall listener as follows,
public class IncomingCallScreenActivity extends BaseActivity {
static final String TAG=IncomingCallScreenActivity.class.getSimpleName();
private String mCallId;
private String mCallLocation;
private AudioPlayer mAudioPlayer;
TextView highlight;
// private NotificationCallvo mCallVo;
Call fmcall;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.incoming);
highlight=(TextView)findViewById(R.id.highlight);
Animation a = AnimationUtils.loadAnimation(this, R.anim.blink);
a.reset();
highlight.startAnimation(a);
img=(ImageView)findViewById(R.id.image);
// Animation shake = AnimationUtils.loadAnimation(this, R.anim.milkshake);
// img.startAnimation(shake);
ImageButton answer=(ImageButton)findViewById(R.id.answerButton);
Animation shake = AnimationUtils.loadAnimation(this, R.anim.milkshake);
answer.startAnimation(shake);
answer.setOnClickListener(mClickListener);
ImageButton decline=(ImageButton) findViewById(R.id.declineButton);
decline.startAnimation(shake);
decline.setOnClickListener(mClickListener);
mAudioPlayer=new AudioPlayer(this);
mAudioPlayer.playRingTone();
mCallId=getIntent().getStringExtra(SinchService.CALL_ID);
System.out.println("cllerid++"+mCallId);
mCallLocation=getIntent().getStringExtra(SinchService.LOCATION);
}
@Override
protected void onServiceConnected()
{
if(getIntent().getExtras().get("mCall")!=null){
fmcall=FireBaseMsgService.callClient.getCall((String) getIntent().getExtras().get("mCall"));
((Call) fmcall).addCallListener(new SinchCallListener());
TextView remoteUser = (TextView) findViewById(R.id.remoteUser);
remoteUser.setText(((Call) fmcall).getRemoteUserId());
// TextView remoteUserLocation = (TextView) findViewById(R.id.remoteUserLocation);
// remoteUserLocation.setText("Calling from " + mCallLocation);
}else {
Call call = getSinchServiceInterface().getCall(mCallId);
if (call != null) {
call.addCallListener(new SinchCallListener());
TextView remoteUser = (TextView) findViewById(R.id.remoteUser);
remoteUser.setText(call.getRemoteUserId());
TextView remoteUserLocation = (TextView) findViewById(R.id.remoteUserLocation);
remoteUserLocation.setText("Calling from " + mCallLocation);
} else {
Log.e(TAG, "Started with invalid callId, aborting");
finish();
}
}
}
private void answerClicked() {
mAudioPlayer.stopRingtone();
if (getIntent().getExtras().get("mCall") != null) {
fmcall = FireBaseMsgService.callClient.getCall((String) getIntent().getExtras().get("mCall"));
fmcall.answer();
Intent intent = new Intent(this, CallScreenActivity.class);
intent.putExtra("fcallId", (String) getIntent().getExtras().get("mCall"));
startActivity(intent);
} else {
Call call = getSinchServiceInterface().getCall(mCallId);
if (call != null) {
call.answer();
Intent intent = new Intent(this, CallScreenActivity.class);
intent.putExtra(SinchService.CALL_ID, mCallId);
startActivity(intent);
} else {
finish();
}
}
}
private void declineClicked(){
mAudioPlayer.stopRingtone();
if(getIntent().getExtras().get("mCall")!=null){
fmcall.hangup();
}else {
Call call=getSinchServiceInterface().getCall(mCallId);
if(call!=null)
{
call.hangup();
}
finish();
}
}
private class SinchCallListener implements CallListener {
@Override
public void onCallProgressing(Call call) {
Log.d(TAG,"Call progressing");
}
@Override
public void onCallEstablished(Call call) {
Log.d(TAG,"Call Established");
}
@Override
public void onCallEnded(Call call) {
CallEndCause cause=call.getDetails().getEndCause();
Log.d(TAG, "Call ended, cause: " + cause.toString());
mAudioPlayer.stopRingtone();
finish();
}
@Override
public void onShouldSendPushNotification(Call call, List<PushPair> list) {
}
}
private View.OnClickListener mClickListener=new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId())
{
case R.id.answerButton:
answerClicked();
break;
case R.id.declineButton:
declineClicked();
break;
}
}
};
}
When I Kill the App in background and make call to the username with another device, I am not receiving call notification. please help