18
votes

I am using Gmail Push Notifications with Google PubSub and have a custom label that I want to monitor for any changes. I use the following code to register a watch for the label (Id of the label is Label_1)

WatchRequest wr = new WatchRequest();
wr.TopicName = "projects/" + primaryLink.ggProjectId + "/topics/iLink" + segmentId;
if (labels != null && labels.Count > 0)
{
    wr.LabelIds = new List<string>();
    wr.LabelIds.Add("Label_1");
    wr.LabelFilterAction = "include";
}

WatchResponse wrr = gs.Users.Watch(wr, emailAccount).Execute();
return "HistoryId " + wrr.HistoryId.ToString();

}

The watch registers OK. The issue is that I get push notifications for any Gmail change not just those under the label.

Are custom labels supported?

1
User labels should work fine as well. Are you sure it's not a different label change was made to a message that also had Label_1? (e.g. a Label_1 message was marked as unread, but Label_1 was not changed on the message.)Eric D
No is occuring for example when sending an email - this is not related to the label. Originally there was a watch for the user with no labels specified. Does a new watch command (with a label filter) override the previous watch? I also tried calling Stop() before the new command?PNC
One watch() should overwrite the previous one. You could call stop() and leave it that way for a while to ensure the old one is gone though (in that case it would be a bug that the new watch() isn't overriding the old one).Eric D
I can confirm this is still happening in v1.Spencer Easton
@PNC There is a google issue tracker entry for this bug. Could you kindly update your post with a reference to issuetracker.google.com/issues/36759803 with a note to star the issue? The more people who star it the sooner it will be fixed.TheAddonDepot

1 Answers

0
votes

I noticed the same issue but later on found out that its because of the way API works. You can filter the emails via LabelIds but you will receive notifications only if emails are directly being filtered to selected custom label. I guess its design rather than a flaw in the API.

To test this, create a custom filter in Gmail which would directly apply your custom label to a set of emails and you should be receiving notifications for those emails.

Edited (June 11, 2015): Push notification send you HistoryID and user's mailbox name. In response your endpoint should call userhistory.list() with HistoryID and LabelId you want to monitor for changes.

$opt_param = array();
$opt_param['startHistoryId'] = $historyID;
$opt_param['labelId'] = $labelID;
$opt_param['fields'] = 'nextPageToken,historyId,history/messagesAdded';

$service->users_history->listUsersHistory($userID, $opt_param);

Above is a PHP code snippet to filter the history list with historyID and labelID.