【问题标题】:Most efficient way to get new messages获取新消息的最有效方式
【发布时间】:2015-04-07 18:50:28
【问题描述】:

我有一个在用户 Gmail 中查找新邮件的过程。如果邮件符合特定的地址条件,则会将邮件添加到外部数据库中。

我们一直在使用 Users.History.List 来返回所有对其进行了更改的消息。这是非常低效的,因为我们必须随后检查每条消息以查看我们是否已经处理了它。

我们正在考虑交替使用 Users.Messages.List 并检查 MsgId 以查看它是否大于先前的检查(我们从中存储 Id)。这里的假设是 MsgId 会不断变大。这种方法有缺陷吗?其他人在做什么?

非常感谢。

【问题讨论】:

  • 我面临同样的情况,它有效,但正如你所说,它效率低下。你有没有找到更好的方法来解决这个问题?
  • 不 - 我们只是定时轮询。效率更高。

标签: gmail-api


【解决方案1】:

这是一个如何只获取新消息的示例(使用 Java 客户端 api)

List<History> histories = new ArrayList<History>();
ListHistoryResponse response = service.users().history().list(userUID).setStartHistoryId(startHistoryId).execute();

//get all history events and not only the first page
while (response.getHistory() != null) {
    histories.addAll(response.getHistory());
    if (response.getNextPageToken() != null) {
        String pageToken = response.getNextPageToken();
        response = service.users().history().list(userUID)
                .setPageToken(pageToken)
                .setStartHistoryId(startHistoryId).execute();
    } else {
        break;
    }
}

//for each history element find the added messages
for (History history : histories) {
    List<HistoryMessageAdded> addedMessages = history.getMessagesAdded();

    if (addedMessages == null){
        continue;
    }

    //call to message.get for each HistoryMessageAdded
    for (HistoryMessageAdded addedMsg : addedMessages) {
        Message message = addedMsg.getMessage();
            Message rawMessage = service.users().messages().get(userUID, message.getId()).setFormat("raw").execute();

    }
}

在其他语言/REST API 中可能有类似的实现。

您可以使用其他历史事件,例如:messagesDeleted、labelsAdded 和 labelsRemoved 参考: https://developers.google.com/gmail/api/v1/reference/users/history/list

【讨论】:

    【解决方案2】:

    消息 ID 是唯一的,其值永远不会改变。要获取新消息,您可以使用history.list(),并在您之前为该消息设置的最大historyId 中提供historyId

    这是示例响应:

    {
     "history": [
      {
       "id": "1825077",
       "messages": [
        {
         "id": "14b4c0dbc6ba9a57",
         "threadId": "14b4b4ae8cfbea5c"
        }
       ]
      },
      {
       "id": "1825087",
       "messages": [
        {
         "id": "14b4c0dc3ab5e49b",
         "threadId": "14b4b4ae8cfbea5c"
        }
       ]
      },
      {
       "id": "1825097",
       "messages": [
        {
         "id": "14b4c0e07e0f6545",
         "threadId": "14b4b4ae8cfbea5c"
        }
       ]
      }
     ]
    }
    

    1825097 是消息“14b4c0e07e0f6545”的最大historyIdMsgid 也没有在这里改变,只是历史 id 被改变了。

    如果您将 1825097 作为历史 ID 并且消息中没有更改,则响应将是 200 和标头。如果您收到响应 404 错误,则需要改用 messages.list() 来执行完全同步。

    【讨论】:

    • 我们一直在使用历史 ID,但这会返回对线程/消息的所有更改。我们只想要新消息,并且希望避免根据我们已经同步的消息检查所有历史记录。如果可能,我们只需要一个新消息列表。
    • 你可以使用messages.list(),将标签名称和q值设为“is:unread”来获取所有未读消息。
    • 是的,我们是messages.list() 并获取所有id 大于上次同步的messageId 的消息。这似乎是最好的方法。未读会忽略我认为的任何已发送消息/已打开消息?
    • @PNC 是否有任何官方文档表明 messageIds 始终按递增顺序排列。 ?
    • @vinitpayal - 没有官方文档,但我们多年来一直使用这种方法没有问题。
    猜你喜欢
    • 1970-01-01
    • 2012-02-22
    • 2012-07-13
    • 2012-08-21
    • 2012-08-08
    • 2016-04-10
    • 2011-09-09
    • 2017-05-13
    • 1970-01-01
    相关资源
    最近更新 更多