Windows phone7 push notification service

Source: Internet
Author: User

Tell Mr Lin Yongjian that WP7

Code download

Overview:

    • Why use PUSH Service
    • Principles of the push notification service
    • Usage Specification
    • Message Type (raw notification, toast notification, tile notification)
    • Timed tile update

First, why is push service used? The execution model of Windows Phone determines that only one third-party application can run on the frontend. Therefore, third-party applications cannot continuously pull data to the cloud in the background, microsoft provides the push notification service to send update notifications to third-party applications. The server initiates communications to reduce battery consumption,

So what is the process and principle of the push notification service:

    • The WP device registers the PN service with the mpns and obtains a unique Uri,
    • The WP device bar service URI is passed to the cloud service and registered
    • When an update message occurs, cloud sends a notification to mpns.
    • Mpns sends an update notification to the WP device.

Usage specification:

The wp7.0 version only supports up to 15 push notification services. Therefore, it is best to ask the user whether to use the push notification service and provide the user with the option of canceling the subscription.

Message Type:

    • Raw notification: data in any format can be sent to applications.ProgramYou can process data as needed and receive messages only when the program is running.
    • Toast notification: The sent data is specified as XML. If the program is running, the message is sent to the application. If the program is not running, a message is displayed in the toast message box,
    • Tile notification: The sent data is specified as XML and will not receive messages when the program is running. Only when the program is pin to start will the updated data be sent to the tile of Start Screen, which contains three attributes: the background, title, and number of attributes have a fixed format and position. You can use these attributes, but not necessarily use them together,

