I used firebase ChildEventListner for chatting application. In this app i pass condition in onChildAdded, that if the new child added by message senderId than play sound in the Activity. Below is my code.
rootRef.child("Messages").child(MessageSenderId).child(MessageReceiverId).addChildEventListener(new ChildEventListener()
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s)
{
Messages messages=dataSnapshot.getValue(Messages.class);
messageslist.add(messages);
messagesAdapter.notifyDataSetChanged();
messageList.smoothScrollToPosition(messageList.getAdapter().getItemCount());
if (messages.from.equals(MessageSenderId))
{
MediaPlayer IncomingMessageSound=MediaPlayer.create(MessageActivity.this,R.raw.incoming_message_sound);
IncomingMessageSound.start();
}
}
But this is work like, when i go to the message activity sound plays total no. of child added by senderId times. It means, if message received 15 times than sound plays 15 time on start of activity. And after no sound will played.
So the my problem is how to pass proper condition when new node added by senderId than play the sound means play sound(not notificatin sound) on receiving message