InfiniTec - Henning Krauses Blog

Don't adjust your mind - it's reality that is malfunctioning

Simple push notification client

As I wrote in my earlier post, I recently published a component on CodePlex that simplifies using push notifications in your applications. Since I’ve not published a full fledged sample application, I will start with a series of blog posts. I will tag all related articles on this topic with “push notifications”, and you can get a list of all articles with this link: http://www.infinitec.de/?tag=/push+notifications.

I’ll start with a very simple application: A console application that creates one subscription get notifications about new mails arriving in the mailbox of the current user.

   1: using System;
   2: using System.Net;
   3: using InfiniTec.Exchange.Notifications;
   4:  
   5: namespace ExchangeNotificationTestClient
   6: {
   7:     internal class Program
   8:     {
   9:         private static void Main()
  10:         {
  11:             // Ignore any certificate errors
  12:             ServicePointManager.ServerCertificateValidationCallback += 
  13:                 (sender, certificate, chain, sslPolicyErrors) => true;
  14:  
  15:             // Setup the adapter which will be used to call into the Exchange WebService
  16:             var adapter = new ExchangeWebServicesAdapter(
  17:                 new Uri("https://casserver/ews/exchange.asmx"), 
  18:                 new NetworkCredential("administrator", "password", "contoso"));
  19:  
  20:             // Create a new subscription collection to manage all the subscriptions
  21:             var subscriptionCollection = new SubscriptionCollection(adapter);
  22:  
  23:             // Setup a listener that listens on port 80 on the local computer
  24:             using (var listener = new PushNotificationListener())
  25:             {
  26:                 // Register for a NewMail notification on the inbox of the administrator
  27:                 subscriptionCollection.Add(
  28:                     new[] {new FolderReference(WellKnownFolderId.Inbox)}, 
  29:                     EventTypes.NewMail);
  30:                 Console.Out.WriteLine("Starting Notification Service...");
  31:                 listener.Start();
  32:  
  33:                 Console.Out.WriteLine("Creating subscription");
  34:  
  35:                 foreach (var subscription in subscriptionCollection)
  36:                 {
  37:                     // Write a line to the console for each new mail received
  38:                     subscription.NewMail += (sender, e) => 
  39:                         Console.Out.WriteLine(string.Format("{0}: New Mail arrived in your inbox", e.Timestamp));
  40:                     subscription.Start(listener);
  41:                 }
  42:  
  43:                 Console.Out.WriteLine("Waiting for notifications... Hit [Enter] to quit...");
  44:  
  45:                 Console.ReadLine();
  46:             }
  47:         }
  48:     }
  49: }

There are four important classes used in this example:

  1. The ExchangeServiceAdapter (created in line 16) is used to actually perform the Web Services calls to the Exchange Server (CAS Role). It’s a rather simple implementation, as it does not support AutoDiscover. You have to specify the Exchange server manually. It does, however, support Exchange Impersonation. And if you want to subscribe to events on public folders, you’ll have to enable this feature by setting the ExchangeServiceAdapter.IsPublicFolderAccessEnabled to true. Your Exchange Server must have Service Pack 1 installed to use this feature.
  2. The PushNotificationListener (created in line 24): This class does all the necessary WCF plumbing to setup a host, receives the notifications and channels them to the right subscription.
  3. The SubscriptionCollection: (created in line 21) It’s not really necessary, but it makes is more easy to handle multiple subscriptions.
  4. The Subscription (created in line 27). You can either create a subscription by calling SubscriptionCollection.Add(), or create a subscription directly via “new Subscription()”.

Both, the PushNotificationListener and the Subscription need to be started to do some actual work. And that’s all you need to do to get a simple notification client up and running. However, there are some security settings to consider, because WCF doesn’t let you run around and open endpoints on your users machines. Additionally, the Windows Firewall needs to be configured correctly to let the notifications through. I’ll discuss this in a separate post.


Posted by Henning Krause on Tuesday, December 30, 2008 10:55 PM, last modified on Saturday, November 27, 2010 6:04 AM
Permalink | Post RSSRSS comment feed

Comments (2) -

On 7/19/2012 2:16:54 PM munish wrote:

munish

This is really helpful- but exact code copy resulted in compilation error on line number 21 and line 40 due to SubscriptionCollection constructor accepts 2 parameters and subscribtion.start() doesn't take any parameter.

Also, after correcting the code and compiling it successfully - I'm not receiving any notifications though I'm able to subscribe to CAS. I do not have access to our Exchange server 2007 so I can't use your trouble shooting application - www.infinitec.de/.../...otifications-failures.aspx


Can you help me with your expertise?

On 10/15/2012 11:47:36 PM Daniel wrote:

Daniel

Henning, thank you for sharing your knowledge.

I tried to create multiple subscriptions but am getting "A call to the Exchange WebServices failed. Error: ErrorFolderNotFound; The specified folder could not be found in the store."

Could you give me some insight on what could cause this.  Below is snippet of code I used.

               NetworkCredential credentials = new NetworkCredential()
                {
                    UserName = "dummyuser",
                    Password = "dummypassword",
                    Domain = "dummydomain"
                };

                Uri uri = new Uri(dummyURL);

                ServiceAdapter = new ExchangeWebServicesAdapter(uri, credentials);
                ExchangeConnector.Credential = credentials;

                PushListener = new PushNotificationListener();
                PushListener.Start();

                SubCollection = new SubscriptionCollection(ServiceAdapter, PushListener);

                foreach (ExUser user in _userList)
                {

                    InfiniTec.Exchange.Notifications.Subscription subscription = SubCollection.Add(new[] { new FolderReference(WellKnownFolderId.Calendar, new MailboxReference(user.UserEmail)), new FolderReference(WellKnownFolderId.Contacts, new MailboxReference(user.UserEmail)) }, EventTypes.Created | EventTypes.Deleted | EventTypes.Modified | EventTypes.Moved, SubscriptionType.Push);
                    
                    subscription.ItemCreated += new EventHandler<ItemNotificationEventArgs>(subscription_ItemCreated);
                    subscription.ItemModified += new EventHandler<ItemNotificationEventArgs>(subscription_ItemModified);
                    subscription.ItemDeleted += new EventHandler<ItemNotificationEventArgs>(subscription_ItemDeleted);
                    subscription.PollIntervalInMinutes = 1;
                    subscription.Start(); //its failing at this point

                }