How to receive push notifications from my Gmail ac

2019-04-05 11:56发布

问题:

I'd like to write an application which monitors a mailbox (ideally a Gmail mailbox). In brief, my question is how do I listen to incoming emails? I could always keep pulling emails but I am afraid that this doesn't scale very well.

How does iOS do it with Gmail? I think they are using Exchange protocol, but I am not sure. Does anybody have an idea on how to do it? Is there a solution which already does something similar? I just need a hook.

Thanks!

回答1:

The most universal "API" for monitoring a Gmail or Google Apps inbox is the IMAP protocol. There's some information about this here: Gmail IMAP OAuth for desktop clients

For Google Apps inboxes (not general consumer Gmail accounts), you can also use the Gmail Inbox Feed, which provides an AtomPub XML feed of unread messages in a Google Apps inbox: http://code.google.com/apis/gmail/docs/inbox_feed.html



回答2:

The "API" to GMail is IMAP.

In IMAP the IDLE feature is used to tell the server to notify the client when there are changes to the mailbox.

IDLE is defined in RFC 2177 IDLE is an optional IMAP feature so this may or may not work on other IMAP servers.



回答3:

If you just want to talk to Gmail, they have a REST API you can use and it supports push notifications (e.g. webhooks): https://developers.google.com/gmail/api/guides/push

But again this is a Gmail-specific API, which may fit you requirements or not. If you are fine just being specific to Gmail it is likely easier and better to work with from a web/mobile type environment than something like IMAP.