Jerbs
Jerbs

Reputation: 303

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

Google give this tutorial page: https://developers.google.com/gmail/api/guides/push

What i did so far:

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

Upvotes: 2

Views: 2091

Answers (2)

Ankitesh Kumar
Ankitesh Kumar

Reputation: 1

your issue seems to be that you're not getting any callbacks, if that's the case then you can try below steps

  1. Create a topic. Grant publisher role to "[email protected]". pic of permission section
  2. Create a subscription for this topic
  3. change Delivery type to Push in your subscription's settings delivery type setting
  4. Endpoint Url is correct and accessible for internet. Your url should look like this https://mydomain/gmail-service/api/path
  5. send watch request with correct topic name

Upvotes: 0

ziganotschka
ziganotschka

Reputation: 26836

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
  • The historyId in simple words reflects the status of your mailbox at a certain moment (e.g. when you set-up a watch request)
  • 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
  • You do not need an endless loop for receiving notifications, you only need to renew the watch request every 7 days.
  • Depending on your implementation you will probably need some HTTP library that handles POST requests - whenever the Gmail API posts notifications to your server.

Upvotes: 2

Related Questions