Introduction:
In this article we are going to see how to use the Raw Notifications to sent messages. In our earlier article we have seen how to use the Toast and Tile notifications to send receive message as per the requirement and nature of the usage. Raw Notifications are not like the TOAST and TILE notification, these notifications are generated by the application itself or from a web service. The application needs to be up and running in order to receive these notifications. If the application is not running then the message will be discarded and we will be getting error on the notification
Let us jump start to see the step by step process on how to use Raw Notification Mechanism to do notifications, unlike Tile we will not have a much good looking image to show the notification rather we will try to catch the notification to the message box. [more]
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.
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 Raw 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:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 we need to add the below two using statements.
Code Behind:
|
|
|
|
Now let us add the event handlers for sending the URI to the web service, to handle notification errors and for receiving the raw notification which is application specific as shown in the below 3 event handler codes.
Code Behind:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Now we need to write the code to get the open channel details which will be used to send the Raw 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:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Now we need to call the above method on the button click event as shown in the screen below.
Code Behind:
|
|
|
|
|
|
|
|
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.
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 Raw Notification message. We will create a web page from which we will post the Raw 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, Title and Sub Title) 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:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 Raw Message information and pass the message to the Microsoft Push Notification services. Just copy the below code to proceed further.
C# Code:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Now we are done with our server code, to test raw notification first run the Windows Phone 7 application (F5debugWp7RawNotification) and get the Channel URI or make use of the URI which we saved in our notepad. Note that the application should be in running mode in order to get the notification message. Now run the project F5debugWp7RawNotificationServer web application and enter the values as shown in the screen below.
Now click on Send Notification button and we can see the result in the label at the bottom as shown in the screen below.
Now go to the Windows Phone 7 Emulator and we can see the Raw Notification Message which we sent from the web application in the Message Box as shown in the screen below.
Conclusion:
So in this article we have seen how to use the RAW Notification Mechanism to send and receive the notifications while the application is running on the foreground.