Exercise 2: Complete the Notification Registration WCF Service

In this exercise, you will add methods to the existing registration WCF service to manage the notification count for each registration.

Task 1 – Beginning the Exercise

In this task, you will open the lab solution in Visual Studio 2010.

  1. Make sure that you have downloaded and installed the items listed in System Requirements above prior to beginning this exercise.
  2. Launch Visual Studio 2010 as administrator and open the lab project by selecting File » Open » Project.
  3. Browse to the WP7.Notification.Events.sln file located at %TrainingKitPath%\Labs\IntegratingTileNotifications\Source\Before and select it.
  4. Click Open to open the solution.

Task 2 – Adding a Count Property to the ListNotificationRegistration Class

In this task, you will add an integer property to the existing ListNotificationRegistration class.

  1. In the NotificationRegistration project, open the ListNotificationRegistration.cs file located in the Model folder.
  2. Add the following code under the //TODO: 7.2.1 comment to define the CurrentCount property:

    C#

    int count; public int CurrentCount { get { return count; } set { count = value; } }

    The above property is used to maintain the current count of notifications that have been sent since the last time the phone application has cleared the count.

  3. Save and close the ListNotificationRegistration.cs file.

Task 3 – Declaring Service Methods to Manage the Count

In this task, you will complete the existing IListNotificationChannels.cs interface to include the required methods declarations to manage the notification count for each registrations.

  1. In the NotificationRegistration project, open the IListNotificationChannels.cs file.
  2. Add the following code under the //TODO: 7.2.2 comment to define the following two method declarations:

    C#

    [OperationContract] void ClearRegistrationCount(string channelUri, string listId); [OperationContract] List<ListNotificationRegistration> RetrieveChannelURIsForListIdWithCountIncrement(string listId);

  3. The above code defines the two methods used to clear a registration count and increment a registration count. The OperationContract attribute decorates these methods to expose the methods as part of the public interface.

Task 4 – Implementing the New Service Methods

In this task, you will complete the existing ListNotificationChannels class to include the declared interface methods from Task 3.

  1. In the NotificationRegistration project, open the ListNotificationChannels.svc file.
  2. Add the following code under the //TODO: 7.2.3 comment to define the RetrieveChannelURIsForListIdWithCountIncrement method:

    C#

    public List<ListNotificationRegistration> RetrieveChannelURIsForListIdWithCountIncrement(string listId) { var items = from r in ListNotificationRegistrations where r.ListId.ToLower() == listId.ToLower() select r; List<ListNotificationRegistration> notifications = items.ToList(); notifications.ForEach(n => n.CurrentCount++); return notifications; }

    The above code uses Linq to retrieve the registrations for the specific list id. It then increments each registration by one and returns the ListNotificationRegistration collection. The event receiver will call this method for each event managed by the event receiver.

  3. Add the following code under the //TODO: 7.2.4 comment to define the ClearRegistrationCount method:

    C#

    public void ClearRegistrationCount(string channelUri, string listId) { var reg = (from r in ListNotificationRegistrations where r.ChannelUri.ToLower() == channelUri.ToLower() && r.ListId.ToLower() == listId.ToLower() select r).SingleOrDefault<ListNotificationRegistration>(); if (reg != null) { reg.CurrentCount = 0; Notification.SendNewTile(channelUri); } }

    The above code uses Linq to retrieve a single ListNotificationRegistration object for the specific channel Uri and list id. If a matching ListNotificationRegistration is found the CurrentCount is set to zero and the service will send a new tile notification with a zero count. A zero count will remove the counter from the tile located on the phone. This method is called by the phone application each time the application starts.

Task 5 – Implementing the Method to Send a Tile Notification with a Zero Count

In this task, you will complete the existing Notification class to send a tile notification to a specific channel Uri with a zero count.

  1. In the NotificationRegistration project, open the Notification.cs file.
  2. Add the following code under the //TODO: 7.2.5 comment to define the SendNewTile method:

    C#

    public static void SendNewTile(string channelUri) { 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>"; 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(tilePushXml); 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 an 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 sends the static count of zero to the phone. A count of zero will remove the counter from the tile.

    The notification is sent using the channel Uri. It is sent to the Push Notification Service which sends the message to the device using the connection between the device and Push Notification Service. This notification will clear the count from the application tile.

Task 6 – Running the Service in Visual Studio 2010

In this task, you will run the NotificationRegistration service to verify it starts correctly.

  1. In the Solution Explorer, in the NotificationRegistration project, right click ListNotificationChannels.svc and select Set as Start Page.
  2. Press F5 to run the service. The service should start in the WCF Test Harness or display the Service page in the web browser.

    Figure 2

    NotificationRegistration WCF Test Client

    Figure 3

    NotificationRegistration WCF Services in Internet Explorer

  3. In Visual Studio, press Shift + F5 to stop debugging.