XAML. CSCode:

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; using system. diagnostics; using system. io; Using System. Text; namespace pushnotifications {public partial class mainpage: phoneapplicationpage {httpicationicationchannel httpchanel; string channelname = "channel1"; // constructor public mainpage () {initializecomponent ();} private void button#click (Object sender, routedeventargs e) {httpchanel = httpicationicationchannel. find (channelname); If (httpchanel! = NULL) {httpchanel. close (); httpchanel. dispose ();} httpchanel = new httpnotificationchannel (channelname, "icationicationservice"); httpchanel. channeluriupdated + = new eventhandler <icationchannelurieventargs> (httpchanel_channeluriupdated); httpchanel. erroccurred + = new eventhandler <icationicationchannelerroreventargs> (httpchanel_erroccurred); httpchanel. httpicationreceivreceived + = new eventhandler 

XAML code:

<Phone: phoneapplicationpage X: class = "pushnotifications. mainpage "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "xmlns: Phone =" CLR-namespace: Microsoft. phone. controls; Assembly = Microsoft. phone "xmlns: shell =" CLR-namespace: Microsoft. phone. shell; Assembly = Microsoft. phone "xmlns: D =" http://schemas.microsoft.com/expression/blend/2008 "x Mlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006" MC: ignorable = "D" D: designwidth = "480" D: designheight = "768" fontfamily = "{staticresource quota}" fontsize = "{staticresource quota}" foreground = "{staticresource quota}" supportedorientations = "portrait" orientation = "portrait" Shell: systemtray. isvisible = "true"> <! -- Layoutroot is the root grid where all page content is placed --> <grid X: Name = "layoutroot" background = "Transparent"> <grid. rowdefinitions> <rowdefinition Height = "Auto"/> <rowdefinition Height = "*"/> </grid. rowdefinitions> <! -- Titlepanel contains the name of the application and page title --> <stackpanel X: Name = "titlepanel" grid. row = "0" margin = ","> <textblock X: name = "applicationtitle" text = "" style = "{staticresource phonetextnormalstyle}"/> <textblock X: name = "pagetitle" text = "push notification service" margin = "9,-7,0, 0" style = "{staticresource phonetexttitle1style}"/> </stackpanel> <! -- Contentpanel-place additional content here --> <grid X: Name = "contentpanel" grid. row = "1" margin = ","> <button content = "connection" Height = "72" horizontalalignment = "Left" margin =, 160 "name =" button1 "verticalignment =" TOP "width =" 124 "Click =" button#click "/> <textblock Height =" 44,160 "horizontalalignment =" Left "margin = ", 0, 0 "name =" textblock1 "text =" textblock "verticalignment =" TOP "Width = "269"/> </GRID> <! -- Sample Code showing usage of ApplicationBar --> <! -- <Phone: phoneapplicationpage. applicationBar> <shell: ApplicationBar isvisible = "true" ismenuenabled = "true"> <shell: applicationbariconbutton iconuri = "/images/appbar_button1.png" text = "button 1"/> <shell: applicationbariconbutton iconuri = "/images/appbar_button2.png" text = "button 2"/> <shell: ApplicationBar. menuitems> <shell: applicationbarmenuitem text = "menuitem 1"/> <shell: applicationbarmenuitem text = "menuitem 2"/> </shell: ApplicationBar. menuitems> </shell: ApplicationBar> </Phone: phoneapplicationpage. applicationBar> --> </Phone: phoneapplicationpage>

cloud program processing code:

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using system. net; using system. io; namespace cloudserver {public partial class form1: FORM {public form1 () {initializecomponent ();} private void sendrawmsg (byte [] bytes) {httpwebrequest sendrequest = (httpwebrequest) webreq Uest. create (textbox1.text); sendrequest. method = webrequestmethods. HTTP. post; sendrequest. headers ["X-messageid"] = guid. newguid (). tostring (); sendrequest. contenttype = "text/XML; charset = UTF-8"; sendrequest. headers. add ("X-icationicationclass", "3"); sendrequest. contentlength = bytes. length; byte [] icationicationmsg = bytes; using (Stream requeststream = sendrequest. getrequeststream () {requeststream. W Rite (icationicationmsg, 0, notificationmsg. length);} httpwebresponse response = (httpwebresponse) sendrequest. getresponse (); string msgstatus = response. headers ["X-icationicationstatus"]; string channelstatus = response. headers ["X-subscriptionstatus"]; string devicestatus = response. headers ["X-deviceconnectionstatus"]; label6.text = string. format ("message status: {0}, MPs queue status: {1}, device connection status: {2}", msgstatus, channelstatu S, devicestatus);} private void button#click (Object sender, eventargs e) {string MSG = string. format ("Message Type: {0} {1} {2}, {3} degree", combobox1.text, combobox2.text, combobox3.text, textbox5.text); string type = combobox1.text. toupper (); MessageBox. show (type); If (type = "Raw") {byte [] strbytes = new utf8encoding (). getbytes (MSG); sendrawmsg (strbytes);} If (type = "toast") {string toastmessage = "<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> "+" <WP: Notification xmlns: Wp = \ "wpnotification \"> "+" <WP: Toast> "+" <WP: text1> weather update </WP: text1> "+" <WP: text2> "+ MSG +" </WP: text2> "+" </WP: Toast> "+" </WP: notification> "; byte [] strbytes = new utf8encoding (). getbytes (toastmessage); sendtoastmsg (strbytes);} If (type = "tile") {string tilemessage = "<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> "+" <WP: Notification xmlns: Wp = \ "wpnotification \"> "+" <WP: Tile> "+" <WP: backgroundimage> red.jpg </WP: backgroundimage> "+" <WP: title> "+ MSG +" </WP: title> "+" </WP: Tile> "+" </WP: notification> "; byte [] strbytes = new utf8encoding (). getbytes (tilemessage); sendtilemsg (strbytes) ;}} private void sendtilemsg (byte [] tile) {try {// get the URI that the Microsoft push notification service returns to the push client when creating a notification channel. // normally, a Web Service wowould listen for Uris coming from the Web Client and maintain a list of URIs to send // notifications out. string subscriptionuri = textbox1.text; httpwebrequest sendnotificationrequest = (httpwebrequest) webrequest. create (subscriptionuri); // create an httpwebrequest that posts the toast notification to the Microsoft push notification service. // http post is the only method allowed to send the notification. sendnotificationrequest. method = "Post"; // The optional custom header X-messageid uniquely identifies a notification message. // if it is present, the same value is returned in the notification response. it must be a string that contains a uuid. sendnotificationrequest. headers ["X-messageid"] = guid. newguid (). tostring (); // set the notification payload to send. byte [] icationicationmessage = tile; // set the Web request content length. sendnotificationrequest. contentlength = icationicationmessage. length; sendnotificationrequest. contenttype = "text/XML"; sendnotificationrequest. headers. add ("X-windowsphone-target", "token"); sendicationicationrequest. headers. add ("X-icationicationclass", "1"); Using (Stream requeststream = sendnotificationicationrequest. getrequeststream () {requeststream. write (icationicationmessage, 0, notificationmessage. length);} // send the notification and get the response. httpwebresponse response = (httpwebresponse) sendnotificationrequest. getresponse (); string icationicationstatus = response. headers ["X-icationicationstatus"]; string icationicationchannelstatus = response. headers ["X-subscriptionstatus"]; string deviceconnectionstatus = response. headers ["X-deviceconnectionstatus"]; // display the response from the Microsoft push notification service. // normally, error handling code wocould be here. in the real world, because data connections are not always available, // configurations may need to be throttled back if the device cannot be reached. label6.text = icationicationstatus + "|" + deviceconnectionstatus + "|" + notifchannelstatus;} catch (exception ex) {label6.text = "exception caught sending update:" + ex. tostring () ;}} private void sendtoastmsg (byte [] toastmsg) {try {// get the URI that the Microsoft push notification service returns to the push client when creating a notification channel. // normally, a Web Service wowould listen for Uris coming from the Web Client and maintain a list of URIs to send // notifications out. string subscriptionuri = textbox1.text; httpwebrequest sendnotificationrequest = (httpwebrequest) webrequest. create (subscriptionuri); // create an httpwebrequest that posts the toast notification to the Microsoft push notification service. // http post is the only method allowed to send the notification. sendnotificationrequest. method = "Post"; // The optional custom header X-messageid uniquely identifies a notification message. // if it is present, the same value is returned in the notification response. it must be a string that contains a uuid. sendnotificationrequest. headers ["X-messageid"] = guid. newguid (). tostring (); sendnotificationrequest. contenttype = "text/XML"; sendnotificationrequest. headers. add ("X-windowsphone-target", "toast"); sendicationicationrequest. headers. add ("X-icationicationclass", "2"); // set the Web request content length. sendnotificationrequest. contentlength = toastmsg. length; // set the notification payload to send. byte [] icationicationmessage = toastmsg; using (Stream requeststream = sendnotificationrequest. getrequeststream () {requeststream. write (icationicationmessage, 0, notificationmessage. length);} // send the notification and get the response. httpwebresponse response = (httpwebresponse) sendnotificationrequest. getresponse (); string icationicationstatus = response. headers ["X-icationicationstatus"]; string icationicationchannelstatus = response. headers ["X-subscriptionstatus"]; string deviceconnectionstatus = response. headers ["X-deviceconnectionstatus"]; // display the response from the Microsoft push notification service. // normally, error handling code wocould be here. in the real world, because data connections are not always available, // configurations may need to be throttled back if the device cannot be reached. label6.text = icationicationstatus + "|" + deviceconnectionstatus + "|" + notifchannelstatus;} catch (exception ex) {label6.text = "exception caught sending update:" + ex. tostring ();}}}}

:

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.