Floating FB popout byF5debug

Learn Windows Phone 7 Development in 31 Days – Day 13 – Working with Tile Notification in WP7

 

Introduction:

In this article we are going to see how to work with Tile Notification which is one of the Microsoft Push Notification Services available. In our earlier article we have seen how to use the Toast Notification services to create a channel and update the toast messages to the Windows Phone 7 Device with the step by step process. In this tutorial we will follow the same process and see the steps to create the channel and sent the notifications messages.   

Tile Notifications are used to show up to date status of an application on the windows phone 7 start screen as a tile. This will work only if the application is pinned to the start screen, basically we can see 3 types of information on the Tile Notifications as follows

  • Count – Also we call this as Badge, is an integer value between 0 to 99. We can specify the count as per the requirement which can be updated on the notifications.         [more]
  • Background Image – An image property which will display the images as the background for the tile.
  • Title – Title of the application which should normally be with in 15 characters, exceeding the 15 characters will be truncated automatically.

The tile images can be a .jpg or .png files and should be of 173 X 173 pixels to have a better appearance. If less than the specified pixels will be automatically stretched and the look and feel will differ. Let us jump start to see the step by step process on how to achieve these tasks.

Steps:

Open Visual Studio 2010 and create a new SIlverlight for Windows Phone 7 Application with a valid project name as shown in the screen below.

clip_image002

Now let us start designing the interface to create a channel, as we discussed in our previous article () we will create the channel first which will be required to communicate for the Tile Notifications. if there are any already available notification events available it will use the same else it will create a new notification event and then connects through the channel. Add the below XAML code to get the channel notification to trigger.
XAML Code:

01

<!--TitlePanel contains the name of the application and page title-->

02

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

03

<TextBlock x:Name="ApplicationTitle" Text="F5DEBUG WP7 TUTORIALS" Style="{StaticResource PhoneTextNormalStyle}"/>

04

<TextBlock x:Name="PageTitle" Text="Tile Notification" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

05

</StackPanel>

06

 

07

<!--ContentPanel - place additional content here-->

08

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

09

<Button Content="Establish Channel!!!" Height="149" HorizontalAlignment="Left" Margin="73,88,0,0" Name="button1" VerticalAlignment="Top" Width="299" Click="button1_Click" />

10

</Grid>

Now we need to go to the code behind and start the process of establishing the Notification channel to get the events trigger. To do that first let we need to add the below two using statements.

Code Behind:

1

using Microsoft.Phone.Notification;

2

using System.Diagnostics;

Now we need to write the code to get the open channel details which will be used to send the Tile Notification, to do that we will use the output window to get the channel details. Copy the below code to the code behind page. Also we need to have a method which will do the channel setup step by step, first it will check if the channel is already available if its available we need to check the channel is null if null then we need to close the channel and open a new channel. But initially if the channel is not available then we can directly create the HttpNotificationChannel and do the process to create the channel as shown in the screen below.

Code Behind:

01

private void ChannelSetup()

02

{

03

HttpNotificationChannel pushtileChannel;

04

string strchannelName = "Channel0";

05

 

06

pushtileChannel = HttpNotificationChannel.Find(strchannelName);

07

if (pushtileChannel == null)

08

{

09

pushtileChannel = new HttpNotificationChannel(strchannelName);

10

 

11

pushtileChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);

12

pushtileChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);

     

13

 

14

pushtileChannel.Open();

15

pushtileChannel.BindToShellTile();

16

}

17

else

18

{

19

pushtileChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);

20

pushtileChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);

     

21

 

22

System.Diagnostics.Debug.WriteLine(pushtileChannel.ChannelUri.ToString());

       

23

Debug.WriteLine(String.Format("URI : {0}",

24

pushtileChannel.ChannelUri.ToString()));

25

}

26

}

27

 

28

void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)

29

{

30

 

31

Dispatcher.BeginInvoke(() =>

32

{

33

System.Diagnostics.Debug.WriteLine(e.ChannelUri.ToString());

34

 

35

Debug.WriteLine(String.Format("URI : {0}",

36

e.ChannelUri.ToString()));

37

});

38

}

39

 

40

void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)

41

{

42

Dispatcher.BeginInvoke(() =>

43

Debug.WriteLine(String.Format("Tile Notification {0} error occurred. {1} ({2}) {3}",

44

e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData))

45

);

46

}

Now we need to call the above method on the button click event as shown in the screen below.

Code Behind:

1

private void button1_Click(object sender, RoutedEventArgs e)

2

{

3

ChannelSetup();

4

}

