使用sinch SDK Android

时间:2016-09-08 04:25:21

标签: android sinch android-sinch-api

我是Android开发的新手。我正在使用 sinch 服务从app到app进行调用。我正在关注本教程https://github.com/sinch/android-app-app-calling-headers 我可以通过 CallScreenActivity onShouldSendPushNotification 方法发送通知来唤醒接收方电话。在接收器手机中,我不知道如何在 CallService 中启动 CallClientListener 界面的 onIncomingCall 方法。我尝试将 CallService GcmListenerService 绑定,但未触发 onIncomingCall 。我已经尝试了3-4天但没有得到正确的解决方案。

public class CallScreenActivity extends BaseActivity {

static final String TAG = CallScreenActivity.class.getSimpleName();

private AudioPlayer mAudioPlayer;
private Timer mTimer;
private UpdateCallDurationTask mDurationTask;
private String mCallId;
private long mCallStart = 0;
public String ReceiverGCMID,ReceiverName,SenderName,ReceiverID,SenderID,SenderGCMID;
private TextView mCallDuration;
private TextView mCallState;
private TextView mCallerName;

private class UpdateCallDurationTask extends TimerTask {

    @Override
    public void run() {
        CallScreenActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                updateCallDuration();
            }
        });
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.callscreen);
    mAudioPlayer = new AudioPlayer(this);
    mCallDuration = (TextView) findViewById(R.id.callDuration);
    mCallerName = (TextView) findViewById(R.id.remoteUser);
    mCallState = (TextView) findViewById(R.id.callState);
    Button endCallButton = (Button) findViewById(R.id.hangupButton);

    endCallButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            endCall();
        }
    });
    mCallStart = System.currentTimeMillis();
    Intent intent = getIntent();
    mCallId = intent.getStringExtra("CALL_ID");

    ReceiverName = intent.getStringExtra("RECEIVER_NAME");
    SenderName = intent.getStringExtra("Sender_NAME");
    ReceiverID = intent.getStringExtra("ReceiverID");
    SenderID = intent.getStringExtra("SenderID");
    SenderGCMID = intent.getStringExtra("SenderGCMID");
    ReceiverGCMID = intent.getStringExtra("receiverGCMID");
}

@Override
public void onServiceConnected() {
    Call call = getSinchServiceInterface().getCall(mCallId);

    if (call != null) {
        call.addCallListener(new SinchCallListener());
        mCallerName.setText("Calling " + ReceiverName);
        mCallState.setText(call.getState().toString());
    } else {
        Log.e(TAG, "Started with invalid callId, aborting.");
        finish();
    }
}

@Override
public void onPause() {
    super.onPause();
    mDurationTask.cancel();
    mTimer.cancel();
}

@Override
public void onResume() {
    super.onResume();
    mTimer = new Timer();
    mDurationTask = new UpdateCallDurationTask();
    mTimer.schedule(mDurationTask, 0, 500);
}

@Override
public void onBackPressed() {
    // User should exit activity by ending call, not by going back.
}

private void endCall() {
    mAudioPlayer.stopProgressTone();
    Call call = getSinchServiceInterface().getCall(mCallId);
    if (call != null) {
        call.hangup();
    }
    finish();
}

private String formatTimespan(long timespan) {
    long totalSeconds = timespan / 1000;
    long minutes = totalSeconds / 60;
    long seconds = totalSeconds % 60;
    return String.format(Locale.US, "%02d:%02d", minutes, seconds);
}

private void updateCallDuration() {
    if (mCallStart > 0) {
        mCallDuration.setText(formatTimespan(System.currentTimeMillis() - mCallStart));
    }
}

private class SinchCallListener implements CallListener {

    @Override
    public void onCallEnded(Call call) {
        CallEndCause cause = call.getDetails().getEndCause();
        Log.e(TAG, "Call ended. Reason: " + cause.toString());
        mAudioPlayer.stopProgressTone();
        setVolumeControlStream(AudioManager.USE_DEFAULT_STREAM_TYPE);
        String endMsg = "Call ended: " + call.getDetails().toString();
        Toast.makeText(CallScreenActivity.this, endMsg, Toast.LENGTH_LONG).show();
        endCall();
    }

