Exercise 3: Creating the List Event Receiver to Send Notifications

In this exercise, you will create the SharePoint list event receiver to send tile notifications with a notification count to registered devices. The SharePoint 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 the WP7.Notification.Events solution, and select Add | Existing Project.
  2. Browse to EventNotification.csproj located in the EventNotification folder and select it.
  3. Click Open.

    Figure 4

    Solution Explorer with WCF Service project and Event Receiver project

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

In this task, you will complete the existing Notification class to send a 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.2.6 comment to define the SendNotification method:

    C#

    public static void SendNotification(string channelUri, int count) { string tilePushXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<wp:Notification xmlns:wp=\"WPNotification\">" + "<wp:Tile>" + "<wp:Count>{0}</wp:Count>" + "</wp:Tile> " + "</wp:Notification>"; string tilePushXmlFormatted = string.Format(tilePushXml, count); HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(channelUri); sendNotificationRequest.Method = "POST"; sendNotificationRequest.Headers = new WebHeaderCollection(); sendNotificationRequest.ContentType = "text/xml"; sendNotificationRequest.Headers.Add("X-NotificationClass", "1"); sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "token"); byte[] strBytes = new UTF8Encoding().GetBytes(tilePushXmlFormatted); 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. // https://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. The XML defines the type of notification as well as the details of the notification. A Tile notification can set the count, background image and the text of the title. This method will send the phone a count value based on the passed in parameter value. This method is similar to the SendNotification method located in the WCF registration service. The difference is that this method adds the current count to the notification instead of sending a default count of 0.

    The Xml payload is sent to the Push Notification Service which then send the notification to the device using the channel between the device and the Push Notification Service.

Task 3 – 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 4 – Verifying the Event Receiver Builds and Deploys Correctly.

In this task, you verify the event receiver and WCF service build and deploy 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.