简体   繁体   English

如何正确观看 gmail 推送通知

[英]How to properly watch for gmail push notifications

I don't understand how to properly watch for gmail push notifications (also i know nothing about the web in terms of coding).我不明白如何正确观看 gmail 推送通知(而且我对 web 的编码一无所知)。

Google give this tutorial page: https://developers.google.com/gmail/api/guides/push谷歌给这个教程页面: https://developers.google.com/gmail/api/guides/push

What i did so far:到目前为止我做了什么:

  • Prerequisites: DONE先决条件:完成
  • Create a topic: DONE创建主题:完成
  • Create a subscription: DONE创建订阅:完成
  • Grant publish rights on your topic: DONE授予您主题的发布权:完成
  • watch() method works and gives this result: {'historyId': '714707', 'expiration': '1618824687477'} watch() 方法有效并给出以下结果: {'historyId': '714707', 'expiration': '1618824687477'}

So far everything works fine, but my knowledge stops here.到目前为止一切正常,但我的知识到此为止。 It seems to me that i have to implement some kind of infinite while loop on the watch method, to check for historyId changes.在我看来,我必须在 watch 方法上实现某种无限 while 循环,以检查historyId的变化。 But if so, why would there be a stop() method on the watch() method, and also why would there be a expiration field on the watch result?但如果是这样,为什么watch()方法上会有一个stop()方法,为什么 watch 结果上会有一个expiration字段? What should i do from there?我应该从那里做什么?

Here is my implementation so far:到目前为止,这是我的实现:

from __future__ import print_function
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials


SCOPES = ['https://mail.google.com/']
MY_TOPIC_NAME = 'my/toppic/name'

creds = None
if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
                creds.refresh(Request())
else:
        flow = InstalledAppFlow.from_client_secrets_file('./my_creds.json', SCOPES)
        creds = flow.run_local_server(port=0)
with open('token.json', 'w') as token:
        token.write(creds.to_json())



gmail = build('gmail', 'v1', credentials=creds)
request = {'labelIds': ['INBOX'],'topicName': MY_TOPIC_NAME}
gmail.users().watch(userId='me', body=request).execute()

I think there is some misunderstanding about the concept of push notifications我认为对推送通知的概念存在一些误解

  • Once you set-up a watch request, push notifications will be automatically received by your application when there is a mailbox update设置监视请求后,当有邮箱更新时,您的应用程序将自动接收推送通知
  • A mailbox update takes place for example when you receive a new email例如,当您收到新的 email 时,会发生邮箱更新
  • The historyId in simple words reflects the status of your mailbox at a certain moment (eg when you set-up a watch request) historyId简单的说就是反映了你邮箱在某个时刻的状态(比如你设置了watch请求的时候)
  • This value is only important for reference, because unless specified otherwise you will only get notifications about mailbox updates taking place after the moment corresponding to the specific historyId此值仅供参考,因为除非另有说明,否则您只会收到有关特定historyId对应时刻之后发生的邮箱更新的通知
  • You do not need an endless loop for receiving notifications, you only need to renew the watch request every 7 days .您不需要无限循环接收通知,您只需要每 7 天更新一次手表请求
  • Depending on your implementation you will probably need some HTTP library that handles POST requests - whenever the Gmail API posts notifications to your server.根据您的实施,您可能需要一些 HTTP 库来处理 POST 请求 - 每当 Gmail API 将通知发布到您的服务器时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM