When is new Gmail API History ID generated - gmail-api

I am trying to design an algorithm to maintain a list of new INBOX messages using a combination the watch()/push notification, with the user.history and user.messages gmail apis. HistoryID documentation is unclear.
https://developers.google.com/gmail/api/guides/sync#partial
indicates
"You can also use push notifications to trigger partial synchronization in real-time and only when necessary, thereby avoiding needless polling." -- My question is HOW?
Which event on the email account triggers an incremented HistoryID?
If I guess HistoryID I get in the push notification the latest historyId of the label specified, then I should be able to use that HistoryID as the starting HistoryId in the User.History.List call, but when I do I just get an json response which is another higher HistoryID. So, if I'm specifying messagesadded and watching INBOX, why is the call to History API with the most current HistoryID returning no HistoryList objects?
I have read Gmail API users.watch - no details for historyId and Gmail History list is not giving complete data
and I'm unclear as to how to design the algorithm to use push notifications in conjunction with the other apis to do a partial sync to maintain a running inventory on new INBOX messages, as per the documentation.

this issue really sad, google just send historyId with empty history list. But why cant add emailId or property that this history binds with new message arrived?
To solve this problem u need remember time when u last time fetch email messages with total counter messages in Inbox, next, when u get google pub/sub message and it has empty history object - u need get new total counter of messages in Inbox label - if there is differences - next what u need is fetch email messages using:
or
max count value = differences between of total counter messages,
means add query to get messages by max result
or
using date time from your last update - means add query to get messages by date time.
Really bad that google make such bad implementation for pub sub for new messages.

Related

How to modify firebase database so that i can set a read or unread status

I need to show the count of unread messages from firebase in the notification bell.
I am done with displaying all contents in my collection in my web application.But when a new message arrives i need to give a feedback to user by showing the unread message count in notification bell .From my understanding,for that i need to set a read/unread status in firebase collection.Is there any alternative way for showing the count of unread messages in my React Web Application?
i am using firebase package of npm for displaying messages.
Below is the sample data in my firebase collection.
name:"John Doe",
message:"you got a new appointment"
i am new to firebase.Please help me with some logic to sort out this problem.
If I understand correctly, you would want to use a status flag (read: boolean), unless you would not mind writing all new messages to a particular path and moving them to another path after it is read.
Something like below
/user/unread-msg => all new messages come here
/user/read-msg => messages are moved here after being read.
This I believe would be less efficient than using a simple flag.
You could use a firebase cloud function to maintain the count of child elements in a particular path like /user/unread-msg or /user/read-msg so that you don't have to download the entire data to get the count.
Refer the sample code at https://github.com/firebase/functions-samples/tree/master/child-count

Why does gmail API when using history.list method send message ids without the field what action has been preformed on them?

When using gmail API, history.list method we get "bare" message ids with no additional field from the fields in 'labelAdded', 'labelRemoved', 'messageAdded', 'messageRemoved'. Why is that? And is it possible that a new message has been added but when we use this method, the field messageAdded hasn't been used, so we receive it "bare" ?
According to the GMail history API, the list only contains the id and threadId fields.
It works this way because the objective of the history API is to provide you with the changes that happened in the mailbox, not its contents.
After you obtain the list from Users.history: list you need to call Users.messages: get or Users.messages: list to get the full messages.
If the messageAdded field is empty, it means that no new messages were added after the last historyId you examined. You might have skipped some. Make sure that every time you query the API you internally store the last historyId you processed so you can resume from that point in the future and not lose any changes.

Is there any way to limit a Gmail watch to only messages added?

When setting up a watch for a user is there a way to limit the watch to only messages added to the inbox?
Based on the documentation (https://developers.google.com/gmail/api/v1/reference/users/watch) I see that there is the option for INBOX labelId, but I want to limit it to only messages added as well. We're currently having to handle this by passing 'history/messagesAdded' in the fields string in the subsequent history.list call.
Unfortunately you cannot. what you have to do is
Get the history when notification arrived. History returns a json and it contains a 'messagesAdded' if new message is added.
You can keep a predefined array of labels like below
predefinedLabels = ['UNREAD', 'CATEGORY_PERSONAL', 'INBOX']
Now you can check, (each is the history json)
if 'messagesAdded' in each:
labels = each["messagesAdded"][0]["message"]["labelIds"]
intersectionOfTwoArrays = list(set(predefinedLabels) & set(labels))
Here you get the intersection of labels. Now you have to check that with predefined labels
if set(predefinedLabels) == set(intersectionOfTwoArrays):
#get the messageId and do what you want
finally you can filter the notification as you want!.
It is better to store histroyId and update it with every
notification and use it when you get the history. It will help you
to get updated history only.
Please note I used python when I was building my sever. So above demo code written using python
It looks like history.list added a new parameter "historyTypes". If you set that to "messageAdded", the api will only return history records of that type.

Gmail API doesn't returns modified time for emails

The get message API, Gmail API to fetch particular email, doesn't returns modified time of email. Modified time should change whenever someones reads an email.
Also there is a field called 'Date' which is return as part of 'payload.headers[].name'. What does this value indicates? From my testing it stores sent/received time of emails.
Use case: WebDav clients expects modified time for legal hold policies.
Reference: https://developers.google.com/gmail/api/v1/reference/users/messages/get
Snippet of message received from Gmail API:
{
"historyId":"8567",
"id":"14e87d4358bde1db",
"internalDate":"1436797907000",
"labelIds":["INBOX","IMPORTANT","CATEGORY_PERSONAL","UNREAD"],
The internalDate epoch timestamp is at least in Java available using message.getInternalDate method.
I am not 100% sure if this value is updated on reading the message, but I assume so since it updates the message labels by removing UNREAD label.

How to retrieve message based on uuid in PUBNUB?

How can we get the list of incoming messages to a particular user based on uuid ?
What need is to show the list of message for a particular user based on uuid , from all the channel the user is subscribed?
The requirement is to list the incoming message of a particular user. Same as the one we see in facebook message icon on right side
The available solution what i got is
1) get the channellist with uuid
PubNub.ngWhereNow()
2) get message from the channels return from the ngWhereNow
I need help to sort out whether there is any single api to get all the incoming message to based on uuid
The easiest thing to do is to create a unique channel name, based on the UUID (or that maps to the UUID). Then you could subscribe to this channel to get messages for that user, pull history on it for later, etc.
Would that work for you?

Resources