Brief introduction:
The Windows phone platform supports three types of push notifications:
1.tile--is also the Start Screen program tile icon
2.toast--Create a Toast pop-up window that appears in the current screen
3.raw--has the notification that the application handles itself: it is transparent to the user.
The three push processes are the same, involving three parties: the Windows phone app, the cloud-based notification service (Notification service, provided by Microsoft), and the notification source.
The process for pushing notifications is as follows:
The order is as follows: 1. Message push is sent by the app with message push function
2, to the Microsoft Push Notification Service (MPNS) request URI, and return the push notification URI (this URI is based on the device, mobile application, channel name combination of a unique identity)
3. Application gets URI
4, will get the URI tells the notification cloud service (that is, the notification source)
5. If the cloud service (notification source) has a notification to be passed to the Windows Phone app, the URI wants to MPNs send a POST request, which also contains the content of the push message
6, followed by MPNs to the corresponding URI of the device to send push notifications
Toast Push Notifications
Toast Push Notifications Display the following styles:
It is composed of three parts: 1, Title. That is, the "title" section in the display. The icon next to your app. In XML (the message sent by the notification source is encoded after the XML content is passed), the string is defined as the Text1 property.
2, Content. The "Content" section in the figure. In XML, defined as the Text2 property
3, Parament. Refers to the parameter, after clicking on the toast, will pass this parameter to your application. You can indicate which pages you want to launch in your app, and you can also have a name-value pair that you can use in your app. defined in XML as the Param property.
Code
Mobile App-side
1 ///Push Notification channel2 Httpnotificationchannel Pushchannel;3 4 //the name of the channel, which can be self-named5 stringChannelName ="Toastsamplechannel";6 7 //to find a channel of a name, if there is a channel that returns a Httpnotificationchannel type, no several null are found8Pushchannel =Httpnotificationchannel.find (channelname);9 Ten //If you don't find it, create a channel with this name . One if(Pushchannel = =NULL) A { -Pushchannel =NewHttpnotificationchannel (channelname); - the //The event is triggered when the new URI is updated, and this event must be registered -pushchannel.channeluriupdated + =pushchannel_channeluriupdated; - //in order to improve the fault tolerance of the system, the error can be captured -Pushchannel.erroroccurred + =pushchannel_erroroccurred; + - //This event registration is optional and when the push notification comes, the program is running, and the event is raised, where you can handle the incoming message at will +Pushchannel.shelltoastnotificationreceived + =pushchannel_shelltoastnotificationreceived; A //Open channel at Pushchannel.open (); - - //bind the channel to a toast push notification, otherwise - pushchannel.bindtoshelltoast (); - - } in Else//channel already exists, you do not need to continue to create a new channel, the direct registration time can - { topushchannel.channeluriupdated + =pushchannel_channeluriupdated; +Pushchannel.erroroccurred + =pushchannel_erroroccurred; - thePushchannel.shelltoastnotificationreceived + =pushchannel_shelltoastnotificationreceived; * $ //Show the URI of the channel when testingPanax Notoginseng System.Diagnostics.Debug.WriteLine (pushChannel.ChannelUri.ToString ()); - the } + } A the voidPushchannel_channeluriupdated (Objectsender, Notificationchannelurieventargs e) + { - $Dispatcher.begininvoke (() = $ { - //Show the URI of the channel when testing - System.Diagnostics.Debug.WriteLine (e.channeluri.tostring ()); theMessageBox.Show (String.Format ("Channel Uri is {0}", - e.channeluri.tostring ()));Wuyi the }); - } Wu voidPushchannel_erroroccurred (Objectsender, Notificationchannelerroreventargs e) - { About //error Handling in push notifications, here is a simple display $Dispatcher.begininvoke (() = -MessageBox.Show (String.Format ("A push notification {0} error occurred. {1} ({2}) {3}", - E.errortype, E.message, E.errorcode, e.erroradditionaldata)) - ); A } + voidPushchannel_shelltoastnotificationreceived (Objectsender, Notificationeventargs e) the { -StringBuilder message =NewStringBuilder (); $ stringRelativeuri =string. Empty; the theMessage. AppendFormat ("Received Toast {0}:\n", DateTime.Now.ToShortTimeString ()); the the //show the transmission over the message to the name-worthy way - foreach(stringKeyinchE.collection.keys) in { theMessage. AppendFormat ("{0}: {1}\n", Key, E.collection[key]); the About if(string. Compare ( the Key, the "Wp:param", the System.Globalization.CultureInfo.InvariantCulture, +System.Globalization.CompareOptions.IgnoreCase) = =0) - { theRelativeuri =E.collection[key];Bayi } the } the - //Display A dialog of all the fields in the toast. -Dispatcher.begininvoke (() =MessageBox.Show (message. ToString ())); the the}
The above code is a sample code given by Microsoft, with a simple explanation.
Reminder : 1, in order to more convenient use of push notifications, you can write the above code in a class, but also to enhance his function. For example, add a program to control whether you need to notify the Checkbutton button, etc.
2, if you want to jump to the page to extract other information in param, you can use a code like this (where Navigatefrom and other are the names of the name-value pairs added to the param parameter, of course, with the notification source to make corresponding changes):
if (NavigationContext.QueryString.TryGetValue ("navigatefrom" out _ Navigatefrom) { _otherthing= navigationcontext.querystring["other"];}
Notify Source Side
This part of the code is not posted, you can refer to MSDN on the corresponding content.
Look at the format of the message sent:
stringToastmessage ="<?xml version=\ "1.0\" encoding=\ "utf-8\"?>"+"<wp:notification xmlns:wp=\ "wpnotification\" >"+"<wp:Toast>"+"<wp:Text1>"+ TextBoxTitle.Text.ToString () +"</wp:Text1>"+"<wp:Text2>"+ TextBoxSubTitle.Text.ToString () +"</wp:Text2>"+"<wp:param>/page2.xaml? Navigatedfrom=toast notification</wp:param>"+"</wp:Toast>"+"</wp:Notification>";
It contains three attributes of the toast mentioned above: Text1 (title), Text2 (content), Param (parameter).
Note: 1, param can have a. xaml page, which is the page that is about to jump to, followed by a name-value pair (can contain many pairs, but each name-value pair to use "&" to distinguish between, if here param name-value pairs using "&", Code is also required for escape character processing).
2, if you want to push the message in Chinese, note that when encoding the use of UTF8 to encode, as
byte [] Notificationmessage = Encoding.UTF8.GetBytes (toastmessage);
3, also pay attention to the length of the push message also has a certain limit, otherwise it will produce real problems, or push can not be.
Spents: 1h15m (oh, it's too slow to write)
Windows phone Toast Message Push learning Notes