Learn Windows Phone 7 Development in 31 Days – Day 12 – Working with Toast Notification in WP7

Introduction:

In this article we are going to see how to use the Toast Notification in windows phone 7. Windows Phone 7 application development supports push notification which provides developers a much more convenient option to transfer message Windows Phone 7 device from a web service when ever a new event occurs. Push notifications are of 3 types as follows

Toast Notification – This notification is launched at the top of the screen with the custom message such as an email alert or weather alert. The notification will be displayed for 10 second unless and until the user will dismiss the alert. If the user clicks on the alert then the application which sends the notification will be launched. [more]

Tile Notification – This notification is used to display dynamic representation of the application state, we can control the image, text and badge count of the notification.

Raw Notification – This notification is used only when the application is running on the foreground and if the application services are running background the notification will not be delivered and it gets discarded which is not delivered to the windows phone device.

We will see in depth on each notification and see a demo application for each notification one by one in our upcoming articles. Let us see the step by step process on how to create a TOAST application, we need to create a client application to send the notification to the device which we will be covering in this article.

Steps:

Open Visual Studio 2010 in administrator mode and create a new Silverlight for Windows Phone 7 application with a valid project name as shown in the screen below.

Now add a button to create the push channel which will be required to create a Toast notification, 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.

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.

C# Code:

1

usingMicrosoft.Phone.Notification;

2

usingSystem.Diagnostics;

Now we need to write the code to get the open channel details which will be used to send the Toast Notification, to do that we will use the output window to get the channel details. Copy the below code to the code behind page.

C# Code:

01

</pre>

02

privatevoidgetChannelURI(Uri value)

03

{

04

Dispatcher.BeginInvoke(() =>

05

{

06

Debug.WriteLine("URI: "+ value.ToString());

07

});

08

09

Debug.WriteLine("URI: "+ value.ToString());

10

}

11

<pre>

Now we need to handle the BindToShellToast of HttpNotificationChannel to bind the toast notifications to do that add the below code.

01

</pre>

02

privatestaticvoidShellBinding(HttpNotificationChannel httpChannel)

03

{

04

try

05

{

06

httpChannel.BindToShellToast();

07

}

08

catch(Exception)

09

{

10

//Catach if required.

11

}

12

}

13

<pre>

Now we need to add the below code to check if the application is running correctly and get the toast notification message in order to log it for administrating purpose.

Now 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.

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

C# Code:

1

privatevoidbutton1_Click(objectsender, 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.

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.

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

We will create a web page from which we will post the toast 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.

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 3 labels and 3 textboxes to get the user inputs (Channel URI and notification message) and a button to trigger the event for the toast message to be sent to the Windows Phone 7 Device .

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

Now run the Windows Phone 7 Toast notification application to get the channel URI, once we got the channel URI keep the application running in background and navigate to the home page of the Windows Phone 7 Application. Now run the Server application (F5debugWp7ToastNotificationServer) and enter the details as shown in the screen below.

Now click on the Send Notification button and navigate to the Windows Phone 7 Emulator to see the Toast Notification Message on the top as shown in the screen below.

Conclusion:

So in this article we have seen what Push Notification is in Windows Phone 7 and the types of notification available. Also we have seen how to create a Toast Notification step by step in detail.

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.

No Comments

I found your tutorial through Pinterest, using the above link. I LOVE your case! I just bought fat quarters to do this project (and a few others from the above link!) I cannot wait to begin making a few of these for my family 🙂

Site Counter

Awards

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.