How to get the number of unread messages PubNub
Asked Answered
A

1

7

Hey i'm using pubnub Services to add chat functionality to my Titanium App but i'm wondering if there is a way to get the number of unread messages.
there is no info about this on api references

What i tried to save the numbers of messages in history then reloading new history and calculate the difference but it's so stupid and complex solution any body know how to achieve this ? Thanks

Aniline answered 29/5, 2015 at 10:11 Comment(0)
L
8

Track Read/Unread Messages on PubNub

Years back we promised we'd make it super easy method for tracking Unread Message Counts in your app. Now it is finally possible! Using PubNub Functions, you can add permanent state objects and values into your multi-device applications. You'll use our atomic methods available in PubNub Functions Key/Value Storage engine. PubNub Functions Storage Engine ( kvstore ) is replicated to every data center, making it fast and reliable to store/retrieve data.

You'll only need to create one function to increment, decrement and retrieve the current count of your unread messages.

Count Unread Messages

Our Function will count messages sent to a channel and save the value using an atomic increment and decrement methods using a key named after the channel and user ID. This is a practical design pattern called "conventional namespacing". It means that you'll use hints and pieces of information found in the message to create a name that is constructible based on the information in the message. We will be using the channel name in this example for our conventional name-spacing.

This first function will increment a value to track the unread messages in a user's inbox.

Increment and Decrement the Unread Message Counter

Channel: room.*

Event: On-Before Publish

// Access to Distributed Database
const db = require('kvstore');
export default (request) => { 
    // Conventionally build the Key ID based on the request parameters and channel name.
    let counterId = request.channels[0] + '/' + request.params.uuid;
    
    // Increment or Decrement the unread message counter
    let method = request.message.read ? -1 : 1;
    
    // Increment/Decrement and read the unread message counter
    return db.incrCounter( counterId,  method ).then(()=>{
        return db.getCounter(counterId).then((counter) => {
            request.message.unread = counter || 0;
            return request.ok();
        });
    });
}

Now any JSON Message published to this channel hierarchy room.* will track unread messages by incrementing a counter. The counter can be decremented by including a read flag. This read method may also be used as a read-receipt tracking system if desired. You can learn more about read-receipts in the article by Adam Bavosa: Read Receipts Pattern for Realtime Chat Apps.

Using the Counter

Publish a message to room.john-smith like this:

{ "message" : "Hello John!" }

John will receive your message and a unread message counter is added to the message. The message will have been augmented with a unread variable. Now the message looks like this:

{ "message" : "Hello John!", "unread" : 1 }

John can respond with a read-receipt reply by publishing:

{ "read" : true }

The message will update the read receipt with a unread counter.

{ "read" : true, "unread" : 0 }

That's it! You did it. Patterns will change based on group and one-to-one messaging apps.

Leverett answered 29/5, 2015 at 18:19 Comment(14)
Thanks for your response but i didn't understand you correctly ,do you mean tracking the history time token and check if there is new messages and i think that will help be if there are new messages but i couldn't get there number ,and i didn't get your second solution sorry :(Aniline
The only way to get the number is by doing a Full-fetch. Also we are adding a future service which will provide this for you automatically. However it will be a few months before it is available.Leverett
How can I do the full-fetch in android? any resources?Tinctorial
Full-fetch Android Example: pubnub.com/docs/android-java/api-reference#history_example_4 - you can fetch all messages by using the Paging Example.Leverett
Hi you have a good question. We have built it and it will be available in Q2!Leverett
Just added this to the list! We are building it now.Leverett
whats the state here?Gargoyle
we have built a BLOCK for this!Leverett
@pubnub could you please paste a link to this BLOCK? I cannot find it :SVisualize
Goooooood request. We haven't posted it yet.Leverett
@PubNub NOW are you providing any proper way for read / unread message? or the mechanism above you mentioned is still. actually we are waiting for basic features of chat app from 2016 Feb., OUR major requirements are for App badge count and show unread message count in user chat list.Glacis
We are almost done. only things left are maintain App badge count and show message read / unread status. Waiting for your team update.....Glacis
What do you need from PubNub? We do not provide detailed features for chat in our SDKs because our SDKs are not use case specific. We many many many chat use case customers that have implemented these features. Have you engaged with a PubNub Solution Architect? It's a free service for customers. Please submit your request to PubNub SupportGodolphin
UPDATE! We've finally made this feature available to you. The latest answer update shows you one way how to achieve an unread message counter using PubNub. See our updated answer above.Leverett

© 2022 - 2024 — McMap. All rights reserved.