【问题标题】:How can I store FireBase Cloud Messaging tokens in firebase database in Android?如何将 FireBase 云消息传递令牌存储在 Android 的 firebase 数据库中?
【发布时间】: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" ));

说“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


【解决方案1】:

您绝对不要将它们存储在同一位置,这不是良好的安全做法。此外,用户可能会退出或更换您不想将通知发送到旧设备的手机。

将 FCM 令牌保存在用户信息或子集合下。 然后,如果您想使用该令牌向他们发送消息,您只需使用他们的 UID 获取他们的令牌并向他们发送消息。您可以使用云功能安全地做到这一点。

【讨论】:

  • 如何“保存用户信息下的 FCM 代币”,请提供更多信息。我已经尝试将令牌存储在 sharedPreferences 中(只是为了尝试),但它没有被存储。同样,如何将令牌保存在 firebase 实时数据库中?
  • 1- 它与 sharedPreferences 无关,您需要将此信息保存在您的数据库中。 2-您需要在本地请求 FCM 并获取 FCM 令牌 3-将此令牌添加到您在数据库中保存用户信息的位置
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-21
  • 2019-04-05
  • 2022-08-19
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多