Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subscribeToPullNotifications doesn't detect new emails in inbox

i am trying to detect new emails in inbox using subscribeToPullNotifications as follows:

PullSubscription subscription = service.subscribeToPullNotifications(
                folder, 1, null, EventType.NewMail);

        GetEventsResults events = subscription.getEvents();
        System.out.println("####### EVENTS: "
                + events.getItemEvents().toString());
        for (ItemEvent itemEvent : events.getItemEvents()) {
            if (itemEvent.getEventType() == EventType.NewMail) {
                EmailMessage message = EmailMessage.bind(service,
                        itemEvent.getItemId());
                System.out.println("######## NEW EMAIL MESSAGE IS: "
                        + message.getSubject());
            }
        }

but the events.getItemEvents() is always empty, even i can see new emails in the inbox. also how to make the above code is always repeated while the application is running, so that each minute it check for new emails.

like image 434
Mahmoud Saleh Avatar asked Nov 18 '12 13:11

Mahmoud Saleh


1 Answers

Here it depends on when you are calling this, if suppose you are calling this as particular interval then you need to pass "WaterMark" of previous response in new request, else all events which occurred in between would be lost. method : subscription.getWaterMark()

need to pass this as thrid argument to method subscribeToPullNotifications()

else you can continously pull on the same service by placing that in loop :

 while (true) {

    GetEventsResults events = null;

    try {
         events = subscription.getEvents();
    } catch (Exception e1) {
         e1.printStackTrace();
    }

    for (ItemEvent itemEvent : events.getItemEvents()) {
        // do something...
    }

 }

But this would continuously pull from server increasing load, so rather use first approach by subscribing at regular interval, and passing previous water-mark in request.

like image 100
Devarsh Avatar answered Oct 30 '22 07:10

Devarsh