【发布时间】:2021-08-15 05:19:48
【问题描述】:
我正在构建一个聊天应用程序并使用 FCM 服务。
当用户注册应用程序时,我可以通过
String token1; FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if (!task.isSuccessful()) {
Log.w("TTTTTTTTTTTT", "Fetching FCM registration token failed", task.getException());
return;
}
// Get new FCM registration token
token1 = task.getResult(); //this is the token
// Log
Log.d("TTTTTTTTTTTT", token1);
}
});
现在,在获得令牌后,我想将其与关联用户一起存储在 Firebase 数据库中。
所以用户会有电子邮件、密码、城市和令牌。
signUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String em = email.getText().toString();
String pass = password.getText().toString();
final String na = name.getText().toString();
final String ci = city.getText().toString();
if (em.equals("") || pass.equals("") || na.equals("") || ci.equals("")) {
name.setError("Enter your name");
password.setError("Enter your password");
city.setError("Enter your city");
email.setError("Enter your email");
Toast.makeText(ActivitySignup.this, "Please fill all the Fields", Toast.LENGTH_SHORT).show();
} else {
progressBar.setVisibility(View.VISIBLE);
FirebaseAuth.getInstance().createUserWithEmailAndPassword(em, pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
progressBar.setVisibility(View.GONE);
FirebaseDatabase.getInstance().getReference().child("USERS").
child(task.getResult().getUser().getUid()).
setValue(new UserModel(em, na, ci,token1 )); //Creating a new user and storing name ,email, city and token in database
startActivity(new Intent(ActivitySignup.this, YearChooseActivity.class).putExtra("activity_name", "ActivitySignUp")
.putExtra("timesVisited", timesVisited));
Toast.makeText(ActivitySignup.this, "Successfully Register User \n" + em, Toast.LENGTH_SHORT).show();
} else {
progressBar.setVisibility(View.GONE);
Toast.makeText(ActivitySignup.this, Objects.requireNonNull(Objects.requireNonNull(task.getException()).getMessage()).toString(), Toast.LENGTH_LONG).show();
}
}
});
}
}
});
FirebaseDatabase.getInstance().getReference().child("USERS").child(task.getResult().getUser().getUid()).setValue(new UserModel(email, name, city , token1 )); //Creating a new user and storing name ,email,
问题是令牌没有存储在 Firebase 中。当我传递一个随机字符串作为令牌参数时,假设我传递“Kobe will be remembered”作为参数
FirebaseDatabase.getInstance().getReference().child("USERS").child(task.getResult().getUser().getUid()).setValue(new UserModel(email, name, city ," Kobe will be remembered" )); //Creating a new user and storing name ,email,
然后它会很好地存储在数据库中。
当我与测试用户注册时,假设 name=a , email = a@gmail.com , city = a 然后在实时数据库中 token 是没有保存。这就是实时数据库的外观。
"aBnS2fuXo2gmvcxzqy3z3mgFpiv2" : {
"city" : "a",
"email" : "a@gmail.com",
"name" : "a"
}
但是,当我将 token 值硬编码为随机字符串时,可以使用 FirebaseDatabase.getInstance().getReference().child("USERS").child(task.getResult().getUser().getUid()).setValue(new UserModel(email, name, city ,"Arnold" ));
那么数据库看起来像
"aBnS2fuXo2gmvcxzqy3z3mgFpiv2" : {
"city" : "a",
"email" : "a@gmail.com",
"name" : "a"
"userToken" : "Arnold",
}
为了通知我需要对方的令牌,因此我如何将所有用户的令牌连同他们的其他凭据一起存储在 firebase 中?
【问题讨论】:
-
您在代码中的哪个位置使用
FirebaseDatabase.getInstance().getReference().child("USERS").child(task.getResult().getUser().getUid()).setValue(new UserModel(email, name, token1 ));? -
我已经更新了帖子并添加了我正在尝试的任何内容,我们将不胜感激。
-
我仍然看不到你的代码在哪里使用
FirebaseDatabase.getInstance().getReference().child("USERS").child(task.getResult().getUser().getUid()).setValue(new UserModel(email, name, token1 ));。 -
编辑了格式,现在你可以清楚地看到它
标签: java android firebase firebase-realtime-database firebase-cloud-messaging