22

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?

Jordan
  • 693
  • 3
  • 16
PNC
  • 1,932
  • 19
  • 36
  • 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 Jun 02 '15 at 16:37
  • 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 Jun 02 '15 at 21:32
  • 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 Jun 03 '15 at 19:09
  • I can confirm this is still happening in v1. – Spencer Easton Sep 09 '15 at 03:35
  • I was just wondering if there was any update on this, or is calling history list with the history id and a filter for the label still the answer? – gdvalderrama Sep 13 '17 at 16:07
  • @guival I haven't touched it for a couple of years. We unfortunately use polling by label on a timed basis as the overhead is way less than dealing with the push notifications for every event in Gmail... – PNC Sep 13 '17 at 19:50
  • 1
    @PNC There is a google issue tracker entry for this bug. Could you kindly update your post with a reference to https://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 Jul 30 '18 at 14:29
  • Just found this SO post. Also doesn't work when you try to exclude labels as well. – Swordfish Jan 17 '19 at 18:47
  • This still doesn't work. – Nathan Boaldin Mar 13 '20 at 18:16

1 Answers1

0

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.

Furhan S.
  • 1,494
  • 2
  • 13
  • 22
  • 1
    I'm not sure I understand - my issue is that I get notifications for anything that happens in Gmail. This is despite only registering for notifications for just the one label? Sounds like this is not what is happening for you? – PNC Jun 10 '15 at 00:05
  • 3
    @PNC The problem is watch() hook gets you only the HistoryID and mailbox for every event, no matter what LabelId you specified in creating watch request. And historyID is mailbox-wide ID rather than LabelID-wide. So no matter if any of the label's content changes, HistoryID will be incremented. In response to push notification, your endpoint should call historyList.list(). This is where you could filter the messages with labelID you want to monitor. – Furhan S. Jun 11 '15 at 10:06
  • 4
    I have had the following update from one of the Google team for this area "You should be able to restrict watch() by label ids like you're specifying but I can confirm it doesn't work right now for *user* labels. The logic and bug is rather complicated, we're working on it but no ETA" – PNC Jun 12 '15 at 05:26
  • 1
    Ehh..I'm not sure it is only custom labels. After I couldn't get custom labels working I tried the default labels. Watch seems to ignore them all and sends all events. – Spencer Easton Sep 09 '15 at 03:32
  • 1
    The watch() hook is also ignoring the system labels ex. STARRED does anyone have any update on this? – Jason Jan 17 '17 at 16:03
  • 1
    From what I understand, this feature is broken for years and no one at google cares ? FYI, I also try to /watch for SENT label and I got notifications for "all events" – Benoit Patra May 10 '17 at 09:28
  • 1
    I am also trying to filter the emails which has the labelD "Label_1". But watch() returns all the E-Mails. So how do I go about it? Create a custom filter in GMail? What should the filter contain? I only want the Pub/Sub to send me the changes made to that label, 'Label_1'. Sorry but I wish the docs were much better in this regard. – Juggernaut Jun 26 '18 at 12:22
  • 2
    was this ever fixed by Google? i am still having the same issue, filters do not work. – jkamdjou Nov 19 '18 at 03:16
  • It is still broken as of 5th Sept. 2019. – Faiz Sep 05 '19 at 13:25
  • 6
    still a problem as of 20th Jan 2020. Full day of work wasted. yey – Edd Jan 20 '20 at 17:47
  • Still a problem as of March 10th 2020 – Anthony Liriano Mar 11 '20 at 00:28
  • Still a problem. – Nathan Boaldin Mar 13 '20 at 18:15
  • 2
    Still a problem as of April 6th 2020 – Erim B.Franci Apr 06 '20 at 20:42
  • 3
    Still a problem as of April 18th 2020 – Paritosh Piplewar Apr 17 '20 at 22:24
  • 1
    still a problem as of October 08th 2020 – Nitin Manocha Oct 08 '20 at 08:20
  • still a problem as of March 05th 2021 – Mian.Ammar Mar 05 '21 at 10:00
  • still a problem as of Oct 16, 2021. Seems like the `labelIds` were entirely ignored by Gmail API as I still receive notification from irrelevant email messages. I would still need to filter the labels/subjects manually in the PubSub handler, which is sad. – Jason Oct 16 '21 at 02:50
  • 1
    In any case, please star this bug issue (https://issuetracker.google.com/issues/36759803) to draw more attention to it. – Jason Oct 16 '21 at 03:26