DETECT READ MESSAGES

1,180 views
Skip to first unread message

Evans Attafuah

unread,
Jun 6, 2015, 6:59:52 AM6/6/15
to fireba...@googlegroups.com
I want to be able to detect read messages on firebase.

My app currently has
pending
sent
read

also has 
deviceUser = user currently sending the message
contextUser = user to receive the message

when a message is sent by the deviceUser it's automatically set to pending. when "setValue" completion event is fired it's set to "sent" . But the last part is what am trying to wrap my head around. When the contextUser reads the message I need to set it to "read" on the server

Will be glad for multiple solutions to see which one works best for my scenario. 

any help will be deeply appreciated. 

Jay

unread,
Jun 6, 2015, 10:23:53 AM6/6/15
to fireba...@googlegroups.com
That should be pretty straight forward:

The contextUser in the app could be observing the node that contains messages and when the app starts (or whatever trigger is used) it will retrieve any new messages in that node

Then, each new message that's available will be read in (a snapshot).

Once the app has the snapshot, present the message to the user and set that node's child node (the flag that is set to pending, sent or read) from 'pending' to 'read'

MacOS:

[messagesRef observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {

   
//snapshot will contain each new message node, one at a time

   
// set the child node flag of the snapshot node from 'pending' to 'read'
   
}];


The above code will continually be observing the messages node so as new messages come in, they can be presented to the user and marked as read.

Jay

unread,
Jun 6, 2015, 10:31:14 AM6/6/15
to fireba...@googlegroups.com
Oh - and you may want to consider changing up the structure a bit if you want better message tracking.

A message that has been sent and not read is pending. So you could slightly reduce the complexity by removing the 'pending' status and just go with sent and read child nodes

Messages
   message_id_0
       message_content
: the message
       sent
: time & date sent
       read
: time & date read

 

Evans Attafuah

unread,
Jun 6, 2015, 5:39:15 PM6/6/15
to fireba...@googlegroups.com
Hello Jay,

Thanks for this. Solution works perfect. Having trouble with setting the deviceUser values to read after a few messages have been sent but the contextUser was inactive at that time. But am sure with this knowledge I will get the solution.

Also does it make sense to set pending for messages that are local and not delivered yet due to latency issues?
Screen Shot 2015-06-06 at 9.37.02 PM.png

Jay

unread,
Jun 7, 2015, 9:47:57 AM6/7/15
to fireba...@googlegroups.com
Not sure if a message needs a pending status at all. If your client is observing the messages node for newly added messages, regardless of latency, it will be grabbing any those messages when they are available. Only after they are read in will the read node be updated.

Setting the deviceUser values could be done within the main block that's responding to the message added events.

MacOS

[messagesRef observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {


 
NSString *currentTimeDate = [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle];
 
 
Firebase *messageRef = snapshot.ref;
 
Firebase *readRef = [messageRef childByAppendingPath@"read"];
 
[readRef setValue:currentTimeDate];
   
}];

note that the setValue should be done in a block but for brevity it's shown as a single-liner.

GmailMe

unread,
Jun 7, 2015, 11:15:03 AM6/7/15
to fireba...@googlegroups.com
Hello Jay,

Thanks for this. 

Sent from my iPhone 6
--
You received this message because you are subscribed to a topic in the Google Groups "Firebase Google Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/firebase-talk/1UfRRmSv_W4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/1707269d-78c9-4bd1-8193-45f1b1345d9f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Evans Attafuah

unread,
Jun 7, 2015, 12:12:09 PM6/7/15
to fireba...@googlegroups.com
Hello Jay,

I think am doing something wrong. After a while messages are set to read regardless. below are my user stories, maybe am missing something. 

as a device user I want to be able to know the delivery status of my messages I send to context user
**inactive - user might be online but not currently in a two-way chat

1. device user sends a message to context user, context user is inactive so delivery status is set to sent.

2. context user comes back and loads all messages and sets all messages with sent to read

3. if device user is active, anytime device user sends a message to context user after message completion event, a value change listener is attached for that single message reference if context user is in-active

4. if context user and device user are active messages are marked as read via child change events

5. if device user becomes active, it loads all messages and create value change events for children with delivery status as sent 


I noticed child change fires so often so I do a few checks before running my method  ::java code below.

public void onChildChanged(DataSnapshot dataSnapshot, String s) {

    if (isLoaded) {

        if (dataSnapshot.exists()) {

            if (dataSnapshot.getValue() instanceof HashMap) {

                Message message = new Message(dataSnapshot.getValue(HashMap.class));

                if (message.getUserId().equals(mContextUserId)) {

                    if (mContextUserStatus.getActive() && message.getDeliveryStatus() == DeliveryStatus.SENT) {

                        //::debugging
                        Alerts.notifications(ChatActivity.this, ChatActivity.class"contextUser""is active""");
                       
                        //set each unread message as read
                        setMessageAsRead(dataSnapshot);

                    }
                }
            }
        }
    }
}




Reply all
Reply to author
Forward
0 new messages