我正在使用Android Studio和Firebase。一切工作正常,但是一段时间以来,我的应用程序中无法接受新用户。 在验证注册成功之后,我的应用程序从头开始重新启动注册过程。 即使自2019年5月27日以来我未进行任何更改,Android Studio仍返回以下错误:
E/StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.firebase.internal.api.FirebaseNoSignedInUserException: Please sign in before trying to get a token.
我试图像这样更新数据库规则:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=****} {
allow read, write;
}
}
}
这是我的主要活动:
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener,
OnFragmentInteractionListener, InternetCallbackListener{
FirebaseStorage storage = FirebaseStorage.getInstance();
private GoogleApiClient client;
protected GoogleHelper googleHelper;
private FirebaseAuth mAuth;
private GoogleSignInClient mGoogleSignInClient;
// [...]
@Override
protected void onCreate(Bundle savedInstanceState) {
if (!isTaskRoot()) {
finish();
return;
}
super.onCreate(savedInstanceState);
// Configure Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken("xxxxxxxxxxxx.apps.googleusercontent.com")
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
mAuth = FirebaseAuth.getInstance();
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, Statics.REQUEST_SIGN_IN);
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
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
FirebaseUser user = mAuth.getCurrentUser();
// updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
}
// ...
}
});
}
// [...]
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
//updateUI(currentUser);
mAuth.signInAnonymously()
.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(TAG, "signInAnonymously:success");
FirebaseUser user = mAuth.getCurrentUser();
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInAnonymously:failure", task.getException());
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
AppIndex.AppIndexApi.start(client, getIndexApiAction());
String uuid = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
dbHelper2.getUser(uuid, new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot document) {
thisUser = document.getData();
if (thisUser == null) {
// go to user profile activity to register
Intent i = new Intent(context, SubscriptionActivity.class);
//i.putExtra("userId", null);
context.startActivity(i);
}
}
});
// [...]
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onResult");
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == Statics.REQUEST_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
// ...
}
}
谢谢您的帮助。