获取新消息的最有效方法 [英] Most efficient way to get new messages

查看:111
本文介绍了获取新消息的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a process that looks for new messages in a users Gmail. The message is added to an external database if it meets certain address criteria.

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

We have been using Users.History.List which returns all messages that have had a change made to them. This is quite inefficient as we have to subsequently check each message to see if we have already processed it.

我们正在交替使用Users.Messages.List并检查MsgId以查看它是否大于上一个检查(我们从中存储ID).这里的假设是MsgId将保持更大.这种方法有缺陷吗?别人在做什么?

We are looking at alternatively using Users.Messages.List and checking the MsgId to see if it is larger than the previous check (we store the Id from that). The assumption here is that the MsgId will keep getting larger. Is this approach flawed? What are others doing?

非常感谢.

推荐答案

下面是一个示例(仅使用Java客户端api)如何仅获取新消息

Here is an example how to get only new message (using Java client 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中也有类似的实现.

Probably there is a similar implementation in other languages/REST API.

您可以使用其他历史事件,例如:消息已删除,标签已删除和标签已移除 参考: https://developers.google.com/gmail/api/v1 /reference/users/history/list

You can use other history events such as:messagesDeleted, labelsAdded and labelsRemoved Reference: https://developers.google.com/gmail/api/v1/reference/users/history/list

这篇关于获取新消息的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