Now we are done with the Windows phone 7 client notification application, we will check by building and executing the application and we can see the Windows Phone 7 Emulator as shown in the screen below.

clip_image004

Now click on the Establish Channel button which will establish the channel if not already created else will use existing channel and we can see the channel URI in the Output window since we have coded to get the channel details. To get the output window just go to the Visual Studio tool bar and select View –> Output window and we can see the output window as shown in the screen below.

clip_image006

Let us copy and keep the channel details on to a separate notepad, Now we need to create a Server to post the tile notifications to the application device to show the title.

Now we need to create an background image with 173 X 173 pixel as shown in the screen below and add it to the project solution. Note that we need to make the Build Action from Resource to Content as shown below.

clip_image008
We will create a web page from which we will post the tile notifications and get the notification on to the Windows Phone 7 device. To start with first create a ASP.NET Web application in C# as shown in the screen below.

clip_image010

Now add the below design code to the ASPX page so that we will get the same look and feel for this tutorial, here we have added 4 labels and 4 textboxes to get the user inputs (Channel URI, Front tile message, Background and Notification txt) and a button to trigger the event for the tile message to be sent to the Windows Phone 7 Device. Just copy and paste the below ASPX code

ASPX Code:

001

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"

002

CodeBehind="Default.aspx.cs" Inherits="F5debugWp7TileNotificationServer._Default" %>

003

 

004

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

005

<style type="text/css">

006

.style1

007

{

008

width: 100%;

009

}

010

.style2

011

{

012

}

013

.style3

014

{

015

width: 690px;

016

}

017

.style4

018

{

019

width: 143px;

020

}

021

.style5

022

{

023

width: 38px;

024

}

025

</style>

026

</asp:Content>

027

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

028

<table class="style1">

029

<tr>

030

<td colspan="3">

031

<asp:Label ID="Label1" runat="server" Font-Bold="true" Font-Size="Large" Text="F5Debug Windows Phone 7 Tile Notification"></asp:Label>

032

</td>

033

<td>

034

&nbsp;</td>

035

</tr>

036

<tr>

037

<td class="style5">

038

&nbsp;</td>

039

<td>

040

&nbsp;</td>

041

<td class="style3">

042

&nbsp;</td>

043

<td>

044

&nbsp;</td>

045

</tr>

046

<tr>

047

<td class="style5">

048

&nbsp;</td>

049

<td>

050

<asp:Label ID="Label2" runat="server" Text="Notification URI"></asp:Label>

051

</td>

052

<td>

053

<asp:TextBox ID="txtNotURI" runat="server" Width="661px"></asp:TextBox>

054

</td>

055

<td>

056

&nbsp;</td>

057

</tr>

058

<tr>

059

<td class="style5">

060

&nbsp;</td>

061

<td>

062

&nbsp;</td>

063

<td class="style3">

064

&nbsp;</td>

065

<td>

066

&nbsp;</td>

067

</tr>

068

<tr>

069

<td class="style5">

070

&nbsp;</td>

071

<td>

072

<asp:Label ID="Label5" runat="server" Text="Notification Front Tile"></asp:Label>

073

</td>

074

<td class="style3">

075

<asp:TextBox ID="txtNotFrontTile" runat="server" Width="661px"></asp:TextBox>

076

</td>

077

<td>

078

&nbsp;</td>

079

</tr>

080

<tr>

081

<td>

082

&nbsp;</td>

083

<td class="style4">

084

&nbsp;</td>

085

<td>

086

&nbsp;</td>

087

<td>

088

&nbsp;</td>

089

</tr>

090

<tr>

091

<td>

092

&nbsp;</td>

093

<td class="style4">

094

<asp:Label ID="Label3" runat="server" Text="Notification Background"></asp:Label>

095

</td>

096

<td>

097

<asp:TextBox ID="txtNotBck" runat="server" Width="661px"></asp:TextBox>

098

</td>

099

<td>

100

&nbsp;</td>

101

</tr>

102

<tr>

103

<td class="style5">

104

&nbsp;</td>

105

<td>

106

&nbsp;</td>

107

<td class="style3">

108

&nbsp;</td>

109

<td>

110

&nbsp;</td>

111

</tr>

112

<tr>

113

<td class="style5">

114

&nbsp;</td>

115

<td>

116

<asp:Label ID="Label4" runat="server" Text="Notification Count"></asp:Label>

117

</td>

118

<td>

119

<asp:TextBox ID="txtNotCount" runat="server" Width="659px"></asp:TextBox>

120

</td>

121

<td>

122

&nbsp;</td>

123

</tr>

124

<tr>

125

<td class="style5">

126

&nbsp;</td>

