我一直在关注如何使用parse.com制作聊天应用程序。这是链接:https://github.com/codepath/android_guides/wiki/Building-Simple-Chat-Client-with-Parse现在,应用程序的设置方式是所有用户都可以与拥有该应用程序的所有人聊天。我该怎么做才能发送邮件的用户可以选择一个收件人并且只与该收件人聊天而不是每个人? 解析聊天客户端的所有代码都与上面教程中的所有代码相同。
这是我的RecipientsActivity:
public class RecipientsActivity extends Activity {
public static final String TAG = RecipientsActivity.class.getSimpleName();
protected ParseRelation<ParseUser> mFriendsRelation;
protected ParseUser mCurrentUser;
protected List<ParseUser> mFriends;
protected MenuItem mSendMenuItem;
protected Uri mMediaUri;
protected String mFileType;
protected GridView mGridView;
protected String mTextMessage;
protected int mMemeImage;
protected String mTextTop;
protected String mTextBottom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.user_grid);
// Show the Up button in the action bar.
setupActionBar();
mGridView = (GridView) findViewById(R.id.friendsGrid);
mGridView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
mGridView.setOnItemClickListener(mOnItemClickListener);
TextView emptyTextView = (TextView) findViewById(android.R.id.empty);
mGridView.setEmptyView(emptyTextView);
mFileType = getIntent().getExtras().getString(
ParseConstants.KEY_FILE_TYPE);
if (mFileType.equals(ParseConstants.TYPE_TEXT_MESSAGE)) {
mTextMessage = getIntent().getExtras().getString(
ParseConstants.KEY_TEXT);
}
else if (mFileType.equals(ParseConstants.TYPE_MEME)) {
mMemeImage = getIntent().getExtras().getInt(
ParseConstants.KEY_MEME_IMAGE);
mTextTop = getIntent().getExtras().getString(
ParseConstants.KEY_MEME_TOP_TEXT);
mTextBottom = getIntent().getExtras().getString(
ParseConstants.KEY_MEME_BOTTOM_TEXT);
}
else {
mMediaUri = getIntent().getData();
}
}
@Override
public void onResume() {
super.onResume();
mCurrentUser = ParseUser.getCurrentUser();
mFriendsRelation = mCurrentUser
.getRelation(ParseConstants.KEY_FRIENDS_RELATION);
setProgressBarIndeterminateVisibility(true);
ParseQuery<ParseUser> query = mFriendsRelation.getQuery();
query.addAscendingOrder(ParseConstants.KEY_USERNAME);
query.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> friends, ParseException e) {
setProgressBarIndeterminateVisibility(false);
if (e == null) {
mFriends = friends;
String[] usernames = new String[mFriends.size()];
int i = 0;
for (ParseUser user : mFriends) {
usernames[i] = user.getUsername();
i++;
}
if (mGridView.getAdapter() == null) {
UserAdapter adapter = new UserAdapter(
RecipientsActivity.this, mFriends);
mGridView.setAdapter(adapter);
} else {
((UserAdapter) mGridView.getAdapter()).refill(mFriends);
}
} else {
Log.e(TAG, e.getMessage());
AlertDialog.Builder builder = new AlertDialog.Builder(
RecipientsActivity.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
/**
* Set up the {@link android.app.ActionBar}.
*/
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.recipients, menu);
mSendMenuItem = menu.getItem(0);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_send:
ParseObject message = createMessage();
if (message == null) {
// error
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.error_selecting_file)
.setTitle(R.string.error_selecting_file_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
} else {
send(message);
finish();
}
return true;
}
return super.onOptionsItemSelected(item);
}
protected ParseObject createMessage() {
ParseObject message = new ParseObject(ParseConstants.CLASS_MESSAGES);
message.put(ParseConstants.KEY_SENDER_ID, ParseUser.getCurrentUser()
.getObjectId());
message.put(ParseConstants.KEY_SENDER_NAME, ParseUser.getCurrentUser()
.getUsername());
message.put(ParseConstants.KEY_RECIPIENT_IDS, getRecipientIds());
message.put(ParseConstants.KEY_FILE_TYPE, mFileType);
if (mFileType.equals(ParseConstants.TYPE_TEXT_MESSAGE)) {
message.put(ParseConstants.KEY_TEXT, mTextMessage);
} else if (mFileType.equals(ParseConstants.TYPE_MEME)) {
message.put(ParseConstants.KEY_MEME_IMAGE, mMemeImage);
message.put(ParseConstants.KEY_MEME_TOP_TEXT, mTextTop);
message.put(ParseConstants.KEY_MEME_BOTTOM_TEXT, mTextBottom);
}
else {
// Process and attach media file
byte[] fileBytes = FileHelper.getByteArrayFromFile(this, mMediaUri);
if (fileBytes == null) {
return null;
} else {
if (mFileType.equals(ParseConstants.TYPE_IMAGE)) {
fileBytes = FileHelper.reduceImageForUpload(fileBytes);
}
String fileName = FileHelper.getFileName(this, mMediaUri,
mFileType);
ParseFile file = new ParseFile(fileName, fileBytes);
message.put(ParseConstants.KEY_FILE, file);
return message;
}
}
return message;
}
protected ArrayList<String> getRecipientIds() {
ArrayList<String> recipientIds = new ArrayList<String>();
for (int i = 0; i < mGridView.getCount(); i++) {
if (mGridView.isItemChecked(i)) {
recipientIds.add(mFriends.get(i).getObjectId());
}
}
return recipientIds;
}
protected void send(ParseObject message) {
message.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
// success!
Toast.makeText(RecipientsActivity.this,
R.string.success_message, Toast.LENGTH_LONG).show();
sendPushNotifications();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(
RecipientsActivity.this);
builder.setMessage(R.string.error_sending_message)
.setTitle(R.string.error_selecting_file_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
protected OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (mGridView.getCheckedItemCount() > 0) {
mSendMenuItem.setVisible(true);
} else {
mSendMenuItem.setVisible(false);
}
ImageView checkImageView = (ImageView) view
.findViewById(R.id.checkImageView);
if (mGridView.isItemChecked(position)) {
// add the recipient
checkImageView.setVisibility(View.VISIBLE);
} else {
// remove the recipient
checkImageView.setVisibility(View.INVISIBLE);
}
}
};
protected void sendPushNotifications() {
ParseQuery<ParseInstallation> query = ParseInstallation.getQuery();
query.whereContainedIn(ParseConstants.KEY_USER_ID, getRecipientIds());
// send push notification
ParsePush push = new ParsePush();
push.setQuery(query);
push.setMessage(getString(R.string.push_message, ParseUser
.getCurrentUser().getUsername()));
push.sendInBackground();
}
}
答案 0 :(得分:0)
这不是个好主意,像这种情况构建应用程序。更好的使用套接字。 但是,如果你想为此使用解析,你可以进行下一步: 1.对所有消息使用标志 - 对于公共消息,例如flag = all,对于个人标志= userID;看起来像
ParseObject message = new ParseObject("Message");
message.put(USER_ID_KEY, sUserId);
message.put("body", data);
message.put("flag", userID); // this id of user who must receive it
在模型类中添加字段
public String getFlag() {
return getString("flag");
}
在适配器中添加下一个
final boolean isPrivateMessageForMe = message.getFlag().equals(mUserId);
然后查看(isMe)部分
`if(isPrivateMessageForMe){textView.setText(&#34;它的私信,没有人能看到它除了你&#34;);} else {view.setVisibility(View.GONE);}
UPD:目标发送 - &gt; 如果您想发送消息,对于具有目标用户自动ID查询的具体用户,您必须实现下一步:
在ChatListAdapter中添加界面:
public interface DestinationListener{
void userCheckDestinationUserInChat(int destUserId);
}
private DestinationListener destinationListener;
public void setOnUserCheckTargetForMessaging(DestinationListener destinationListener){
this.destinationListener = listener;
}
然后在getView方法中添加下一个 return convertView;
convertView.setOnClickListener( new ....
onClick(){
if(destinationListener != null){
destinationListener.userCheckDestinationUserInChat(message.getUserId())
}
});
在适配器初始化后的setupMessagePosting()方法中mAdapter = new ChatListAdapter(ChatActivity.this,sUserId,mMessages);添加下一个:
mAdapter.setOnUserCheckTargetForMessaging(new .....
@Overide
void userCheckDestinationUserInChat(int destUserId){
etMessage.setText("message for: "+destUserId+", ");
});
像这样的东西。尝试查找有关接口的更多信息。它可以帮助您解决同样的问题