Exercise 3: Creating the List Event Receiver to Send Notifications

In this exercise, you will create a list event receiver to send toast notifications to registered devices. The list event receiver will be associated with calendar lists.

Task 1 – Adding the EventNotification Project to the Solution

In this task, you add the existing EventNotification project to the solution.

  1. In the Solution Explorer, right-click WP7.Notification.Events and select Add | Existing Project.
  2. Browse to EventNotification.csproj located in the EventNotification folder.
  3. Click Open.

    Figure 4

    Solution Explorer with WCF Service project and Event Receiver project

Task 2 – Completing the Notification.cs Class to Send Toast Notifications

In this task, you will complete the existing Notification class to send toast notification to any devices registered for notification.

  1. In the EventNotification project, open the Notification.cs file.
  2. Add the following code under the //TODO: 7.1.1 comment to define the SendNotification method:

    C#

    public static void SendNotification(string channelUri, string text1, string text2) { string ToastPushXML = "<?xml version='1.0' encoding='utf-8'?>" + "<wp:Notification xmlns:wp='WPNotification'>" + "<wp:Toast>" + "<wp:Text1>{0}</wp:Text1>" + "<wp:Text2>{1}</wp:Text2>" + "</wp:Toast>" + "</wp:Notification>"; string str = string.Format(ToastPushXML, text1, text2); HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(channelUri); sendNotificationRequest.Method = "POST"; sendNotificationRequest.Headers = new WebHeaderCollection(); sendNotificationRequest.ContentType = "text/xml"; sendNotificationRequest.Headers.Add("X-NotificationClass", "2"); sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "toast"); byte[] strBytes = new UTF8Encoding().GetBytes(str); sendNotificationRequest.ContentLength = strBytes.Length; using (Stream requestStream = sendNotificationRequest.GetRequestStream()) { requestStream.Write(strBytes, 0, strBytes.Length); } try { var response = (HttpWebResponse)sendNotificationRequest.GetResponse(); var notiticationStatus = response.Headers["X-NotificationStatus"]; var notitificationChannelStatus = response.Headers["X-SubscriptionStatus"]; var deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"]; } catch { // Ignoring the response. // In a production application you should review the response and // code appropriate for the specific response. // http://msdn.microsoft.com/en-us/library/ff941100%28v=VS.92%29.aspx } }

    The above code creates a HttpWebRequest object and posts XML to the channel Uri provided to the method. This method is called once for each channel (device) that is registered for notification with a matching list id.l The XMLpayload defines the type of notification (Toast) as well as the details of the notification ( text1 and text 2). A Toast notification accepts two text fields. Push Notification Services requires the additional header values included in the above code.

  3. Add the following code under the //TODO: 7.1.2 comment to define the Notify method:

    C#

    public static void Notify(string listId, string text1, string text2) { NotificationRegistration.ListNotificationChannels svc = new NotificationRegistration.ListNotificationChannels(); string[] channelUris = svc.RetrieveChannelURIsForListId(listId); foreach (var channel in channelUris) { SendNotification(channel, text1, text2); } }

  4. The above code uses the proxy class Visual Studio 2010 generated for the NotificationRegistration WCF service to retrieve an array of channel URIs for any device registered for notifications for this specific list id. There will be one entry for each channel (device) registered for notifications for the list. This method will call the SendNotification method for each channel returned from the registration service.
  5. Save and close the Notification.cs file.

Task 3 – Completing the EventListNotification.cs Class to Send Toast Notifications

In this task, you will complete the existing EventListNotification class to send a Toast notification to any devices registered for notification.

  1. In the EventNotification project, open the EventListNotification.cs file located in the EventListNotification SharePoint Project Item.
  2. Add the following code under the //TODO: 7.1.3 comment to define the Notify method:

    C#

    private void Notify(SPItemEventProperties properties, string eventName) { string listId = properties.ListId.ToString(); string listTitle = properties.List.Title; Notification.Notify(listId, string.Format("{0}: ", listTitle), string.Format("Item: {0}", eventName)); }

  3. The above code is called by the specific events in the receiver class. The code will call the static Notify method of the Notification class passing in the list id and the values for the notification’s text message. For each notified event of the event receiver, the receiver code will send notifications to devices registered for notifications for this list.
  4. Save and close the EventListNotification.cs file.

Task 4 – Adding a Reference to the NotificationRegistration WCF Service

In this task, you will add a reference to the NotificationRegistration WCF service.

  1. In the Solution Explorer, in the EventNotification project, right click Service References and select Add Service Reference.
  2. Click Advanced…
  3. Click Add Web Reference…
  4. Click Web services in this solution

    Figure 5

    Selecting Web Services in this solution

  5. Click ListNotificationChannels

    Figure 6

    Selecting the ListNotificationChannels service

  6. In the Web reference name textbox enter NotificationRegistration.

    Figure 7

    Entering the Web reference name for the service

  7. Click Add Reference.

Task 5 – Verifying the Event Receiver Builds and Deploys Correctly.

In this task, you verify the event receiver and WCF service builds and deploys correctly.

  1. In the Solution Explorer, right click the WP7.Notification.Events solution and select Properties.
  2. Select Multiple startup projects.
  3. Set the action for EventNotification to Start.
  4. Set the action for NotificationRegistration to Start.

    Figure 8

    Selecting multiple startup objects in Visual Studio 2010

  5. Click OK.
  6. In the Solution Explorer, select the EventNotification project.
  7. Set the Site URL property to the site containing the Maintenance Training Schedule list.

    Figure 9

    Setting the Site URL for the event receiver project

  8. In the WP7.Notification.Events solution, press F5.
  9. Verify the solution builds and deploys without error.
  10. In the WP7.Notification.Events solution, press Shift +F5 to stop debugging.