Reputation: 347
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
Upvotes: 4
Views: 217
Reputation: 598728
This essentially boils down to knowing whether a user has already seen a specific message. Or even more correctly: whether there are any messages if the user hasn't seen yet, as you'll probably only want to play one sound, even if there are multiple new messages.
Also see this answer I gave in an #AskFirebase video, and this previous answer here: Android Firebase New Message Query.
Upvotes: 1