如何让用户在firebase中拥有自己的聊天室和地图标记?

时间:2017-07-23 08:03:14

标签: java android google-maps firebase

我创建了一个应用程序,可让用户登录并在谷歌地图上设置标记,标记连接到属于设置它的用户的聊天室。

现在的问题是,无论我如何尝试,我都无法在Chatactivity和MapFragment中获取uid,我不知道问题是什么 这是三项活动:

有人可以帮帮我~~

LoginActivity:

unsigned short int

ChatActivity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    auth = FirebaseAuth.getInstance();

    if (auth.getCurrentUser() != null) {
        startActivity(new Intent(LoginActivity.this, MainActivity.class));
        finish();
    }
    //authenticate user
    auth.signInWithEmailAndPassword(email, password)
            .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    // If sign in fails, display a message to the user. If sign in succeeds
                    // the auth state listener will be notified and logic to handle the
                    // signed in user can be handled in the listener.
                    if (!task.isSuccessful()) {
                        // there was an error
                        if (password.length() < 6) {
                            edUserid.setError("密碼太短,請輸入超過6個字元!");
                        } else {
                            Toast.makeText(LoginActivity.this, "登入失敗", Toast.LENGTH_LONG).show();
                        }
                    } else {
                        Toast.makeText(getApplicationContext(),"登入成功",Toast.LENGTH_LONG).show();
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));

                        finish();
                    }
                }
            });




}

}

MapFragment(部分内容):

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            EditText input = (EditText)findViewById(R.id.input);

            // Read the input field and push a new instance
            // of ChatMessage to the Firebase database
            FirebaseDatabase.getInstance()
                    .getReference(CHAT_PATH)
                    .push()
                    .setValue(new ChatMessage(input.getText().toString(),
                            FirebaseAuth.getInstance()
                                    .getCurrentUser()
                                    .getDisplayName())
                    );

            // Clear the input
            input.setText("");
        }
    });
    ListView listOfMessages = (ListView)findViewById(R.id.list_of_messages);

    adapter = new FirebaseListAdapter<ChatMessage>(this, ChatMessage.class,
            R.layout.message, FirebaseDatabase.getInstance().getReference(CHAT_PATH)) {
        @Override
        protected void populateView(View v, ChatMessage model, int position) {
            // Get references to the views of message.xml
            TextView messageText = (TextView)v.findViewById(R.id.message_text);
            TextView messageUser = (TextView)v.findViewById(R.id.message_user);
            TextView messageTime = (TextView)v.findViewById(R.id.message_time);

            // Set their text
            messageText.setText(model.getMessageText());
            messageUser.setText(model.getMessageUser());

            // Format the date before showing it
            messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",
                    model.getMessageTime()));
        }
    };

    listOfMessages.setAdapter(adapter);

}

1 个答案:

答案 0 :(得分:0)

您的数据库结构应如下所示:

  • 通讯录

    • [用户id]
      • 电子邮件
      • 密码
  • 标记

    • [用户ID]
      • 纬度
      • 经度
      • UID

聊天

  • [用户ID]
    • [来自推送的keyMessage]
      • MessageText中
      • messageTime
      • messageUser

使用此结构,您可以从标记获取聊天室ID,并且您也可以使用userId从标记显示用户信息。

而不是使用push中的userId,而是使用firebase auth中的uid会更好。

auth.createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener(CreateActivity.this, new 
          OnCompleteListener<AuthResult>() {

            public void onComplete( Task<AuthResult> task) {
                Toast.makeText(CreateActivity.this, "創建成功,請重新登 入", Toast.LENGTH_SHORT).show();

                // If sign in fails, display a message to the user. If sign in succeeds
                // the auth state listener will be notified and logic to handle the
                // signed in user can be handled in the listener.
                if (!task.isSuccessful()) {
                    Toast.makeText(CreateActivity.this, "認證失敗或帳號已 存在" , Toast.LENGTH_SHORT).show();
                } else {
                // insert user to database
                    FirebaseDatabase database = FirebaseDatabase.getInstance();
                    String currentUid = auth.getCurrentUser().getUid();
                    DatabaseReference myRef = database.getReference("Contacts/" + currentUid);
                    ContactInfo contact1 = new ContactInfo(email,id,password);
                    myRef.setValue(contact1);//將會員資料寫入FIREBASE
                    startActivity(new Intent(CreateActivity.this, MainActivity.class));
                    finish();
                }
            }
        });


}