Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
JIHOON JUNG
10,927 Pointspush notification, chat message
Hello, Friends
I almost creating chat application except for push notification.
This is my code below
package com.oikostudio.famchat.ui;
import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.support.v4.app.FragmentActivity; import android.text.InputType; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.AbsListView; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast;
import com.oikostudio.famchat.R; import com.oikostudio.famchat.adapter.ChatAdapter; import com.oikostudio.famchat.constants.ParseConstants; import com.oikostudio.famchat.fragment.FriendsFragment; import com.oikostudio.famchat.model.Conversation; import com.parse.FindCallback; import com.parse.ParseException; import com.parse.ParseInstallation; import com.parse.ParseObject; import com.parse.ParsePush; import com.parse.ParseQuery; import com.parse.ParseUser; import com.parse.SaveCallback;
import java.util.ArrayList; import java.util.Date; import java.util.List;
public class ChatActivity extends FragmentActivity {
private ArrayList<Conversation> mConversationArrayList;
private ChatAdapter mChatAdapter;
private EditText mChatEditText;
private String mBuddy;
private ListView mChatMessageList;
private Button mSendChatMessageButton;
private Date mLastMessageDate;
private boolean isRunning;
private static Handler mHandler;
private String chatMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
// ConvertList
mConversationArrayList = new ArrayList<Conversation>();
mChatMessageList = (ListView) findViewById(R.id.chatMessageList);
// Adapter into ListView
mChatAdapter = new ChatAdapter(this, mConversationArrayList);
mChatMessageList.setAdapter(mChatAdapter);
mChatMessageList.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
mChatMessageList.setStackFromBottom(true);
// UserName
TextView ChatUserName = (TextView) findViewById(R.id.chatUserName);
mBuddy = getIntent().getStringExtra(ParseConstants.EXTRA_DATA);
ChatUserName.setText(mBuddy);
mChatEditText = (EditText) findViewById(R.id.chatMessageText);
mChatEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
mHandler = new Handler();
mSendChatMessageButton = (Button) findViewById(R.id.sendChatMessageButton);
mSendChatMessageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendMessage();
}
});
}
@Override
protected void onResume() {
super.onResume();
isRunning = true;
loadConversationList();
}
@Override
protected void onPause() {
super.onPause();
isRunning = false;
}
protected void sendMessage() {
// Toast.makeText(this, "Send Message OK", Toast.LENGTH_LONG).show();
if (mChatEditText.length() == 0) {
return;
}
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(mChatEditText.getWindowToken(), 0);
chatMessage = mChatEditText.getText().toString();
final Conversation conversation = new Conversation(chatMessage, new Date(),
FriendsFragment.mCurrentUser.getUsername());
conversation.setStatus(Conversation.STATUS_SENDING);
mConversationArrayList.add(conversation);
mChatAdapter.notifyDataSetChanged();
mChatEditText.setText(null);
ParseObject parseObject = new ParseObject(ParseConstants.CLASS_CHAT);
parseObject.put(ParseConstants.KEY_SENDER, FriendsFragment.mCurrentUser.getUsername());
parseObject.put(ParseConstants.KEY_RECEIVER, mBuddy);
parseObject.put(ParseConstants.KEY_MESSAGE_BODY, chatMessage);
parseObject.saveEventually(new SaveCallback() {
@Override
public void done(ParseException e) {
if(e == null) {
conversation.setStatus(Conversation.STATUS_SENT);
Toast.makeText(ChatActivity.this, com.oikostudio.famchat.R.string.success_message, Toast.LENGTH_LONG).show();
sendPushNotification();
} else {
conversation.setStatus(Conversation.STATUS_FAILED);
}
mChatAdapter.notifyDataSetChanged();
}
});
}
public void sendPushNotification() {
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();
}
protected void loadConversationList() {
ParseQuery<ParseObject> query = ParseQuery.getQuery(ParseConstants.CLASS_CHAT);
if (mConversationArrayList.size() == 0) {
// load all Messages
ArrayList<String> messageAll = new ArrayList<String>();
messageAll.add(mBuddy);
messageAll.add(FriendsFragment.mCurrentUser.getUsername());
query.whereContainedIn(ParseConstants.KEY_SENDER, messageAll);
query.whereContainedIn(ParseConstants.KEY_RECEIVER, messageAll);
} else {
// load only newly received msg
if(mLastMessageDate != null)
query.whereGreaterThan(ParseConstants.KEY_CREATED_AT, mLastMessageDate);
query.whereEqualTo(ParseConstants.KEY_SENDER, mBuddy);
query.whereEqualTo(ParseConstants.KEY_RECEIVER, FriendsFragment.mCurrentUser.getUsername());
}
query.orderByDescending(ParseConstants.KEY_CREATED_AT);
query.setLimit(30);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> chatMessageBody, ParseException e) {
if(chatMessageBody != null && chatMessageBody.size() > 0) {
for (int i = chatMessageBody.size() - 1; i >= 0; i--) {
ParseObject messageBody = chatMessageBody.get(i);
Conversation conversation = new Conversation(
messageBody.getString(ParseConstants.KEY_MESSAGE_BODY),
messageBody.getCreatedAt(), messageBody.getString(ParseConstants.KEY_SENDER));
mConversationArrayList.add(conversation);
if(mLastMessageDate == null || mLastMessageDate.before(conversation.getDate()))
mLastMessageDate = conversation.getDate();
mChatAdapter.notifyDataSetChanged();
}
}
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if(isRunning) {
loadConversationList();
}
}
}, 1000);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_chat, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if (item.getItemId() == android.R.id.home)
{
finish();
}
return super.onOptionsItemSelected(item);
}
}
In this function public void sendPushNotification() { ParseQuery<ParseInstallation> query = ParseInstallation.getQuery(); // query.
// send push notification
ParsePush push = new ParsePush();
push.setQuery(query);
push.setMessage(getString(R.string.push_message, ParseUser.getCurrentUser().getUsername()));
push.sendInBackground();
}
what to add more after query. try everything that I can do, but I lost
please hlep