1

Good evening everyone, I use firebase authentication and I wish that at registration, a unique token for each user is generated so that I can do other things. The problem is that all the users I created on an X device, all have the same token. here is the code that i use:

        mAuth.createUserWithEmailAndPassword(email,pswd).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
            @Override
            public void onSuccess(AuthResult authResult) {
                                    // String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

                FirebaseInstanceId.getInstance().getInstanceId()
                        .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
                            @Override
                            public void onComplete(@NonNull final Task<InstanceIdResult> task) {
                                if (task.isSuccessful()) {

                                    final FirebaseUser mFirebaseUser = mAuth.getCurrentUser();
                                    if (mFirebaseUser != null) {
                                        String newuser = mFirebaseUser.getUid(); //Do what you need to do with the id
                                        Log.d(TAG, " COMPARAISON " + newuser);

                                        mFirebaseUser.getIdToken(true).addOnSuccessListener(new OnSuccessListener<GetTokenResult>() {
                                            @Override
                                            public void onSuccess(GetTokenResult result) {
                                                String token = result.getToken();
                                                //Do whatever
                                                Log.d(TAG, "GetTokenResult result = " + token);
                                                //String token = FirebaseInstanceId.getInstance().getToken();

                                                progressBar.setVisibility(View.VISIBLE);
                                                register.setVisibility(View.GONE);
                                                mAuth = FirebaseAuth.getInstance();

                                                String uid = mFirebaseUser.getUid();
                                                Log.d(TAG, " COMPARAION2" + uid);
                                                // String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

                                                Log.d(TAG, "Token utilisateur" + token);
                                                // Send token to your backend via HTTPS
                                                Log.d(TAG, " TOKEN " + token);
                                                Log.d(TAG, " UID DU USER" + uid);
                                                Map<String, Object> userdata = new HashMap<>();
                                                userdata.put("uid", uid);
                                                userdata.put("fln", fln);
                                                userdata.put("idToken", token);
                                                userdata.put("email", email);
                                                userdata.put("password", pswd);
                                                Log.d(TAG, "FULLNAME: " + fln);
                                                SharedPreferences.Editor editor = prefEnreg.edit();
                                                editor.putString("FULLNAME", fln);
                                                editor.putString("tok", token);
                                                editor.apply();
                                                db.collection("Users2").document(uid).set(userdata).addOnCompleteListener(new OnCompleteListener<Void>() {
                                                    @Override
                                                    public void onComplete(@NonNull Task<Void> task) {
                                                        Toast.makeText(getApplicationContext(), "Register User", Toast.LENGTH_SHORT).show();
                                                        register.setVisibility(View.VISIBLE);
                                                        progressBar.setVisibility(View.GONE);
                                                        Intent intent13 = new Intent(MainActivity.this, FriendList.class);
                                                        startActivity(intent13);
                                                        mail.setText("");
                                                        phone.setText("");
                                                        fllname.setText("");
                                                        passwd.setText("");

                                                    }

                                                });

                                            }
                                        });



                                    } else{
                                        Toast.makeText(getApplicationContext(),"Une erreur inconnue s'est produite",Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }

                        });
                  }
             })

I searched again and again on the net google, youtube, blog ... most of the solutions they give, it is to have the token according to the devices and like that, it is not good. I want to use the tokens after to send timely notifications

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Rolanddev
  • 49
  • 7

1 Answers1

0

The token returned by FirebaseInstanceId.getInstance().getInstanceId() is unrelated to the user of the app.

If you want to generate a new instance ID for each user, you'll want to clear the existing instance ID when the user signs out, or a new user signs in.

See my answer here for a longer explanation: When to register an FCM token for a user

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807