    @Override
    public void onCallEstablished(Call call) {
        Log.d(TAG, "Call established");
        mAudioPlayer.stopProgressTone();
        mCallState.setText(call.getState().toString());
        setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
        mCallStart = System.currentTimeMillis();
    }

    @Override
    public void onCallProgressing(Call call) {
        Log.d(TAG, "Call progressing");
        mAudioPlayer.playProgressTone();
    }

    @Override
    public void onShouldSendPushNotification(Call call, List<PushPair> pushPairs) {
        Log.e(TAG,"onShouldSendPushNotification");
        String gcmid = ReceiverGCMID;
        String APIkey = "**********************";
        GCM gcm = new GCM();
        String messagetoSend = "talaqbacheacallreceivekahebanasdo"+",,,@@@Uc@Y@U...,,,"+SenderID+",,,@@@Uc@Y@U...,,,"+ReceiverID+",,,@@@Uc@Y@U...,,,"+SenderName+",,,@@@Uc@Y@U...,,,"+ReceiverName+",,,@@@Uc@Y@U...,,,"+SenderGCMID+",,,@@@Uc@Y@U...,,,"+ReceiverGCMID+",,,@@@Uc@Y@U...,,,"+mCallId;
        gcm.sendMessage(APIkey,gcmid,messagetoSend);
    }
}

}

public  class MessageAndCallReceiver extends GcmListenerService implements ServiceConnection{

private CallService.SinchServiceInterface mSinchServiceInterface;
static final String TAG = MessageAndCallReceiver.class.getSimpleName();

@Override
public void onMessageReceived(String from, Bundle data) {
    Log.e(TAG,"onMessageReceived called");
    getApplicationContext().bindService(new Intent(this, CallService.class), this, Context.BIND_AUTO_CREATE);
    String daata = data.getString("data");
    String[] parts = daata.split(",,,@@@Uc@Y@U...,,,");
    if(parts[0].equals("talaqbacheacallreceivekahebanasdo")){

        Intent intent = new Intent(this,CallService.class);
        startService(intent);
        Log.e(TAG,"Call Received in notification");

    }else{
        Log.e(TAG,"Message Received in notification");

    }

}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
    if (CallService.class.getName().equals(componentName.getClassName())) {
        mSinchServiceInterface = (CallService.SinchServiceInterface) iBinder;
    }
}

@Override
public void onServiceDisconnected(ComponentName componentName) {
    if (CallService.class.getName().equals(componentName.getClassName())) {
        mSinchServiceInterface = null;
    }
}

}

public class CallService extends Service  {

private static final String APP_KEY = "******************";
private static final String APP_SECRET = "********************";
private static final String ENVIRONMENT = "sandbox.sinch.com";

public static final String LOCATION = "LOCATION";
public static final String CALL_ID = "CALL_ID";
static final String TAG = CallService.class.getSimpleName();

private SinchServiceInterface mSinchServiceInterface = new SinchServiceInterface();
private SinchClient mSinchClient;
private String mUserId;
private StartFailedListener mListener;


@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return super.onStartCommand(intent, flags, startId);

}

@Override
public void onDestroy() {
    Log.e(TAG,"onDestroy");
    if (mSinchClient != null && mSinchClient.isStarted()) {
        mSinchClient.terminate();
    }
    super.onDestroy();
}

public void start(String userName,Context context) {
    Log.e(TAG,"start");
    if (mSinchClient == null) {
        mUserId = userName;
        mSinchClient = Sinch.getSinchClientBuilder().context(getApplicationContext()).userId(userName)
                .applicationKey(APP_KEY)
                .applicationSecret(APP_SECRET)
                .environmentHost(ENVIRONMENT).build();

        mSinchClient.setSupportCalling(true);
        mSinchClient.startListeningOnActiveConnection();

        mSinchClient.addSinchClientListener(new MySinchClientListener());
        mSinchClient.getCallClient().addCallClientListener(new SinchCallClientListener());
        mSinchClient.start();
    }
}

private void stop() {
    Log.e(TAG,"stop");
    if (mSinchClient != null) {
        mSinchClient.terminate();
        mSinchClient = null;
    }
}

private boolean isStarted() {
    return (mSinchClient != null && mSinchClient.isStarted());
}

@Override
public IBinder onBind(Intent intent) {
    Log.e(TAG,"onBind");
    return mSinchServiceInterface;
}

