With the recent update to the Gmail app you are now able to reply and delete a message directly from the expanded notification. Many of the messages I get are short and easily read from the notification itself, so I would like to be able to mark the message as read from the notification without having to go into the app. Hopefully the big G can add this functionality to a future iteration of the app, but until then, I've come up with my own solution using Tasker and SL4A scripts
I have Tasker create a notification whenever I get an email that simply says "Mark as read". When I click that notification it runs the following python script in SL4A. You must pass the variables %gm_user, %gm_pw, and %gm_subject to the script as your gmail username (
user...@gmail.com), password, and the subject of the message you are marking. The subject can be retrieved through a sqlite database search using Tasker shell commands. There's a thread in here on that process already. One thing to note is that the gmail imap service uses the same search rules as the regular gmail search, so certain characters have to be removed from the subject line. For example I use a regex search and replace to get rid of 's in the subjects.
For most of my SL4A scripts I have some code to set the Tasker variable "%SL4A_Response" to some kind of useful message for the purpose of troubleshooting.
Several bits of what follows are surely borrowed from other people's work, but I've forgotten who and what now. Sorry if I'm using your code without proper credit.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
import imaplib
import android
droid=android.Android()
import time
import datetime
class Task():
SET_VARIABLE = 547
def new_task(self):
self.action_cnt = 0
self.extras = {'version_number': '1.0', 'task_name': 'task' + str(time.time()), 'task_priority': 9 }
def set_var(self, varname, value):
self.action_cnt += 1
self.extras['action' + str(self.action_cnt)] = {'action': self.SET_VARIABLE, 'arg:1': varname, 'arg:2': value, 'arg:3': False, 'arg:4': False, 'arg:5': False}
def run_task(self):
taskIntent = droid.makeIntent('net.dinglisch.android.tasker.ACTION_TASK', None, None, self.extras).result
droid.sendBroadcastIntent(taskIntent)
def set_var_now(self, varname, value):
self.new_task()
self.set_var(varname, value)
self.run_task()
try:
gm_user = droid.getIntent().result[u'extras'][u'%gm_user']
gm_pw = droid.getIntent().result[u'extras'][u'%gm_pw']
gm_subject = droid.getIntent().result[u'extras'][u'%gm_subject']
mail = imaplib.IMAP4_SSL('
imap.gmail.com')
mail.login(gm_user, gm_pw)
mail.select("inbox")
date = (datetime.date.today() - datetime.timedelta(1)).strftime("%d-%b-%Y")
search_string = '(SENTSINCE {date} HEADER Subject "' + gm_subject + '")'
typ, data = mail.search(None, search_string.format(date=date))
if len(data[0])>0:
mail.store(data[0].replace(' ',','),'+FLAGS','\Seen')
droid.makeToast('Mail Marked as Read: ' + gm_subject)
t = Task()
t.set_var_now("%SL4A_Response", 'SL4A GMail: Message Marked as Read (' + gm_subject + ')')
else:
droid.makeToast('Mail Not Found')
t = Task()
t.set_var_now("%SL4A_Response", 'SL4A GMail: Mail Not Found (' + gm_subject + ')')
except Exception, e:
t = Task()
t.set_var_now("%SL4A_Response", 'SL4A GMail Message Error: ' + str(e))