Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Chat extends AppCompatActivity
- {
- LinearLayout layout;
- RelativeLayout layout_2;
- ImageView sendButton;
- EditText messageArea;
- ScrollView scrollView;
- Firebase reference1, reference2;
- boolean timerProcessing = false;
- boolean timerStarts = false;
- String date = "";
- String message;
- String userName;
- int value = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_chat);
- layout = (LinearLayout) findViewById(R.id.layout1);
- layout_2 = (RelativeLayout)findViewById(R.id.layout2);
- sendButton = (ImageView)findViewById(R.id.sendButton);
- messageArea = (EditText)findViewById(R.id.messageArea);
- scrollView = (ScrollView)findViewById(R.id.scrollView);
- Firebase.setAndroidContext(this);
- reference1 = new Firebase("https://zipa1x.firebaseio.com/messages/" + UserDetails.username + "_" + UserDetails.chatWith);
- reference2 = new Firebase("https://zipa1x.firebaseio.com/messages/" + UserDetails.chatWith + "_" + UserDetails.username);
- sendButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String messageText = messageArea.getText().toString();
- if(!messageText.equals(""))
- {
- Map<String, String> map = new HashMap<String, String>();
- map.put("message", messageText);
- map.put("user", UserDetails.username);
- reference1.push().setValue(map);
- reference2.push().setValue(map);
- messageArea.setText("");
- }
- }
- });
- reference1.addChildEventListener(new ChildEventListener()
- {
- @Override
- public void onChildAdded(DataSnapshot dataSnapshot, String s)
- {
- Map map = (Map) dataSnapshot.getValue();
- message = map.get("message").toString();
- userName = map.get("user").toString();
- String lastMessage = String.valueOf(map.values().toArray()[map.size() - 2]);
- Log.e("LAST MESSAGE", lastMessage);
- if (userName.equals(UserDetails.username))
- {
- addMessageBox(message, 1);
- }
- else
- {
- addMessageBox(message, 2);
- if(message == lastMessage)
- {
- sendNotification(lastMessage);
- }
- }
- }
- @Override
- public void onChildChanged(final DataSnapshot dataSnapshot, String s)
- {
- }
- @Override
- public void onChildRemoved(DataSnapshot dataSnapshot) {
- }
- @Override
- public void onChildMoved(DataSnapshot dataSnapshot, String s) {
- }
- @Override
- public void onCancelled(FirebaseError firebaseError) {
- }
- });
- scrollView.post(new Runnable() {
- @Override
- public void run() {
- scrollView.fullScroll(View.FOCUS_DOWN);
- }
- });
- }
- TextView textView,textView2;
- public void addMessageBox(String message, int type)
- {
- DateFormat df = new SimpleDateFormat("h:mm a");
- date = df.format(Calendar.getInstance().getTime());
- TextView textView1 = new TextView(Chat.this);
- textView1.setText(date);
- textView1.setTextSize(10);
- textView = new TextView(Chat.this);
- textView.setText(message);
- LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
- lp2.weight = 1.0f;
- if(type == 1) {
- lp2.gravity = Gravity.RIGHT;
- textView.setBackgroundResource(R.drawable.left_bubble);
- }
- else{
- lp2.gravity = Gravity.LEFT;
- textView.setBackgroundResource(R.drawable.right_bubble);
- }
- textView.setLayoutParams(lp2);
- layout.addView(textView);
- textView1.setLayoutParams(lp2);
- layout.addView(textView1);
- scrollView.post(new Runnable() {
- @Override
- public void run() {
- scrollView.fullScroll(View.FOCUS_DOWN);
- }
- });
- }
- private void sendNotification(String message)
- {
- Intent intent = new Intent(this,Chat.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
- Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
- NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
- NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
- .setVisibility(Notification.VISIBILITY_PUBLIC)
- .setSmallIcon(R.mipmap.ic_launcher)
- .setContentTitle(userName)
- .setContentText(message)
- .setSound(notificationSound)
- .setContentIntent(pendingIntent);
- NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- mNotificationManager.notify(0, builder.build());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement