In my last article I discussed about Part 21 - Windows Phone 7 - Toast Push Notification, in this article we will discuss about Windows Phone 7 Tile Push Notification. Tile notification can update texts and images. Ideally, Tile notification contains a image and two strings. Tile notification is useful when information keep undating in frequent interval.

Let's write code:
Step 1: Create a windows phone application project.

Step 2: Place below using Directive in MainPage.xaml.cs
using Microsoft.Phone.Notification; using System.Diagnostics;
Step 3: Create a button and a textbox in the MainPage.xaml
<Button Content="Create Channel" Height="72" HorizontalAlignment="Left" ame="Call" VerticalAlignment="Top" Width="250" Click="btnCreateChannel_Click" /> <TextBox x:Name="txtURI" Height="72" Margin="0,0,0,0"/>
Step 4: Place below code in the MainPage.xaml.cs after the MainPage constructor. The below code will print the opened channel in the textbox and in the Debug window. URI of opened channel will be required by the Windows Application which will send the notification.
private void OnChannelUriChanged(Uri value) { Dispatcher.BeginInvoke(() => { txtURI.Text = value.ToString(); });
Debug.WriteLine("URI: " + value.ToString()); }
Step 5: Create button event handler of btnCreateChannel which will invoke
private void btnCreateChannel_Click(object sender, RoutedEventArgs e) { SetupChannel(); }
Step 6: Place SetUpChannel method code in the MainPage.xaml.cs. If the channel is already exist but ChannelURI is null we need to close the channel and reopen it. If channel does not exist create HttpNotificationChannel and attach NotificationChannelUriEventArgs and NotificationEventArgs event handler.
private void SetupChannel() { HttpNotificationChannel httpChannel = null; string channelName = "DemoChannel";
//if channel exists, retrieve existing channel httpChannel = HttpNotificationChannel.Find(channelName);
if (httpChannel != null) { //If we cannot get Channel URI, then close the channel and reopen it if (httpChannel.ChannelUri == null) { httpChannel.UnbindToShellToast(); httpChannel.Close(); SetupChannel(); return; } else { OnChannelUriChanged(httpChannel.ChannelUri); } BindToShell(httpChannel); } else { httpChannel = new HttpNotificationChannel(channelName); httpChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(httpChannel_ChannelUriUpdated); httpChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(httpChannel_ShellToastNotificationReceived); httpChannel.Open(); BindToShell(httpChannel); } }
Step 7: If the application is running the below method will read the notification message and print on the textbox.
void httpChannel_ShellToastNotificationReceived(object sender, NotificationEventArgs e) { Dispatcher.BeginInvoke(() => { txtURI.Text = "Toast Notification Message Received: "; if (e.Collection != null) { Dictionary<string, string> collection = (Dictionary<string, string>)e.Collection; System.Text.StringBuilder messageBuilder = new System.Text.StringBuilder(); foreach (string elementName in collection.Keys) { txtURI.Text += string.Format("Key: {0}, Value:{1}\r\n", elementName, collection[elementName]); } } }); }
Step 8: BindToShellToast of HttpNotificationChannel needs to be called to bind toast notification.
private static void BindToShell(HttpNotificationChannel httpChannel) { try { httpChannel.BindToShellTile(); } catch (Exception) { } }
Step 9: Below code will be triggered whenever ChannelURI will be updated.
void httpChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e) { //You get the new Uri (or maybe it's updated) OnChannelUriChanged(e.ChannelUri); }
Step 10: Create a Maroon image of 173 X 173 and add the image in the project. Change the Build Action property of the image to Content from Resource.
On running the application Windows Phone 7 emulator will look like below image.

Now we need to create Windows Appliction to send notification.
Step 1: Create a C# project and name it Windows Phone 7 - Send Push Notification.
Step 2: Design a screen like below.

Step 3: Add below using directive in the code behind.
using System.Net; using System.IO;
Step 4: Add a button handler of btnSendNotification. To push a notification Post requires URI along with X-WindowsPhone-Target and X-NotificationClass header. The tile push notification xml will contain Background image name, title and text.
string tileMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<wp:Notification xmlns:wp=\"WPNotification\">" + "<wp:Tile>" + "<wp:BackgroundImage>" + txtBackground.Text + "</wp:BackgroundImage>" + "<wp:Count>" + txtFrontTitle.Text + "</wp:Count>" + "<wp:Title>" + txtText.Text + "</wp:Title>" + "</wp:Tile> " + "</wp:Notification>";
HttpWebRequest is required to post the Tile Notification to Push Notification Service. Http Post is only allowed to send notification.
private void btnSendNotification_Click(object sender, EventArgs e) { try { string subscriptionUri = txtURL.Text; HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri); sendNotificationRequest.Method = "POST";
// Create the Tile message. string tileMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<wp:Notification xmlns:wp=\"WPNotification\">" + "<wp:Tile>" + "<wp:BackgroundImage>" + txtBackground.Text + "</wp:BackgroundImage>" + "<wp:Count>" + txtFrontTitle.Text + "</wp:Count>" + "<wp:Title>" + txtText.Text + "</wp:Title>" + "</wp:Tile> " + "</wp:Notification>";
// Set the notification payload to send. byte[] notificationMessage = Encoding.Default.GetBytes(tileMessage);
// Set the web request content length. sendNotificationRequest.ContentLength = notificationMessage.Length; sendNotificationRequest.ContentType = "text/xml"; sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "token"); sendNotificationRequest.Headers.Add("X-NotificationClass", "1");
using (Stream requestStream = sendNotificationRequest.GetRequestStream()) { requestStream.Write(notificationMessage, 0, notificationMessage.Length); }
// Send the notification and get the response. HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse(); string notificationStatus = response.Headers["X-NotificationStatus"]; string notificationChannelStatus = response.Headers["X-SubscriptionStatus"]; string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"]; lblStatus.Text = "Status: " + notificationStatus + " : " + deviceConnectionStatus; } catch (Exception ex) { lblStatus.Text = "Exception caught sending update: " + ex.ToString(); } }
We have finish the coding. Now lets run the Windows Phone client application and create channel and copy the URI from output window.
If you are unable to see output window, turn it on from View menu of Visual Studio.
Now let's run the Windows Send Notification applicaiton and enter the URI generated in above step, title, text and backgroung image name. Enter Maroon.jpg in the background image.

Go back to Windows Phone 7 emulator and pin the client application to appear as tile. Hold the client application with mouse and you will get option of "Pin to start" and "Uninstall". Select "Pin to start" which will pin the client application as Tile.

Now press send button of Windows Application to send push notification. You will receive notification.

This ends the article of Windows Phone 7 Toast Notification.
|