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

时间:2018-04-26 11:26:21

标签: objective-c swift3 pubnub

我正在使用pubnub服务向我的应用添加聊天功能,但我想知道是否有办法获取未读消息的个别数量。我正在使用此链接 - > https://www.pubnub.com/docs/swift/data-streams-publish-and-subscribe

1 个答案:

答案 0 :(得分:2)

这可以使用 PubNub Functions 来完成。函数是您自己的脚本,当在一个或多个PubNub频道上发布消息时,它们会自动在云中运行。

它有一个具有incrementdecrementretrieve功能的键值存储。这可以非常直观地使用PubNub上的未读消息模式。

频道:会议室。*

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

关注此official guide ,您可以将此新功能集成到现有应用中。