【问题标题】:Firebase Realtime database from android来自 Android 的 Firebase 实时数据库
【发布时间】:2023-01-11 14:38:21
【问题描述】:

尝试从 android 连接 firebase 实时数据库,但无法将数据保存在 firebase 中。 我们在 firebase 的一个项目中有多个应用程序,因此数据库具有不同的 db url。访问数据库

此代码用于生成令牌

  FirebaseInstanceId.getInstance().getInstanceId()
                .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
                    @Override
                    public void onComplete(@NonNull Task<InstanceIdResult> task) {
                        if (!task.isSuccessful()) {
                            return;
                        }
                        token = task.getResult().getToken();

                        Log.e("token", token);
                    }
                });

这是为了在 firebase 中保存数据

  try {
                        //Save the location data to the database//
                        DatabaseReference ref = FirebaseDatabase.getInstance("firebase db path").
                                getReference("user" + authref.getUid());
                        ref.child(token);
                        Location location = locationResult.getLastLocation();
                        if (location != null) {
                            FirebaseLocationDTO locdto = new FirebaseLocationDTO();
                            locdto.setLatitude(location.getLatitude());
                            locdto.setLongitude(location.getLongitude());                           
                            locdto.setOsVersion(Build.VERSION.RELEASE);
                            locdto.setUserId(userId);
                            locdto.setAppVersionName(BuildConfig.VERSION_NAME);
                            locdto.setAppType(BaseConstant.FIREBASE_APP_TYPE);

                            ref.child(token).setValue(locdto);
                        }
                    } catch (Exception E) {
                        Log.e("error fb",E.toString());
                     
                    }

【问题讨论】:

    标签: java android firebase firebase-realtime-database


    【解决方案1】:

    你允许读/写 Firebase Realtime Database Rules

    【讨论】:

      猜你喜欢
      • 2019-12-26
      • 1970-01-01
      • 2017-06-03
      • 2020-07-23
      • 2020-03-29
      • 2020-12-12
      • 2021-11-01
      • 2021-08-23
      • 1970-01-01
      相关资源
      最近更新 更多