0

I am new to firebase, I have a login screen. Post successful login, i would like to invoke the getToken method cause along with the Token i am also saving User loginid in centralised server. Hence after login i am calling these two lines.

FirebaseMessaging.getInstance().subscribeToTopic("test");
FirebaseInstanceId.getInstance().getToken();

My services is as follows

public class FirebaseInstanceIDService extends FirebaseInstanceIdService {

    public static  String busid;


    @Override
    public void onTokenRefresh() {
        String token = FirebaseInstanceId.getInstance().getToken();
        registerToken(token);
    }

    private void registerToken(String token) {


     OkHttpClient client = new OkHttpClient();
     business_login_id=Lib.GetPref(FirebaseInstanceIDService.this, "KEY_USER_ID", "");


        RequestBody body = new FormBody.Builder()
                .add("token",token)
                .add("business_login_id", business_login_id)
                .build();

  Lib.Debug("Calling URL with body "+body.toString());

        Request request = new Request.Builder()
                .url("http://mywebsite.com/gcm/register.php")
                .post(body)
                .build();

        try {
            client.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

My question is : Why Service FirebaseInstanceIDService is executing irrespective of Post Login screen and creating the Token id instantly for the app (in my case business_login_id is null).

At this time my user id is not logged in yet. Is there any way to control exactly POST login with the right credential create the Token NOW?

Android Rao
  • 157
  • 1
  • 10
  • Wondering why my question is down voted..Its a unique problem when you are persisting multiple elements in Database server. – Android Rao Aug 16 '16 at 13:52

2 Answers2

2

A FirebaseInstanceIdService is automatically started when you app is first installed. It does not wait until the user is signed in, nor does it have to: the token can be gotten without the user being signed in.

But it is often the case that you cannot save the token (e.g. to a hosted database) until the user is authenticated. So you'll need to either hang on to the token somewhere in the app OR request the token later.

Hang on to the token

You can store the token in Shared Preferences until you app is ready to consume it:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("token", token);
editor.commit();

And then once the user signs in:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String token = sharedPref.getInt(token, null);
registerToken(token);

Request the token later

Firebase Cloud Messaging will generate a token as soon as the app is installed. So it is very likely that by the time your user signs in, the token has long been generated. In that case, you can also simply request the token after the user has signed in.

If you'd be using Firebase Authentication, that'd be something like:

FirebaseAuth.getInstance().addAuthStateListener(new FirebaseAuth.AuthStateListener() {
    public void onAuthStateChanged(FirebaseAuth firebaseAuth) {
        FirebaseUser user = firebaseAuth.getCurrentUser();
        if (user != null) {
            String token = FirebaseInstanceId.getInstance().getToken();
            registerToken(token);
        }
    }
};
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Perfect... This is exactly what i am doing now. Just wanted to confirm my workaround is the right one. Many thanks Frank. – Android Rao Aug 16 '16 at 13:43
0

For Firebase no need to check more references. Just go through this sample code and it's 100% working.

https://github.com/firebase/quickstart-android

ViramP
  • 1,659
  • 11
  • 11