Incoming mails notification on android
I am looking for a way to programmatically intercept incoming emails on Android, no matter their source (gmail, exchange, IMAP, etc.). My main concern is to have a look at the mail headers rather than the mail body. It can be done for GMail only using the following code -
ContentResolver cr = getContentResolver();
Cursor unread = cr.query(Uri.parse("content://gmail-ls/conversations/xxxxxx@gmail.com"), null, "label:^u", null, null);
unread.moveToFirst();
int subjectIdx = unread.getColumnIndex("subject");
do {
String subject = unread.getString(subjectIdx);
} while (unread.moveToNext());
... there is also this answer. Is something like this available to non-gmail accounts? I know that theoretically I can ask the user for the use开发者_JS百科rname and password and connect via imap, but I can't do it from the business aspect.
Any solution that is possible will be welcome, i.e. Java SDK, NDK, undocumented API or whatever means necessary.
Answering my own question:
Google has released push notifications for the GMail Rest API. I have not used it, but the documentation is here
Old answer
There are two options to do this:
- Use the GMail Inbox Feed - it's an atom feed whom you can get access to using OAuth. It shows only unread items in the inbox, so it is very limited.
- Use the GMail IMAP extensions, and connect using IMAP and OAuth. Here you have access to the entire user mailbox.
Both of this methods has the limit of being pull based rather than push based. I suspect (even though I haven't tried it) that the IMAP push wouldn't work, and in any case I wouldn't trust it on mobile device.
精彩评论