Click here to Skip to main content
15,889,116 members
Home / Discussions / Android
   

Android

 
QuestionAndroid chat application message notification receive issue Pin
Pavlex426-May-17 20:46
Pavlex426-May-17 20:46 
I have created android chat application. When I send message it loops all messages through notification and then show the latest message instead of just showing newest message inside the notification!!!! How to fix this issue???

Java
public class Chat extends AppCompatActivity
{
    LinearLayout layout;
    RelativeLayout layout_2;
    ImageView sendButton;
    EditText messageArea;
    ScrollView scrollView;
    Firebase reference1, reference2;

    @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) {

                for (DataSnapshot child: dataSnapshot.getChildren())
                {
                    Map map = dataSnapshot.getValue(Map.class);
                    String message = map.get("message").toString();
                    String userName = map.get("user").toString();

                    if (userName.equals(UserDetails.username))
                    {
                        addMessageBox("You:-\n" + message, 1);

                        //sendNotification(message,userName);
                    } else
                    {
                        addMessageBox(UserDetails.chatWith + ":-\n" + message, 2);

                        sendNotification(message,userName);
                    }
                }
            }

            @Override
            public void onChildChanged(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);
            }
        });
    }

    public void addMessageBox(String message, int type){
        TextView 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.LEFT;
            textView.setBackgroundResource(R.drawable.bubble_in);
        }
        else{
            lp2.gravity = Gravity.RIGHT;
            textView.setBackgroundResource(R.drawable.bubble_out);
        }
        textView.setLayoutParams(lp2);
        layout.addView(textView);
        //scrollView.fullScroll(View.FOCUS_DOWN);

        scrollView.post(new Runnable() {
            @Override
            public void run() {
                scrollView.fullScroll(View.FOCUS_DOWN);
            }
        });
    }

    private void sendNotification(String message,String userName)
    {
        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)
                //.setOngoing(true)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(userName)
                .setContentText(message)
                .setSound(notificationSound)
                .setContentIntent(pendingIntent);

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNotificationManager.notify(0, builder.build());
    }
}

SuggestionRe: Android chat application message notification receive issue Pin
Richard MacCutchan26-May-17 21:57
mveRichard MacCutchan26-May-17 21:57 
QuestionNeural Network Algorithms Pin
Member 1320852321-May-17 20:30
Member 1320852321-May-17 20:30 
AnswerRe: Neural Network Algorithms Pin
David Crow22-May-17 3:28
David Crow22-May-17 3:28 
GeneralRe: Neural Network Algorithms Pin
Member 1320852323-May-17 2:32
Member 1320852323-May-17 2:32 
AnswerRe: Neural Network Algorithms Pin
Richard MacCutchan22-May-17 6:48
mveRichard MacCutchan22-May-17 6:48 
Questionhow can I make my android app location aware? Pin
Member 1320194920-May-17 8:38
Member 1320194920-May-17 8:38 
AnswerRe: how can I make my android app location aware? Pin
Afzaal Ahmad Zeeshan20-May-17 9:47
professionalAfzaal Ahmad Zeeshan20-May-17 9:47 
AnswerRe: how can I make my android app location aware? Pin
David Crow20-May-17 10:16
David Crow20-May-17 10:16 
QuestionAndroid connect to MS SQL Server Pin
Member 1190014018-May-17 18:31
Member 1190014018-May-17 18:31 
AnswerRe: Android connect to MS SQL Server Pin
Peter_in_278018-May-17 18:53
professionalPeter_in_278018-May-17 18:53 
GeneralRe: Android connect to MS SQL Server Pin
Member 1190014018-May-17 19:38
Member 1190014018-May-17 19:38 
GeneralRe: Android connect to MS SQL Server Pin
Peter_in_278018-May-17 20:56
professionalPeter_in_278018-May-17 20:56 
AnswerRe: Android connect to MS SQL Server Pin
Richard MacCutchan18-May-17 21:48
mveRichard MacCutchan18-May-17 21:48 
GeneralRe: Android connect to MS SQL Server Pin
Member 1190014018-May-17 21:56
Member 1190014018-May-17 21:56 
GeneralRe: Android connect to MS SQL Server Pin
Richard MacCutchan18-May-17 22:12
mveRichard MacCutchan18-May-17 22:12 
QuestionRe: Android connect to MS SQL Server Pin
David Crow19-May-17 3:11
David Crow19-May-17 3:11 
AnswerRe: Android connect to MS SQL Server Pin
Richard MacCutchan19-May-17 6:33
mveRichard MacCutchan19-May-17 6:33 
GeneralRe: Android connect to MS SQL Server Pin
David Crow19-May-17 6:35
David Crow19-May-17 6:35 
Question(Short Video Demo) Fragment back navigation issue and overlapping fragments using a frame layout Pin
Daniel Antony Ali17-May-17 18:41
Daniel Antony Ali17-May-17 18:41 
QuestionAnyone can help Pin
wseng16-May-17 21:58
wseng16-May-17 21:58 
AnswerRe: Anyone can help Pin
Richard MacCutchan16-May-17 22:46
mveRichard MacCutchan16-May-17 22:46 
QuestionAndroid media player won't start Pin
Pavlex413-May-17 2:08
Pavlex413-May-17 2:08 
AnswerRe: Android media player won't start Pin
Pavlex413-May-17 5:13
Pavlex413-May-17 5:13 
QuestionAndroid read file from url Pin
Pavlex412-May-17 6:39
Pavlex412-May-17 6:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.