• 카테고리

    질문 & 답변
  • 세부 분야

    모바일 앱 개발

  • 해결 여부

    미해결

chat_messgages 생성이 안됩니다.

18.07.23 03:22 작성 조회수 162

0

하루 꼬박 강의 몇번반복해도...

sendMessage() 메서드 실행시 첫줄의

mChatMemeberRef = mFirebaseDb.getReference("chat_members").child(mChatId);

이게 작동을 안하는것 같습니다. RealTimeBase를 조회해도

  • chat_members
  • users

    밖에없네요... 뭐가 문제일까요??

    아래에 수업해주신 ChatActivity 소스코드 입니다.

    public class ChatActivity extends AppCompatActivity {

    private String mChatId;

    @BindView(R.id.senderBtn)

    ImageView mSenderButton;

    @BindView(R.id.edtContent)

    EditText mMessageText;

    private FirebaseDatabase mFirebaseDb;

    private FirebaseUser mFirebaseUser;

    private DatabaseReference mChatRef;

    private DatabaseReference mChatMemberRef;

    private DatabaseReference mChatMessageRef;

    private DatabaseReference mUserRef;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.content_chat);

    ButterKnife.bind(this);

    mChatId = getIntent().getStringExtra("chat_id");

    mFirebaseDb = FirebaseDatabase.getInstance();

    mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();

    mUserRef = mFirebaseDb.getReference("users");

    }

    @OnClick(R.id.senderBtn)

    public void onSendEvent(View v) {

    if (mChatId != null) {

    sendMessage();

    Log.d("sendMessgae","센드메시지 실행");

    } else {

    createChat();

    Log.d("createChat","크리트 채팅 실행");

    }

    }

    private void sendMessage() {

    //메세지 키 생성

    mChatMessageRef = mFirebaseDb.getReference("chat_messages").child(mChatId);

    // chat_message > {chat_id} > {message_id} > messageInfo

    String messageId = mChatMessageRef.push().getKey();

    Log.d("chat_messages", messageId);

    String messageText = mMessageText.getText().toString();

    if (messageText.isEmpty()) {

    return;

    }

    final TextMessage textMessage = new TextMessage();

    textMessage.setMessageText(messageText);

    textMessage.setMessageDate(new Date());

    textMessage.setChatId(mChatId);

    textMessage.setMessageId(messageId);

    textMessage.setMessageType(Message.MessageType.TEXT);

    textMessage.setReadUserList(Arrays.asList(new String[]{mFirebaseUser.getUid()}));

    String[] uids = getIntent().getStringArrayExtra("uids");

    if (uids != null) {

    textMessage.setUnreadCount(uids.length - 1);

    }

    mMessageText.setText("");

    mChatMemberRef.addListenerForSingleValueEvent(new ValueEventListener() {

    @Override

    public void onDataChange(final DataSnapshot dataSnapshot) {

    //unreadCount 셋팅하기 위한 대화 상대의 수 가져온다.

    long memberCount = dataSnapshot.getChildrenCount();

    textMessage.setUnreadCount((int) memberCount - 1);

    mChatMemberRef.child(textMessage.getMessageId()).setValue(textMessage, new DatabaseReference.CompletionListener() {

    @Override

    public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {

    Iterator<DataSnapshot> memberIterator = dataSnapshot.getChildren().iterator();

    while (memberIterator.hasNext()) {

    User chatMember = memberIterator.next().getValue(User.class);

    mUserRef.child(chatMember.getUid())

    .child("chats")

    .child(mChatId)

    .child("lastMessage")

    .setValue(textMessage);

    if (!chatMember.getUid().equals(mFirebaseUser.getUid())) {

    mUserRef.child(chatMember.getUid())

    .child("chats")

    .child(mChatId)

    .child("totalUnreadCount")

    .addListenerForSingleValueEvent(new ValueEventListener() {

    @Override

    public void onDataChange(DataSnapshot dataSnapshot) {

    long totalUnreadCount = dataSnapshot.getValue(long.class);

    dataSnapshot.getRef().setValue(totalUnreadCount+1);

    }

    @Override

    public void onCancelled(DatabaseError databaseError) {

    }

    });

    }

    }

    }

    });

    }

    @Override

    public void onCancelled(DatabaseError databaseError) {

    }

    });

    }

    private boolean isSentMessage = false;

    private void createChat() {

    //<방생성>

    //0. 방 정보 설정 <-- 기존 방이어야 가능함

    //1. 대화 상대가 내가 선택한 사람 추가

    //2. 각 상대별 chats에 방추가

    //3. 에너지 정보 중 읽은 사람에 내 정보 추가

    //4. 첫 메세지 전송

    final Chat chat = new Chat();

    mChatRef = mFirebaseDb.getReference("users").child(mFirebaseUser.getUid()).child("chats");

    mChatId = mChatRef.push().getKey();

    mChatMemberRef = mFirebaseDb.getReference("chat_members").child(mChatId);

    // mChatId = mChatRef.push() --> users >> uid >> chats >> chat_id 가져옴

    chat.setChatId(mChatId);

    chat.setCreateDate(new Date());

    //1:1 채팅

    String uid = getIntent().getStringExtra("uid");

    //1:n 채팅

    String[] uids = getIntent().getStringArrayExtra("uids");

    if (uid != null) {

    //1:1

    uids = new String[]{uid};

    }

    List<String> uidList = new ArrayList<>(Arrays.asList(uids));

    uidList.add(mFirebaseUser.getUid());

    // 방타이틀 == 나를제외한 사용자이름 출력

    final StringBuffer titleBuffer = new StringBuffer();

    for (String userId : uidList) {

    // 유저의 uid >> userInfo

    mUserRef.child(userId).addListenerForSingleValueEvent(new ValueEventListener() {

    @Override

    public void onDataChange(final DataSnapshot dataSnapshot) {

    User member = dataSnapshot.getValue(User.class);

    mChatMemberRef.child(member.getUid())

    .setValue(member, new DatabaseReference.CompletionListener() {

    @Override

    public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {

    // USERS >> chats >> {chat_id} >> chatinfo

    dataSnapshot.getRef().child("chats").child(mChatId).setValue(chat);

    if (!isSentMessage) {

    Log.d("센드메세지","센드메세지 실행");

    sendMessage();

    isSentMessage = true;

    }

    }

    });

    }

    @Override

    public void onCancelled(DatabaseError databaseError) {

    }

    });

    }

    }

    }

답변 0

답변을 작성해보세요.

답변을 기다리고 있는 질문이에요.
첫번째 답변을 남겨보세요!