ews-java-api icon indicating copy to clipboard operation
ews-java-api copied to clipboard

How to code a listener when I use SubscribeToPushNotifications() to get the new mails.

Open icezmb opened this issue 11 years ago • 10 comments

Hi all. Now I want to use SubscribeToPushNotifications() to let the server push new mails to my app.

/=====================================================/ Public PushSubscription subscribeToPushNotifications( Iterable folderIds, URI url, int frequency, String watermark, EventType... eventTypes) throws Exception{} /=====================================================/ The function SubscribeToPushNotifications() has a few parameters, and one of them is URI. I don't know how to write a listener, anyone can help me? Please, thanks a lot.

icezmb avatar Dec 29 '14 09:12 icezmb

is there a bug/problem on the readme using-push-notifications-with-the-ews-java-api? ref. #154

serious6 avatar Dec 30 '14 10:12 serious6

+1, what is a listener url ? socket url ?

XciD avatar Jan 29 '15 12:01 XciD

any update ?

XciD avatar Jul 13 '15 11:07 XciD

I just succeed to set up the push request, but I don't know how to deserialize the request from the Exchange Server :

<?xml version="1.0" encoding="utf-8"?>
<soap11:Envelope xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/">
  <soap11:Header>
    <t:RequestServerVersion xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" Version="Exchange2010_SP2" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" />
  </soap11:Header>
  <soap11:Body>
    <m:SendNotification xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages">
      <m:ResponseMessages>
        <m:SendNotificationResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:Notification>
            <t:SubscriptionId>JwB2aTFwcjA1bWIxMDA4LmV1cnByZDA1LnByb2Qub3V0bG9vay5jb20QAAAA+l+lLQTaA06BVIWNwne7WF/DUcp2i9IIEAAAAAvWSXbzJs5Mgx6zO1iHfjE=</t:SubscriptionId>
            <t:PreviousWatermark>AQAAAMFcdQJ5b45EkzokImftRv6WKucBAAAAAAA=</t:PreviousWatermark>
            <t:MoreEvents>false</t:MoreEvents>
            <t:StatusEvent>
              <t:Watermark>AQAAAMFcdQJ5b45EkzokImftRv5kK+cBAAAAAAE=</t:Watermark>
            </t:StatusEvent>
          </m:Notification>
        </m:SendNotificationResponseMessage>
      </m:ResponseMessages>
    </m:SendNotification>
  </soap11:Body>
</soap11:Envelope>

XciD avatar Jul 13 '15 12:07 XciD

:+1: I don't understand how to get notification either.

I assume I should use something like the following code, but

  • output of http request is "OK" for both AsyncCallback and IAsyncResult

  • callback is called immediately but only once

    WellKnownFolderName wkFolder = WellKnownFolderName.Inbox;
    FolderId folderId = new FolderId(wkFolder);
    List<FolderId> folder = new ArrayList<FolderId>();
    folder.add(folderId);
    String url = ...
    int frequency = 1; // minutes from 1 to 1440
    AsyncCallback cb = new AsyncCallbackImplementation() {
        @Override
        public Object processMe(Future<?> task) {
            System.out.println("In AsyncCallback : " + task.isDone());
            try {
                HttpClientWebRequest r = (HttpClientWebRequest)task.get();
    
                System.out.println("AsyncCallbackImplementation : " + r.getResponseText());
                return task.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (EWSHttpException e) {
                e.printStackTrace();
            }
            return null;
        }
    };
    IAsyncResult result = service.beginSubscribeToPushNotifications(cb, null, folder, new URI(url), frequency, null, EventType.NewMail, EventType.Created, EventType.Deleted);
    System.out.println("IAsyncResult : " + ((HttpClientWebRequest)result.get()).getResponseText());
    Thread.sleep(100000000);
    PushSubscription subscription = service.endSubscribeToPushNotifications(result); 
    

Using 2.0-SNAPSHOT

jzy3d avatar Jul 22 '15 12:07 jzy3d

Any advance on this topic?

acaride avatar Nov 17 '15 12:11 acaride

No, I have been working on something else as I had no answer to make it work :(

Le mardi 17 novembre 2015, Alejandro Caride [email protected] a écrit :

Any advance on this topic?

— Reply to this email directly or view it on GitHub https://github.com/OfficeDev/ews-java-api/issues/151#issuecomment-157356786 .

jzy3d avatar Nov 17 '15 13:11 jzy3d

I also managed to configure a push notification to work, but only one event (repeated 3 times) is received. It looks like I'm not returning something the server is expected and terminate the subscription. The doc https://msdn.microsoft.com/en-us/library/office/dn458791(v=exchg.150).aspx#bk_eventtypes says something about returning, on the listener side, a StatusFrequency element in the SendNotificationResponseMessage.

Any clue how to build that soap response message ?

ticapix avatar Dec 14 '17 14:12 ticapix

We see the same thing (although we're not trying to do async like you are - we just use service.SubscribeToPushNotifications()). You have to send a SOAP handshake of "OK" with the HTTP 200 response. Here is a packet capture of what it should look like (based on a capture of a .NET interaction):

    <soap:Envelope
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
        xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"
        xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
        <soap:Body>
            <m:SendNotificationResult>
                <m:SubscriptionStatus>
                    OK
                    </m:SubscriptionStatus>
                </m:SendNotificationResult>
            </soap:Body>
        </soap:Envelope>

It will retry 3 times if it does not receive this handshake.

bobdrad avatar Apr 10 '18 01:04 bobdrad

@bobdrad thanks. Have been searching hours. Its ridiculous, the spec for this listener is not mentioned anywhere

mastermind3043-zz avatar May 15 '18 12:05 mastermind3043-zz