如何获取未读消息的数量PubNub

问题描述:

嘿,我正在使用 pubnub 服务为我的Titanium App添加聊天功能,但我想知道是否有办法获取未读消息的数量。
在api引用上没有关于此的信息

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

跟踪PubNub上的已读/未读消息



多年前我们承诺,我们会在您的应用中使用非常简单的方法来跟踪未读消息计数
现在终于可以了!
使用 PubNub功能
您可以添加永久状态对象和值到您的多设备应用程序中。
您将使用PubNub Functions Key / Value Storage引擎中提供的原子方法。
PubNub函数存储引擎( kvstore )被复制到每个数据中心,使其快速可靠地存储/检索数据。

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.

您只需要创建一个函数 increment 减少检索当前未读邮件的数量。

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

我们的函数将计算发送到通道的消息并使用原子递增和递减方法使用以通道命名的键保存值和用户ID。
这是一种称为传统命名空间的实用设计模式。
这意味着您将使用消息中的提示和信息来创建基于消息中的信息可构造的名称。
我们将在此示例中使用通道名称作为常规名称间距。

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.


频道: 房间。*

活动: 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();
        });
    });
}

现在发布到此渠道层级的任何JSON消息 room 。* 将通过递增计数器来跟踪未读消息。
可以通过包含读标志来递减计数器。
如果需要,此读取方法也可用作读取收据跟踪系统。
您可以通过 Adam Bavosa 读取实时聊天应用的收据模式

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.

将消息发布到 room.john-smith ,如下所示:

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

{ "message" : "Hello John!" }

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可以通过发布回复收据回复:

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.