我正在制作一个聊天应用程序,并使用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 状态。因此,在回调 onProviderSessionExpire(String provider( 中,您可以获取当前的 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();
}
}
});
}
过期没关系
据我了解,onVerificationComplete回调将在两种情况下触发:
- 首次登录(通过短信确认(
- 代币续订
https://firebase.google.com/docs/auth/android/phone-auth#verification-callbacks
因此,您必须一直收听它,如果它已续订 - 然后再次调用QuickBlox登录