WP push message notes

Source: Internet
Author: User

Recently I want to add a push message to the application, mainly the toast message. So I want to know about the WP message push mechanism and implementation method. I checked a lot of information during the process, I also encountered some problems. After that, I made a small note and concluded that good memory is not as good as bad writing. I can look back later. It is much more convenient and I will not talk much about it!

 

WP message push mechanism

I will use a picture to illustrate that, because the official website is in English, I have added some Chinese characters on it for your convenience!

 

The core content of this figure is that when your app requests a mobile phone uri (Note: Here URI is equivalent to an IP address or a physical address, it is a unique identifier ), the mobile client service will contact the Microsoft push center, then return a URI to the client PUSH Service, and then the client service will send this URI to your application, your application sends the URI back to your server, and then your server sends the URI and the message to be sent to the Microsoft push center, finally, the Microsoft push center pushes messages to the specified Mobile Phone Based on the URI!

Toast message push (WP end)

I don't want to explain this much. I directly push the code. The following code mainly checks whether the toast notification channel has been set in early application instances. If the notification channel is found, the notification channel is connected to the notification event. If no notification channel is found, create a notification channel and connect it to the notification event

// Constructor public mainpage () {httpicationicationchannel mychannel = NULL; // The Name Of The push channel, which can be string channelname = "toastchannel"; initializecomponent (); // The find static method can find the channel mychannel = httpicationicationchannel by name. find (channelname); // because the system sometimes deletes the channel, you must determine whether it exists. If it cannot be found, you must create an IF (mychannel = NULL) {mychannel = new httpicationicationchannel (channelname); // register the event mychannel. channeluriupdated + = new eventhandler <icationicationchannelurieventargs> (mychannel_channeluriupdated); mychannel. erroccurred + = new eventhandler <icationicationchannelerroreventargs> (mychannel_erroccurred); mychannel. shelltoastnotificationreceived + = new eventhandler <icationeventeventargs> (mychannel_shelltoastnotificationreceived); // open the channel mychannel. open (); // bind the toast notification so that the notification message mychannel at the top of the screen is displayed only when the program is not in the foreground. bindtoshelltoast ();} else {// If an event exists, an event is also registered because after the program is thrown to the background, the event bound to mychannel may be deleted. channeluriupdated + = new eventhandler <icationicationchannelurieventargs> (mychannel_channeluriupdated); mychannel. erroccurred + = new eventhandler <icationicationchannelerroreventargs> (mychannel_erroccurred); mychannel. shelltoastnotificationreceived + = new eventhandler <icationeventeventargs> (mychannel_shelltoastnotificationreceived); // output the URL in the "output" window because we only test the URL, which makes it easier for system. diagnostics. debug. writeline ("channel URI: {0}", mychannel. channeluri. tostring () ;}} void mychannel_shelltoastnotificationreceived (Object sender, notificationeventargs e) {// here, when the user is using your application, and you just pushed the message, run this method string MSG = ""; foreach (string key in E. collection. keys) {MSG + = Key + ":" + E. collection [Key] + "\ r \ n";} dispatcher. begininvoke () => {this.txt info. TEXT = MSG;});} void mychannel_erroccurred (Object sender, icationicationchannelerroreventargs e) {dispatcher. begininvoke () => MessageBox. show (E. message); // when an error occurs, execute the content here. You can handle it as needed} void mychannel_channeluriupdated (Object sender, icationicationchannelurieventargs E) {// when the URL changes, output it again. // ensure that we get the latest version of Uri dispatcher. begininvoke () => {system. diagnostics. debug. writeline ("channel URI: {0}", E. channeluri. tostring ());});}
Toast message push (server)

Here I create an ASP. net web page, which uses the URI returned when the push channel is created on the device to send toast notifications. Of course, you can use any application that can send network messages, such as winform jsp php.

The code for pushing messages is as follows:

