Firebase + android 为我的应用程序的每个用户独立于设备生成唯一令牌的方法?

Firebase + android method for generating a unique token for each user of my application independent from the device?

大家晚上好,我使用 firebase 身份验证,我希望在注册时为每个用户生成一个唯一的令牌,以便我可以做其他事情。问题是我在 X 设备上创建的所有用户都具有相同的令牌。这是我使用的代码:

        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();
                                    }
                                }
                            }

                        });
                  }
             })

我在网上搜索了一遍又一遍google, youtube, blog ... 他们给出的大多数解决方案都是根据设备获得令牌之类的,这不好.我想使用之后的代币来及时发送通知

FirebaseInstanceId.getInstance().getInstanceId()返回的令牌与应用程序的用户无关。

如果您想为每个用户生成一个新的实例 ID,您需要在用户注销或新用户登录时清除现有的实例 ID。

在这里查看我的回答以获得更详细的解释:

另见:

  • How to handle multiple Firebase FCM tokens per user?
  • Receiving the same token for different users when using firebase messaging
  • Is FCM (firebase cloud messaging) Token for one device or for one account?