Windows Phone 7 Development talk on the 31st day-19th: push notification

Source: Internet
Author: User

This article is"Discussion on Windows Phone 7 development on the 31st day"19th day of the series.

Yesterday, I introduced the webbrowser control and howProgram. Today, let's talk about the most important topic in this series: push notifications.

Maybe you are not very familiar with the concept of push notification. In fact, it is very simple: you do not have to force your application to check the server every few minutes, and the server can notify your mobile phone when there is new data.

Why use push notifications?

The first reason is that battery power is reduced. The detection server data consumes a lot of power, while the battery in mobile phones is definitely a scarce resource. You will never have enough power, and no matter how long your battery can last, try to avoid notifications that will shorten the battery life.

Second, you can push notifications to inform your users of some interesting things in the program, even if the program is not running. You can also remind users to open the program to see what you want to tell them.

Push notification process

To help you understand what I threw belowCodeI think it is necessary to explain to you exactly what happened in this process.

    1. When a user runs a program for the first time on a mobile phone, the application calls Microsoft's push notification service and requests a custom URI for communication.
    2. When an event is triggered in your web service, you should pass the information to that Uri (with a specific load), and then push the notification service to update it with active tiles, toast notifications or real data in programs send data to users' mobile phones.

This article explains how to do the above. If you want to see an example that can be built step by step, see the Windows Phone developer training package. There is an excellent tutorial on pushing notifications.

Obtain the custom URI from the push notification service.

Thank you very much. Microsoft has made this part very simple. We have to use the Microsoft. Phone. Notification assembly, but I still have to use 10 lines of code to get a custom URI from the push notification service (PNS. First, I have to create an httpicationicationchannel. It will automatically communicate with PNS (in another thread) and capture the content returned by the service through an event.

Code Httpicationicationchannel channel;

Void Getpushchannel ()
{
Channel =   New Httpicationicationchannel ( " Blankensoft _ "   + Datetime. Now. ticks. tostring ());
Channel. channeluriupdated + =   New Eventhandler < Icationicationchannelurieventargs > (Channel_channeluriupdated );
Channel. open ();
}

VoidChannel_channeluriupdated (ObjectSender, icationicationchannelurieventargs E)
{
Dispatcher. begininvoke (Delegate
{
Uriblock. Text=Channel. channeluri. tostring ();
});
}

In this example, the URI is as follows:

Http://sn1.notify.live.net/throttledthirdparty/01.00/AAHsLicyiJgtTaiDbJoSgmFiAgAAAAADAwAAAAQUZm52OjlzOEQ2NDJDRkl5MEVFMEQ

Once you have a URI, you can save it in web service. The Web Service initializes the information that will be sent to your mobile phone. We have three methods to implement this: Tile notifications, toast notifications, and Native notifications.

Different requirements and messages

As I mentioned earlier, you can send three different types of messages to your mobile phone. The following is an overview:

Native notification )-Native notifications are used for programs that are actually running in the device. It allows you to update the user interface in real time during use.

Toast notification)-This message is received no matter whether the program is running or not, but it may be a bit annoying to bring up the toast message when the program is running. I will demonstrate it in the example below. The toast notification cannot update your program data. To do this, you still need to pass a native notification.

Tile notification)-If your program is pinned to the start interface, you can use the tile notification to update the tile. You can change the background image and an integer between 0 and 99.

Send a toast notification

Once we get the push Uri, the rest is to combine the HTTP message and then send the message to this URI. The following is a sample code:

Code Httpwebrequest sendnotificationrequest = (Httpwebrequest) webrequest. Create (Channel. channeluri. tostring ());
Sendnotificationrequest. Method =   " Post " ;
// Indicate that you'll send toast notifications!
Sendnotificationrequest. contenttype =   " Text/XML " ;
Sendnotificationrequest. Headers =   New Webheadercollection ();
Sendnotificationrequest. headers. Add ( " X-icationicationclass " , " 2 " );
If ( String . Isnullorempty (txtmessage. Text )) Return ;

// Create XML envelope
String Data =   " X-windowsphone-target: Toast \ r \ n "   +
" <? XML version = '1. 0' encoding = 'utf-8'?> "   +
" <WP: Notification xmlns: Wp = 'wpnotification'> "   +
" <WP: Toast> "   +
" <WP: text1> {0} </WP: text1> "   +
" </WP: Toast> "   +
" </WP: Notification> " ;

// wrap custom data into envelope
string message = string . format (data, txtmessage. text);
byte [] icationicationmessage = encoding. default. getbytes (Message);

// set content length
sendnotificationrequest. contentlength = icationicationmessage. length;

// push data to stream
using (Stream requeststream = sendicationicationrequest. getrequeststream ()
{< br> requeststream. write (icationicationmessage, 0 , icationicationmessage. length);
}

// Get reponse for message sending
Httpwebresponse response = (Httpwebresponse) sendnotificationrequest. getresponse ();
String Icationicationstatus = Response. headers [ " X-icationicationstatus " ];
String Icationicationchannelstatus = Response. headers [ " X-subscriptionstatus " ];
String Deviceconnectionstatus = Response. headers [ " X-deviceconnectionstatus " ];

As you can see, this part of the code is long and complex. I suggest you spend more time on the Windows Phone developer training package and follow the example of push notifications.

This is an excellent example of how push notifications (from beginning to end) work. It also shows you how these updates keep your applications powerful in front of your users.

Download Sample Code

For today's example, the above Code does not help you because it lacks the required environment. Today's download is actually the final version of the push notification example in the Windows Phone developer training package.

Address: http://www.jeffblankenburg.com/post/31-Days-of-Windows-Phone-7c-Day-19-Push-Notifications.aspx

If you like myArticle, Please click "recommended ", Thank you!
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.