Windows Phone development (45): push notification finale-raw notification

Source: Internet
Author: User

Why is it an ending? Because there are only three types of push Notification Services, the first two are involved, and there is one left-raw notification.

We have passed two hands-on experiments. I believe everyone knows that pushing notifications is not complicated. Why? You can see that no matter which method, the usage is basically the same, if you do not want to writeCodeYou can copy the Code a few times to complete the process. The implementation code of the Three push notifications is the same, but only the content sent is different.

Raw push notification is simpler than the previous two because it does not have a standard format, as long as you post a byte stream array to the specified Uri, it is OK, that is, everything that can be changed to byte [] can be sent. However, you should not send too much data. It is generally used to send some brief text information. Don't think about sending files !!

 

Severe reminder: to receive raw notifications, your WP ApplicationProgramThe push notification must be run on the frontend, otherwise it cannot be received, which is different from the toast notification. If your program is not running on the frontend, the push notification will be dropped by XX.

 

Okay. If you don't want to talk about F, start practicing it.

First, let's use WPF to send notifications to the server. I will cut down a tu on the interface.

 

This is the benefit of using WPF. You may not be able to see which controls are used in the window and what attributes are set. However, if I paste the XAML, I will understand it.

<Window X: class = "rawntfserver. mainwindow "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "Title =" raw Notification Server Side "Height =" 350 "width =" 525 "> <grid. rowdefinitions> <rowdefinition Height = "Auto"/> <rowdefinition/> </grid. rowdefinitions> <grid. row = "0"> <grid. columndefinitions> <columndefinition width = "Auto"/> <columndefinition width = "*"/> <columndefinition width = "Auto"/> </grid. columndefinitions> <textblock grid. column = "0" text = "target URI:" verticalignment = "center"/> <textbox name = "txturi" grid. column = "1" margin = "2" background = "# ffd8e4e4"/> <button grid. column = "2" padding = "8, 3, 8, 3" margin = "7, 2, 3, 2" content = "send" Click = "onsend"/> </GRID> <groupbox grid. row = "1" header = "sent content"> <textbox verticalscrollbarvisibility = "Auto" textwrapping = "Wrap" name = "txtmsg" background = "# ffecf4d7"/> </groupbox> <groupbox grid. row = "2" header = "response content"> <textbox name = "txtresp" verticalscrollbarvisibility = "Auto" textwrapping = "Wrap" background = "# ffc9edfa"/> </groupbox> </GRID> </WINDOW>

well, the front desk is ready. Let's get started with the background.

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. windows; using system. windows. controls; using system. windows. data; using system. windows. documents; using system. windows. input; using system. windows. media; using system. windows. media. imaging; using system. windows. navigation; using system. windows. shapes; using system. net; using system. io; using system. net. mime; namespa Ce rawntfserver {// <summary> // mainwindow. interaction logic of XAML // </Summary> Public partial class mainwindow: window {public mainwindow () {initializecomponent ();} private void onsend (Object sender, routedeventargs E) {If (txturi. TEXT = "" | txtmsg. TEXT = "") {MessageBox. show ("Enter the required parameters. "); Return;} httpwebrequest request = (httpwebrequest) webrequest. create (txturi. text); Request. method = webrequestmethods. HTTP. post; request. contenttype = mediatypenames. text. plain; // HTTP header: // X-icationicationclass: 3 // 3: Send Now // send the request after seconds // 23: 900 seconds. headers. add ("X-icationicationclass", "3"); byte [] buffer = encoding. utf8.getbytes (txtmsg. text); Request. contentlength = buffer. length; using (Stream S = request. getrequeststream () {S. write (buffer, 0, buffer. length);} // receives the response httpwebresponse response = (httpwebresponse) request. getresponse (); string HDS = ""; foreach (string key in response. headers. allkeys) {HDS + = Key + ":" + response. headers. get (key) + "\ r \ n";} txtresp. TEXT = HDS ;}}}

 

Do you think the code is familiar? Is it like the example in the first two sections?

 

Okay, done on the server. The next step is the WP client.

The layout does not need to be tu, so you can rest assured that there is no picture of the truth. On XAML.

 
<! -- Contentpanel-place other content here --> <grid X: Name = "contentpanel" grid. row = "1" margin = "12, 0, 12, 0"> <ListBox name = "lbmsg"/> </GRID>

Let's just put it in a control-ListBox. The notifications we will receive later will be thrown into it.

OK. Check the C # code in the background.

Using system; using system. collections. generic; using system. LINQ; using system. net; using system. windows; using system. windows. controls; using system. windows. documents; using system. windows. input; using system. windows. media; using system. windows. media. animation; using system. windows. shapes; using Microsoft. phone. controls; using Microsoft. phone. notification; namespace wpclient {public partial class mainpage: phoneapplicationpage {// constructor public mainpage () {initializecomponent (); httpicationicationchannel channel = NULL; string channelname = "Raw "; channel = httpicationicationchannel. find (channelname); If (Channel = NULL) {channel = new httpicationicationchannel (channelname); channel. channeluriupdated + = new eventhandler <icationchannelurieventargs> (channel_channeluriupdated); channel. erroccurred + = new eventhandler <icationicationchannelerroreventargs> (channel_erroccurred); channel. httpicationreceivreceived + = new eventhandler 

Avoid a friend saying that the Code cannot be understood. I will paste it again after Ctrl +.

 

Run the following command. First, run the WP terminal. You can run it at the same time ., But at least let the WP simulator or mobile phone receive the URI allocated by the ECS.

 

Copy the URI and fill it in the server window. Then, enter the message you want to send and click send ".

 

Well, that's how it works. Shouldn't it be complicated?

In the process of sending and receiving messages, it is recommended to use UTF-8 for encoding, it seems that there is no garbled.

 

Ha, the cow is blowing here. In the next section, we will play socket, a horrible thing.

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.