【问题标题】:How to get the number of individual unread messages of PubNub?如何获取 PubNub 的单个未读消息数?
【发布时间】:2018-04-26 11:26:20
【问题描述】:

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

【问题讨论】:

标签: objective-c swift3 pubnub


【解决方案1】:

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

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

频道:房间。*

事件:在发布前

// 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,您可以将此新功能集成到现有应用中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多