Android - Mark Gmail as read when archiving from notification

Did you say Google Script?

Marking Gmail read with Apps Script

The idea behind this script is to mark as read any message that is not in the Inbox (i.e., has been archived).

  1. Head to script.google.com to start a script.
  2. Choose to create a script for Gmail in the little popup.
  3. Delete all the sample code it gives you.
  4. Replace it with this (written using the API reference):

    function markArchivedAsRead() {
       var threads = GmailApp.search('label:unread -label:inbox');
       GmailApp.markThreadsRead(threads);
     };
  5. Save the project with File > Save.

  6. Add a new version using File > Manage Versions and enter "initial version" then submit that.
  7. Do a test run using Run > markArchivedAsRead and be sure and authorize the app when it asks you to.
  8. Add a new trigger using Resource > Current Project's Triggers and choose to run the above function every minute.
  9. Save the script again and exit.

I don't know that it's necessary to run it every minute, but as long as you run it regularly.

This isn't a direct answer to your issue, and if you have reasons to have unread messages that aren't in your inbox this won't work for you.


I managed to make it work with the following Google Script.
It's almos the same as @AlEverett's answer, but it never marks as read messages that skipped the inbox entirely (from a filter or something).

Unfortunately, it won't work for you if you tend archive messages very quickly (less then 30 seconds on average).

/** Mark as read archived threads which were previously in the inbox (determined by the label "i"). **/
function cleanAndroidArchived(){
  markArchivedAsRead();
  labelInboxAsI();
}

function markArchivedAsRead() {
  var threads = GmailApp.search('in:unread label:i -label:inbox');
  var label = GmailApp.createLabel("i");
  label.removeFromThreads(threads);
  GmailApp.markThreadsRead(threads);
};

function labelInboxAsI() {
  var threads = GmailApp.search('in:unread label:inbox');
  var label = GmailApp.createLabel("i");
  label.addToThreads(threads);
};

I believe I resolved the issue with @BruceConnor's case, needing to wait for the script to execute before it can work.

  1. I created a new filter that looks for anything in the inbox
  2. assigns the "i" label (which is created by his version)

Gmail complains that the filter will never match anything, but in this case it does match all incoming messages that stay in the inbox.

The result is that all new incoming messages are automatically "memorized" by this tag, then when you hit archive the script can compare the inbox list with the "i" label and know which ones were just archived, and then mark only those as "read". You don't have to wait to archive, because all messages are assigned into the archive "i" queue.

Bonus: I also set the new "i" label to "Hide in message list" and "Hide in label list", so it never shows up at all. (Click the little arrow next to the new "i" label and choose these options.)