使用parse.com创建聊天应用程序并选择收件人

时间:2015-07-03 12:44:41

标签: android parse-platform

我已经被困在这几天了。我使用parse.com在https://github.com/codepath/android_guides/wiki/Building-Simple-Chat-Client-with-Parse 上创建了一个聊天应用程序,如何使用户可以选择收件人(另一个parseUser)?我想这样做该应用程序将一对一聊天或作为群聊。现在该应用程序没有"选择收件人"选项,以便聊天进入每个parseUser。

2 个答案:

答案 0 :(得分:2)

添加到Message.java:

...

public String getReceiverId() {
    return getString("receiverId");
}

public void setReceiverId(String receiverId) {
    put("receiverId", receiverId);
}

...

然后在ChatActivity.java中,您需要发送要发送消息的朋友的ID。我使用意图通过点击列表视图中的朋友来传递它。

private static String sFriendsId;
....

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

然后在setupMessagePosting()方法下,您需要设置接收者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("");
        }
    });

最后添加到receiveMessage()方法:

...
    // 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>() {
....

答案 1 :(得分:0)

要发送您要向其发送消息的朋友的ID。在列表视图中单击朋友时,必须使用意图来传递它。 就是这样:

userListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(getApplicationContext(), MessageListActivity.class);
            intent.putExtra("username", usersId.get(position));
            Log.i("user", users.get(position));
            startActivity(intent);
        }
    });

查询:

ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.whereNotEqualTo("username", ParseUser.getCurrentUser().getUsername());
    query.findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(List<ParseUser> objects, ParseException e) {
            if(e==null){
                if(objects.size() >0){
                    for(ParseUser user : objects){
                        users.add(user.getUsername());
                        usersId.add(user.getObjectId());
                    }
                    arrayAdapter.notifyDataSetChanged();
                }
            }
        }
    });