Private void sendmsg () {// txturl. tex this parameter is the URI of the mobile phone. To push messages, you must first determine the URI of your mobile phone httpwebrequest myrequest = (httpwebrequest) webrequest. create (txturl. text); myrequest. contenttype = "text/XML"; myrequest. headers. add ("X-windowsphone-target", "toast "); /** X-icationicationclass processing interval * 2-Send immediately * Send within 12-450 seconds * Send within 22-900 seconds */myrequest. headers. add ("X-icationicationclass", "2"); // txtvalue1.text: title, txtvalue 2. Text: Push content. txtparam. Text: the navigation page when a user clicks a message, such as mainpage. XAML string toastmessage = "<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> "+" <WP: Notification xmlns: Wp = \ "wpnotification \"> "+" <WP: Toast> "+" <WP: text1> "+ txtvalue1.text +" </WP: text1> "+" <WP: text2> "+ txtvalue2.text +" </WP: text2> "+" <WP: param> "+ txtparam. text + "</WP: param>" + "</WP: Toast>" + "</WP: Notification>"; byte [] buffer = encoding. utf8.getbytes (toastmessage); // note that the official website is encoding. default. getbytes (toastmessage), I always report errors during use. The following describes the cause of myrequest. contentlength = buffer. length; myrequest. method = "Post"; using (Stream stream = myrequest. getrequeststream () {stream. write (buffer, 0, buffer. length);} // send the message and obtain the response httpwebresponse response = (httpwebresponse) myrequest. getresponse (); string icationicationstatus = response. headers ["X-icationicationstatus"]; string icationicationchannelstatus = response. headers ["X-subscriptionstatus"]; string deviceconnectionstatus = response. headers ["X-deviceconnectionstatus"]; // determines whether the push is successful to the Microsoft PUSH Service Center. // textboxresponse. TEXT = icationicationstatus + "|" + deviceconnectionstatus + "|" + notifchannelstatus; if (icationicationstatus = "received" & deviceconnectionstatus = "connected" & notificationchannelstatus = "active") {success ++ ;}}
Summary and attention

(1) <WP: param> "+ txtparam. Text +" </WP: param>. If the param content is empty, skip this node. At the same time, some characters in XML need to be converted:

Both <text1> and <text2> use string format.

<Param> values can be in the following format:

  • /Page1.xaml-defines the page in the application that you navigate to when the application starts. The page must start.

  • /Page1.xaml? Value1 = 1234 & amp; value2 = 9876-defines the page to which the application is started and the name/value pair of the information. The page must start.

  • ? Value1 = 1234 & amp; value2 = 9876-contains the information name/value pair passed to the default start page of the application. The page must start with "?" .

Character

XML Encoding

<

& Lt;

>

& Gt;

&

& Amp;

'

& Apos;

"

& Quot;

 

(2) When the toast notification sent by the server contains XML format errors or illegal content, Microsoft push closes this channel and prompts an error. I always report an error at runtime ("the XML payload contains invalid or improperly formatted XML or the notification type specified in the header does not match the payload type used. the channel has been closed. check your xml payload for errors and reopen the channel to obtain a new Uri. "). After checking the information, I changed encoding. Default. getbytes (toastmessage) to encoding. utf8.getbytes (toastmessage). In this way, no error is reported. This may be a problem with the official sample code. (3) It is difficult for our server to count the success or failure of pushing. Because the corresponding code returned from the Microsoft push service simply reflects the arrival of the push notification to the proxy server, rather than the result when the notification arrives at the client. For example, if the proxy server of Microsoft returns the response code 200, the server simply knows that the information has successfully arrived at it and meets the conditions for forwarding to the client program; if the client finally receives the notification, you cannot find it here. Of course, when the client program receives the notification, it can report it to the server, but this is not accurate, because some information may be ignored because the program is not running at the front end, here, I directly use the three information headers X-icationicationstatus X-subscriptionstatus X-deviceconnectionstatus to determine whether the information is successfully sent to the Microsoft push center to determine whether the information is successfully sent. There are other more accurate solutions, if you know, please let me know!

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.