How to use the firebase translation detector database

I am trying to use a Firebase database with BroadcastReceiver to retrieve some data. Unfortunately, sometimes the user is null , which means that I cannot use the UID for the sample data.

 @Override public void onReceive(final Context context, Intent intent) { FirebaseAuth.getInstance().addAuthStateListener(new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { FirebaseUser user = firebaseAuth.getCurrentUser(); if(user != null){ //sometimes not null }else{ //sometimes null } } }); } 

Also FirebaseAuth.getInstance().getCurrentUser sometimes null . In addition, I do not run much time in my BroadcastReceiver . I am talking about getting one opjes without any nodes under it.

How can I use a Firebase database with BroadcastReceiver ?

+7
android firebase-database firebase-authentication
source share
4 answers

I think I get it. The problem was not in the translator, but in firebase updating the token. At this point, the user was null . Something like this worked:

 @Override public void onReceive(final Context context, Intent intent) { FirebaseAuth.getInstance().addAuthStateListener(new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { FirebaseUser user = firebaseAuth.getCurrentUser(); if(user != null){ FirebaseAuth.getInstance().removeAuthStateListener(this); //do stuff }else{ //do nothing } } }); } 
0
source share

Try delegating a service like this

 public class AuthenticationCheckService extends IntentService { public AuthenticationCheckService() { super("AuthenticationCheckService"); } @Override protected void onHandleIntent(@Nullable Intent intent) { //Once this code runs and its done the service stops so need to worry about long running service checkAuthenticationStatus(); } private void checkAuthenticationStatus() { FirebaseAuth.getInstance().addAuthStateListener(new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { FirebaseUser user = firebaseAuth.getCurrentUser(); if (user != null) { //Do stuffs } else { //Resolve to authentication activity } } }); } } 

Then start the service inside the onReceive method, for example,

 @Override public void onReceive(final Context context, Intent intent) { //Method implemented below checkAuthStatus(context); } private void checkAuthStatus(Context context) { Intent authCheckIntent = new Intent(context, AuthenticationCheckService.class); context.startService(authCheckIntent); } 

then be sure to register the service in your manifest file

  <service android:name=".AuthenticationCheckService" /> 
+4
source share

BroadcastReceiver not intended for lengthy operations, try starting the IntentService from this receiver. Android will end this receiver if it takes too long. Perhaps for this reason, sometimes the result is null.

+3
source share

Confirm that the user / device is signed in / Log in to Firebase for more information about user authentication. Configure Firebase Authentication for Android

0
source share

All Articles