Stfalcon / ChatKit库实现

时间:2019-03-20 20:09:46

标签: java android firebase-realtime-database chatkit

有人对Stfalcon / ChatKit库有清晰的解释或教程吗?

我到目前为止所做的-

public void retrieveMessages() {


        FirebaseFirestore db = FirebaseFirestore.getInstance();
        final String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

        db.collection("Messages").whereEqualTo("UID", uid ).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {

            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {

                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        if (document != null) {

                            final String jobID = document.getString("JobID");
                            final String UserID = document.getString("UserID");
                            final String name = document.getString("Name");
                            final String company = document.getString("Company");
                            final String UID = document.getString("UID");

                            DatabaseReference mDatabase;

                            mDatabase = FirebaseDatabase.getInstance().getReference();


                            mDatabase.child(jobID).child(UserID).child("Messages").orderByKey().addListenerForSingleValueEvent(new ValueEventListener() {

                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {

                                    fetchMessages((Map<String,Object>) dataSnapshot.getValue());






                                }

                                @Override
                                public void onCancelled(DatabaseError databaseError) {
                                }
                            });

                        } else {
                            Log.d("LOGGER", "No such document");
                        }
                    }
                }
            }





        });

    }

    private void fetchMessages(Map<String,Object> users) {

        ArrayList<String> messages = new ArrayList<>();

        //iterate through each user, ignoring their UID
        for (Map.Entry<String, Object> entry : users.entrySet()){

            //Get user map
            Map singleUser = (Map) entry.getValue();
            //Get phone field and append to list
            messages.add((String) singleUser.get("MessageBody"));
        }


        messagesN = messages;


        }

    public Message getTextMessage() {
        return getTextMessage(getRandomMessage());
    }

    public Message getTextMessage(String text) {

        String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
        return new Message(getRandomid(),text, new Author(getRandomName(),uid));
    }

    static ArrayList<String> messagesN = new ArrayList<>();


    static String getRandomMessage() {
        return messagesN.toString();
    }

    static final ArrayList<String> idN = new ArrayList<String>() {
        {
            add("sdfdsfds");
            add("sdfdsfdsf");

        }
    };

    static String getRandomid() {
        return idN.get(rnd.nextInt(idN.size()));
    }

    static final ArrayList<String> Name = new ArrayList<String>() {
        {
            add("sdfdsfds");
            add("sdfdsfdsf");

        }
    };

    static String getRandomName() {
        return Name.get(rnd.nextInt(Name.size()));
    }


    public ArrayList<Message> getMessages(Date startDate) {
        ArrayList<Message> messages = new ArrayList<>();
        for (int i = 0; i < 10/*days count*/; i++) {


            Message message;
            message = getTextMessage();


            Calendar calendar = Calendar.getInstance();

            message.setCreatedAt(calendar.getTime());
            messages.add(message);

        }

        return messages;
    }


    protected void loadMessages() {
        new Handler().postDelayed(new Runnable() { //imitation of internet connection
            @Override
            public void run() {
                ArrayList<Message> messages = getMessages(lastLoadedDate);
                messagesAdapter.addToEnd(messages, false);
            }
        }, 1000);
    }



}

但是问题是,我没有按预期从firebase提取消息, 而是将其显示为类似以下内容-[Hello,Hello,Hello]。如何正确实现此库?非常感谢您的帮助。

链接到库-https://github.com/stfalcon-studio/ChatKit

谢谢。

0 个答案:

没有答案