PunNub - 关闭设备时恢复丢失消息的最佳方法?

时间:2016-02-09 14:26:03

标签: android ios push-notification pubnub

我们有多个移动客户端(ios和android)订阅了channel-msg。

服务器正在向channel-msg发布7条消息:
enter image description here

  1. 讯息1-3已发布到频道。
  2. 设备通过原生PubNub获得消息1-3。
  3. 用户关闭了设备。
  4. 消息4-5已发布到频道。
  5. 用户打开了设备,应用程序处于前台。
  6. 消息6已发布。
  7. 消息6到达设备。
  8. 此时(在"获得了PubNub消息"设备代码中的回调),我不想执行消息6,因为我必须执行消息首先是4-5

    我可以使用history()API来获取丢失的消息,但是通过这种策略,我被迫执行"历史检查"在我可以执行之前,我在设备上收到的每条消息上。

    我有更好的模式/设计来处理这种情况吗?

1 个答案:

答案 0 :(得分:1)

PubNub历史记录

您不需要为每条消息调用历史记录,而是检索所有错过的消息(每history次呼叫最多100条消息)。

只需保存上次收到的消息的timetoken,您可以在重新启动应用程序时检索它(NSUserDefaults)。启动您的应用后,只需在end来电中使用timetoken作为start参数(history param为零)。

如果您收到100封邮件(最多),那么您可能会有更多邮件,而您需要call history again (paging through storage) and again until you receive > 100 messages

收到最后一封邮件后,use the timetoken of that last message (the one closest to now), use that timetoken to subscribe会在最后history次来电与subscribe之间收到任何消息,并继续订阅其他消息。