Search code examples
androidparse-platform

Making a chat application with parse.com and choosing recipients


I've been stuck on this for a couple of days now. I made a chat application using parse.com with this tutorial at https://github.com/codepath/android_guides/wiki/Building-Simple-Chat-Client-with-Parse How do I make it so that the user can select a recipient (another parseUser)? I want to make it so that the app will be one on one chatting or as a group chat. Right now the app doesn't have a "choose a recipient" option so the chats are coming in to every parseUser.


Solution

  • Add to the Message.java:

    ...
    
    public String getReceiverId() {
        return getString("receiverId");
    }
    
    public void setReceiverId(String receiverId) {
        put("receiverId", receiverId);
    }
    
    ...
    

    Then in the ChatActivity.java you need to send the id of the friend you want to send a message to. I use intent to pass it through on clicking on a friend in a listview.

    private static String sFriendsId;
    ....
    
    Intent callingIntent = getIntent();
    sFriendsId = callingIntent.getExtras().getString("friendsObjectId");
    

    Then under the setupMessagePosting() method you need to set the receiver id:

       // When send button is clicked, create message object on Parse
        btSend.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                String body = etMessage.getText().toString();
                // Use Message model to create new messages now
                Message message = new Message();
                message.setUserId(sUserId);
                message.setReceiverId(sFriendsId);
                message.setBody(body);
                message.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        receiveMessage();
                    }
                });
                etMessage.setText("");
            }
        });
    

    Lastly add to the receiveMessage() method:

    ...
        // Sent Messages Query
        ParseQuery<Message> sentMessagesQuery = ParseQuery.getQuery(Message.class);
        sentMessagesQuery.whereEqualTo("userId", sUserId);
        sentMessagesQuery.whereEqualTo("receiverId", sFriendsId);
    
        // Receiver Messages Query
        ParseQuery<Message> receiveMessagesQuery = ParseQuery.getQuery(Message.class);
        receiveMessagesQuery.whereEqualTo("userId", sFriendsId);
        receiveMessagesQuery.whereEqualTo("receiverId", sUserId); //receiver is me (current user)
    
        // Combine the queries
        List<ParseQuery<Message>> queries = new ArrayList<>();
        queries.add(sentMessagesQuery);
        queries.add(receiveMessagesQuery);
    
        // Get the messages
        ParseQuery<Message> mainQuery = ParseQuery.or(queries);
        // Configure limit and sort order
        mainQuery.setLimit(MAX_CHAT_MESSAGES_TO_SHOW);
        mainQuery.orderByAscending("createdAt");
    
        mainQuery.findInBackground(new FindCallback<Message>() {
    ....