none
PushNotification Subscription EventType Question RRS feed

  • Question

  • I am having an issue arise with a PushNotification subscription and hopefully someone can lead me in the right direction. Any help is appreciated. I have a windows service that is handling all the subscriptions for each mailbox. We have a user table holds the subscription id, watermark, last status date time. Then we have a web service that processes the notifications the service takes the messages and looks to see if they are to/from a known contact then if they are inserts a record in a pending message table. To get that for each mailbox, I need the subscription to be for New Mail items and Sent Mail items. Below is the code that handles both sides.

     

    Here is the code that creates the subscription. I am using the EWS Managed API v1.1 to create the subscription.

     

    PushSubscription pushSubscription = ewService.SubscribeToPushNotifications(
                    new FolderId[] { WellKnownFolderName.Inbox, WellKnownFolderName.SentItems },
                    Settings.ListenerService, 30, user.Watermark,
                    EventType.NewMail, EventType.Created);

     

    Here is the code that is called from the PushNotification subscription:

     

    public SendNotificationResultType SendNotification(SendNotificationResponseType SendNotification1)
    {
     using (var _users = new ExchangeUser(Settings.SqlConnectionString))
     {
     var result = new SendNotificationResultType();
    
     var responseMessages = SendNotification1.ResponseMessages.Items;
    
     foreach (var responseMessage in responseMessages)
     {
     if (responseMessage.ResponseCode != ResponseCodeType.NoError)
     {
     //log error and unsubscribe.
     result.SubscriptionStatus = SubscriptionStatusType.Unsubscribe;
     return result;
     }
    
     var sendNoficationResponse = responseMessage as SendNotificationResponseMessageType;
     if (sendNoficationResponse == null)
     {
     result.SubscriptionStatus = SubscriptionStatusType.Unsubscribe;
     return result;
     }
    
     var notificationType = sendNoficationResponse.Notification;
     var subscriptionId = notificationType.SubscriptionId;
     var previousWatermark = notificationType.PreviousWatermark;
    
     User user = _users.GetById(subscriptionId);
     if (user != null)
     {
     BaseNotificationEventType[] baseNotifications = notificationType.Items;
      
     for (int i = 0; i < notificationType.Items.Length; i++)
     {
      if (baseNotifications[i] is BaseObjectChangedEventType)
      {
      var bocet = baseNotifications[i] as BaseObjectChangedEventType;
      AccessCreateDeleteNewMailEvent(bocet, ref user);
      }
     }
     }
     }
    
     result.SubscriptionStatus = SubscriptionStatusType.OK;
     return result;
     }
    }
    

     

    The code above processes properly when it is a Sent Mail item. The problem comes when it is a New Mail message coming into the server. I am seeing two Notification.Items instead of one like when sent mail notification comes in. And sense I am looping over the items I get two records inserted into my database when there should be only one.

     

    1.     How does the EventType.NewMail differ from EventType.Created?

    2.     Will the EventType.NewMail see a message even if there are server side rules in place to move the message?

    3.     If you just have the folder set to WellKnownFolderName.Inbox with EventType.Created will it notify you of all incoming mail messages?

     

    I have a work around that I can put in place that will stop it from duplicating messages. I can add a field to the pending messages table to store the item id of the mail message then when I go to insert the record check to make sure that items id doesn't already exist.

     

     

    • Edited by tnederveld Friday, March 11, 2011 10:20 PM added note about version of exchange
    Friday, March 11, 2011 9:02 PM

All replies