gmail-api标记为“已读”

时间:2015-10-22 10:15:02

标签: android gmail-api

使用gmail-api我试图将消息视为“读取”,如下所示,但它不起作用。

 ModifyMessageRequest mods = new ModifyMessageRequest()
            .setRemoveLabelIds(new ArrayList<String>(Arrays.asList("UNREAD")));
    com.google.api.services.gmail.model.Message message = null;
    try {
        message = mService.users().messages().modify(acct.sEmail, gmsailID, mods).execute();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }

2 个答案:

答案 0 :(得分:1)

setRemoveLabelIds()的参数不是Label String,而是Label ID。 所以从下面的代码中获取标签ID并在函数中使用它

    List<String> labels = new ArrayList<String>();
    ListLabelsResponse listResponse = service.users().labels().list(userId).execute();
    for (Label label : listResponse.getLabels()) {
        Label countLabel = service.users().labels().get(userId, label.getId()).execute();
        if (countLabel.getName().equalsIgnoreCase("UNREAD"))
            unreadId = countLabel.getId();
    }
    List<String> add = new ArrayList<String>();
    List<String> remove = new ArrayList<String>();
    remove.add(unreadId);
    modifyMessage(service, userId, message.getId(), add, remove);


public static void modifyMessage(Gmail service, String userId, String messageId,
                                 List<String> labelsToAdd, List<String> labelsToRemove) throws IOException {
    ModifyMessageRequest mods = new ModifyMessageRequest().setAddLabelIds(labelsToAdd)
            .setRemoveLabelIds(labelsToRemove);
    Message message = service.users().messages().modify(userId, messageId, mods).execute();
}

答案 1 :(得分:0)

这是我使用的一个(在使用ListMessagesResponse获取messageId之后):

public void modifyMessage(String userId, String messageId) throws IOException {
        List<String> lblIDRemove=new ArrayList<String>();
        lblIDRemove.add("UNREAD");
        ModifyMessageRequest mods = new ModifyMessageRequest()
                .setAddLabelIds(null)
                .setRemoveLabelIds(lblIDRemove);
        Message message = mService.users().messages().modify(userId, messageId, mods).execute();
    }