127

<td>

128

&nbsp;</td>

129

<td class="style3">

130

&nbsp;</td>

131

<td>

132

&nbsp;</td>

133

</tr>

134

<tr>

135

<td class="style5">

136

&nbsp;</td>

137

<td>

138

<asp:Button ID="btnSendNote" runat="server" Font-Bold="True"

139

onclick="Button1_Click" Text="Send Notification" Width="134px" />

140

</td>

141

<td>

142

<asp:Label ID="lblresult" runat="server"></asp:Label>

143

</td>

144

<td>

145

&nbsp;</td>

146

</tr>

147

</table>

148

</asp:Content>

clip_image012

Now go to the code behind and add the below code, this code will get the user inputs mainly the Channel URI, background and the tile information and pass the message to the Microsoft Push Notification services. Just copy the below code to proceed further.

Code Behind:

01

using System;

02

using System.Collections.Generic;

03

using System.Linq;

04

using System.Web;

05

using System.Web.UI;

06

using System.Web.UI.WebControls;

07

using System.Net;

08

using System.IO;

09

using System.Text;

10

 

11

namespace F5debugWp7TileNotificationServer

12

{

13

public partial class _Default : System.Web.UI.Page

14

{

15

protected void Page_Load(object sender, EventArgs e)

16

{

17

 

18

}

19

 

20

protected void Button1_Click(object sender, EventArgs e)

21

{

22

string PushNotificationXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<wp:Notification xmlns:wp=\"WPNotification\">" + "<wp:Tile>" +

23

"<wp:BackgroundImage>" + txtNotBck.Text + "</wp:BackgroundImage>" + "<wp:Count>" + txtNotCount.Text + "</wp:Count>" + "<wp:Title>" +

24

txtNotFrontTile.Text + "</wp:Title>" + "</wp:Tile> " + "</wp:Notification>";

25

 

26

string strChannelURI = txtNotURI.Text.ToString();

27

string strNotificationBackground = txtNotBck.Text.ToString();

28

string strNotifitcationCount = txtNotCount.Text.ToString();

29

string strNotifitcationFronttile = txtNotFrontTile.Text.ToString();

30

 

31

if (strChannelURI == string.Empty || strNotificationBackground == string.Empty || strNotifitcationCount == string.Empty || strNotifitcationFronttile == string.Empty)

32

{

33

lblresult.Text = "All the fields are Mandatory!!!";

34

return;

35

}

36

 

37

HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(strChannelURI);

38

sendNotificationRequest.Method = "POST";

39

 

40

byte[] notificationMessage = Encoding.Default.GetBytes(PushNotificationXML);

41

 

42

// Set the web request content length.

43

sendNotificationRequest.ContentLength = notificationMessage.Length;

44

sendNotificationRequest.ContentType = "text/xml";

45

sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "token");

46

sendNotificationRequest.Headers.Add("X-NotificationClass", "1");

47

 

48

using (Stream requestStream = sendNotificationRequest.GetRequestStream())

49

{

50

requestStream.Write(notificationMessage, 0, notificationMessage.Length);

51

}

52

 

53

// Send the notification and get the response.

54

HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();

55

string notificationStatus = response.Headers["X-NotificationStatus"];

56

string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];

57

string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];

58

lblresult.Text = "Status: " + notificationStatus + " | " + deviceConnectionStatus + " | " + notificationChannelStatus;

59

}

60

}

61

}

Now we are done with our server code, to test tile notification first run the Windows Phone 7 application (F5debugWp7TileNotification) and get the Channel URI. Once we got the URL click on the Back button and navigate to the application list. Point our application F5debugWp7TileNotification by pressing continuously using the mouse pointer and we can see the context menu as shown in the screen below.

clip_image014

Now select pin to start from the menu and we can see the application is pinned to the Tile as shown in the screen below.

clip_image016

Now run the project F5debugWp7TileNotificationServer web application and enter the values as shown in the screen below.

clip_image018

Now click on Send Notification button and we can see the result in the label at the bottom as shown in the screen below.

clip_image020

Now go to the Windows Phone 7 Emulator and we can see the Tile notification message which we send from the web application as shown in the screen below.

clip_image022

Conclusion:

So in this article we have seen how to use the Tile Notifications to send live updates to the Windows phone 7 devices to update the primary tiles, in our upcoming tutorials we will see how to do the secondary tiles and also see the raw notification process as well.

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Comments (1) -

Mahesh
Mahesh
8/13/2012 11:32:50 PM #

Hi,

I am getting the count in white background. So it is not showing the count instead a circle with white background. How to change the count background colour?

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter