未从Android

时间:2016-06-15 07:41:44

标签: android firebase firebase-realtime-database

我正在尝试将新值推送到firebase数据库,但它没有显示在我的Firebase帐户中,即数据没有被推送。我在firebase控制台中给了读/写权限。互联网权限也包含所有最新的gradle依赖项。这是我试图推送数据的代码:

    mAuth = FirebaseAuth.getInstance();

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());


                // Write a message to the database
                FirebaseDatabase database = FirebaseDatabase.getInstance();
                DatabaseReference myRef = database.getReference();


                Map<String, Object> updates = new HashMap<String, Object>();
                updates.put("USER_ID", user.getUid());
                String version ="-";
                try {
                    PackageManager manager = getApplicationContext().getPackageManager();
                    PackageInfo info = manager.getPackageInfo(
                            getApplicationContext().getPackageName(), 0);
                    version = info.versionName;
                    updates.put("APP_VERSION", version);
                }
                catch(Exception e)
                {
                    updates.put("APP_VERSION", version);
                    e.printStackTrace();
                }
                updates.put("FIRE_TOKEN", FirebaseInstanceId.getInstance().getToken());
                myRef.setValue(updates);

                Log.d(TAG, "Database Executed.\nVersion:"+version+"\nUSER_ID:"+user.getUid()+"\nFIRE_TOKEN:"+FirebaseInstanceId.getInstance().getToken());



            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
            // ...
        }

    };

请注意,下面的行也完美地记录了所有值。

Log.d(TAG, "Database Executed.\nVersion:"+version+"\nUSER_ID:"+user.getUid()+"\nFIRE_TOKEN:"+FirebaseInstanceId.getInstance().getToken());

1 个答案:

答案 0 :(得分:0)

试试这样:
制作像这样的用户模型类

@IgnoreExtraProperties
public class User {

    public String username;
    public String email;

    public User() {
        // Default constructor required for calls to DataSnapshot.getValue(User.class)
    }

    public User(String username, String email) {
        this.username = username;
        this.email = email;
    }

@Exclude
public Map<String, Object> toMap() {
    HashMap<String, Object> result = new HashMap<>();
    result.put("username", username);
    result.put("email", email);


    return result;
}
}

然后用于保存firebase数据库中的数据

String key = mDatabase.child("user").push().getKey();
        User user = new User( username,email);
        Map<String, Object> postValues = user.toMap();

        Map<String, Object> childUpdates = new HashMap<>();
        childUpdates.put("/user/" + key, postValues);
        mDatabase.updateChildren(childUpdates);

希望有所帮助