我正在制作一个聊天应用程序,并使用firebase电话授权和quickblox聊天api。
我的代码是:
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d("test", "signInWithCredential:success");
FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();
mUser.getToken(false).addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
@Override
public void onComplete(@NonNull Task<GetTokenResult> task) {
if(task.isSuccessful()){
String m=task.getResult().getToken();
signIn(m);
}
}
});
public void signIn(String token){
QBUsers.signInUsingFirebase(projectId, token).performAsync( new QBEntityCallback<QBUser>() {
@Override
public void onSuccess(QBUser user, Bundle args) {
messenger.com.nowchat.helper.DataHolder.getInstance().setSignInQbUser(user);
Intent intent = new Intent(Registration.this, WelcomeProfile.class);
startActivity(intent);
finish();
}
}
但问题是我的代币在4到5小时后过期。当我清除缓存或重新安装应用程序时,它将再次工作5小时。
最佳答案
您可以使用qbsessionlistener来监听qbsession状态。因此,在回调onprovidersessionexpired(字符串提供程序)中,您可以获取当前的firebase令牌并续订qbsession。在代码中,它将如下所示:
@Override
public void onProviderSessionExpired(String provider) {
if (!QBProvider.FIREBASE_PHONE.equals(provider)){
return;
}
final String projectId = "your.project.id";
FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();
mUser.getToken(true)
.addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
public void onComplete(@NonNull Task<GetTokenResult> task) {
if (task.isSuccessful()) {
String authToken = task.getResult().getToken();
QBUsers.signInUsingFirebase(projectId, authToken).performAsync(new QBEntityCallback<QBUser>() {
@Override
public void onSuccess(QBUser qbUser, Bundle bundle) {
qbUser.setPassword(QBSessionManager.getInstance().getToken());
QBChatService.getInstance().login(qbUser, new QBEntityCallback() {
@Override
public void onSuccess(Object o, Bundle bundle) {
//your actions after success login to the chat
}
@Override
public void onError(QBResponseException e) {
}
});
}
@Override
public void onError(QBResponseException e) {
}
});
} else {
// Handle error -> task.getException();
}
}
});
}