public class SinchServiceInterface extends Binder {

    public Call callPhoneNumber(String phoneNumber) {
        return mSinchClient.getCallClient().callPhoneNumber(phoneNumber);
    }

    public Call callUser(String userId) {
        return mSinchClient.getCallClient().callUser(userId);
    }

    public Call callUser(String userId, Map<String, String> headers) {
        return mSinchClient.getCallClient().callUser(userId, headers);
    }

    public String getUserName() {
        return mUserId;
    }

    public boolean isStarted() {
        return CallService.this.isStarted();
    }

    public void startClient(String userName) {
        Log.e(TAG,"startClient");
        Context context = getApplicationContext();
        start(userName,context);
    }

    public void stopClient() {
        Log.e(TAG,"stopClient");
        stop();
    }

    public void setStartListener(StartFailedListener listener) {
        mListener = listener;
    }

    public Call getCall(String callId) {
        return mSinchClient.getCallClient().getCall(callId);
    }
}

public interface StartFailedListener {
    void onStartFailed(SinchError error);
    void onStarted();
}

 public void startClient(String userName,Context context) {
    Log.e(TAG,"startClient");
    start(userName,context);
}

private class MySinchClientListener implements SinchClientListener {

    @Override
    public void onClientFailed(SinchClient client, SinchError error) {
        Log.e(TAG, "onClientFailed because "+ error.getMessage().toString());
        if (mListener != null) {
            mListener.onStartFailed(error);
        }
        mSinchClient.terminate();
        mSinchClient = null;
    }

    @Override
    public void onClientStarted(SinchClient client) {
        Log.e(TAG, "onClientStarted");
        if (mListener != null) {
            mListener.onStarted();
        }
    }

    @Override
    public void onClientStopped(SinchClient client) {
        Log.e(TAG, "onClientStopped");
    }

    @Override
    public void onLogMessage(int level, String area, String message) {
        switch (level) {
            case Log.DEBUG:
                Log.d(TAG, message);
                break;
            case Log.ERROR:
                Log.e(TAG, message);
                break;
            case Log.INFO:
                Log.i(TAG, message);
                break;
            case Log.VERBOSE:
                Log.v(TAG, message);
                break;
            case Log.WARN:
                Log.w(TAG, message);
                break;
        }
    }

    @Override
    public void onRegistrationCredentialsRequired(SinchClient client,
            ClientRegistration clientRegistration) {
    }
}

private class SinchCallClientListener implements CallClientListener {

    @Override
    public void onIncomingCall(CallClient callClient, Call call) {
        Log.e(TAG, "Incoming call");
        Intent intent = new Intent(CallService.this, IncomingCallScreenActivity.class);
        intent.putExtra(CALL_ID, call.getCallId());
        intent.putExtra(LOCATION, call.getHeaders().get("location"));
        intent.putExtra("sndrName", call.getHeaders().get("sndrName"));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplicationContext().startActivity(intent);
    }
}

}

public  class BaseActivity extends AppCompatActivity implements ServiceConnection {

public CallService.SinchServiceInterface mSinchServiceInterface;
static final String TAG = BaseActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getApplicationContext().bindService(new Intent(this, CallService.class), this,
            BIND_AUTO_CREATE);
}

@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
    Log.e(TAG,"onServiceConnected");
    if (CallService.class.getName().equals(componentName.getClassName())) {
        mSinchServiceInterface = (CallService.SinchServiceInterface) iBinder;
        onServiceConnected();
    }
}

@Override
public void onServiceDisconnected(ComponentName componentName) {
    if (CallService.class.getName().equals(componentName.getClassName())) {
        mSinchServiceInterface = null;
        onServiceDisconnected();
    }
}

protected void onServiceConnected() {
    // for subclasses
}

protected void onServiceDisconnected() {
    // for subclasses
}

public  CallService.SinchServiceInterface getSinchServiceInterface() {
    return mSinchServiceInterface;
}

}

1 个答案:

答案 0 :(得分:0)

我没有发现您在推送

中使用推对数据设置SIN键的位置

https://www.sinch.com/docs/voice/android/#pushnotifications

或者你可以做Managedpush并且它很容易,请查看SDK下载中包含的样本