python - 如何正确观看 Gmail 推送通知

标签 python push-notification google-api gmail-api

我不明白如何正确监视 Gmail 推送通知(而且我对网络编码方面一无所知)。

Google给出了这个教程页面:https://developers.google.com/gmail/api/guides/push

到目前为止我做了什么:

  • 先决条件:已完成
  • 创建主题:完成
  • 创建订阅:完成
  • 授予您主题的发布权:完成
  • watch() 方法有效并给出以下结果:{'historyId': '714707', 'expiration': '1618824687477'}

到目前为止,一切正常,但我的知识仅限于此。在我看来,我必须在 watch 方法上实现某种无限 while 循环,以检查 historyId 更改。但如果是这样,为什么 watch() 方法上会有一个 stop() 方法,以及为什么会有一个 expiration 字段观察结果? 我应该从那里做什么?

这是我迄今为止的实现:

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()

最佳答案

我认为对推送通知的概念存在一些误解

  • 设置监视请求后,当邮箱更新时,您的应用程序将自动接收推送通知
  • 例如,当您收到新电子邮件时,会发生邮箱更新
  • 简单来说,historyId 反射(reflect)了您的邮箱在特定时刻(例如,当您设置监视请求时)的状态
  • 该值仅供引用,因为除非 specified otherwise您只会收到有关特定 historyId
  • 对应时刻之后发生的邮箱更新的通知
  • 您不需要无限循环来接收通知,您只需要 renew the watch request every 7 days .
  • 根据您的实现情况,您可能需要一些 HTTP 库来处理 POST 请求 - 每当 Gmail API 向您的服务器发布通知时。

关于python - 如何正确观看 Gmail 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67057438/

相关文章:

ios - 如何检测用户卸载的 iOS App?我需要推送严重通知。我不想失去它

PHP:如何在一个帖子中向多个设备发送 GCM 通知

android - 在 Android 中获取 Google 帐户域

python - Ubuntu中安装python google api

python - 创建 Json 输出 Boto3

python - 导入 matplotlib 不起作用

android - 为什么 firebase push_token 返回黑名单?

python - 有没有办法获取将文件下载到 Python 的原始链接?

python - 将 pandas 系列转换为对象数组,如 JavaScript 中所示

javascript - Google OAuth2 + Backbone + Require.js = "this"绑定(bind)问题