It seems that I haven't updated it for several days. Sorry, I'm sorry, I 've recently lost my job in a glorious place. I'm busy looking for a new place. Then there are some things to do, but it doesn't matter. Let's continue today.
The content of the animation has come to an end. This series of articles only provide simple guidance and will not cover every detail. If you can learn it well, you will be able to fully understand yourself.
Starting from this section, we will discuss the principles and processes of pushing notifications, which are not very well understood. If you look at msdn, I believe you will be a little dizzy. If you don't, I will post it to you.
Forget it. I don't know what's going on. I can't upload images.
Current patch.
Let me tell you a story, hoping to help you understand what push notifications are.
When I went to college, I liked to borrow books from the library. Then I watched them in my dormitory at night and went to bed when I saw tired. One time, I found a good book called "Chinese-style business path". As a result, I did not find it in the library, but I checked it. I read this book and asked the Administrator, the Administrator said it may have been lent by someone else.
At this time, I thought to myself: check whether there are any shelves and borrow them every day.
The Administrator seemed to have guessed my mind. He said, "This student, you can leave your borrow card number and contact information. If you really want to read this book, once someone returns the book, I will inform you immediately that you do not have to come and find it every day."
I hurriedly said thank you.
For example, I have enabled app A, and user B's mobile phone is using my app, but sometimes I will send some notifications to user B's mobile phone, such as adding new features or fixing some bugs, or invite users to participate in public welfare activities. However, how does the application on user B know that there is a new message?
According to the traditional practice, make a regular "bomb" in the application program, access my server through the network at intervals, and check whether there are any new messages, then return the result to the client application. What are the disadvantages of doing so?
Frequent access to the network, increasing network traffic, will also consume a certain amount of power and resources, if I use GPRS to access the Internet, it will be unlucky.
However, if my client never needs to actively access the network, I do not have to place a timer in the application, and the program does not need to access the network. My new message is not sent to the user's mobile phone, instead, it is sent to Microsoft's ECs and then the ECS pushes messages to users' mobile phones. This is like the previous example. I don't have to go to the library every day to find a book. As long as there is a book, the library administrator will call me. Do you think this is both worry-free and effort-saving?
There are three types of push notifications: Toast notifications, magnetic post notifications, and custom notifications. The first two are both dead and hard-coded. Don't ask why, just remember. The third method is raw notification, which is more flexible. You can customize the format and content of the notification.
Today, let's take a look at toast, the first notification.
What is this?
I wanted to cut a picture, but I didn't know what to do. I just couldn't upload it and didn't respond. csdn blogs often have problems. There is no way to do it. Let me describe it in text. The toast notification means that when the application is not running in the foreground, if you receive the toast notification, a prompt message will be displayed at the top of the screen, it is the same as when we receive the text message.
Microsoft's ECs will allocate a URL for our mobile phone, which will be updated after registration of the push channel in the ghost application, ECS uses this URL to find your mobile phone and send the notification to your mobile phone. Just like in the previous example, I have left the credential number and phone number. At that time, the administrator can contact me by phone number. Actually, you can upload the URL to your server in various ways to save it, because the URL is required for sending push notifications.
Generally, if you have your own server, you should have a fixed IP address or domain name. You may wish to send the user's mobile phone URL to your server through HTTP for storage.
So how do I send push notifications? Don't be intimidated. In fact, it is very simple. It is usually the POST method that we are familiar with to submit an HTTP request, and the submitted URL is the URL obtained from the ECS. The post content is an XML document. The format of the toast push notification is as follows:
<!--?</span--> XML version = "1.0" encoding = "utf-8"? >
< wp: Notification XMLNS: wp = "WPNotification" >
< wp: Toast >
<wp:text1> Text1 </wp:text1>
<wp:text2> Text2 </wp:text2>
< < wp: Param > parameters/wp: Param >
< / wp: Toast >
< / wp: Notification >
It's a fixed format, don't ask me why, it's dead. "Text 1" refers to the title that shows the Toast prompt. "text 2" is the body of the Toast.
What about parameters? The other is a URI, which is the page that the user navigates to when he or she starts the application after clicking on the Toast message, which is the same as the "secondary tiles" mentioned earlier. Let me give you some examples.
/ MainPage. Xaml
/ MainPage. Xa/Mml? V = 12345
/ MainPage. Xaml? Value1 = 123 & amp; Value2 = the abcd
The last one is actually value1=123&value2= ABC, don't forget it's an XML document, the character & is escaped, remember someone asked you earlier, in the navigation section, set the navigation page/mypage.xaml in XAML, right? T1 =aaaa&t2= BBBB, and you get an error, and you know that XAML is actually an extension of XML, special characters to escape.
For example, if I want to send a Toast notification with the title "hello", the content is "would like to invite you to dinner", and the parameter is "/ mainpage.xmal", then the XML document of our POST should be:
<!--?</span--> The XML version = "1.0" encoding = "utf-8"? >
< wp: Notification XMLNS: wp = "WPNotification" >
< wp: Toast >
< wp: Text1 > hello < / wp: Text1 >
<wp:text2> I'd like to invite you to dinner </wp:text2>
< < wp: Param > / MainPage xaml/wp: Param >
< / wp: Toast >
< / wp: Notification >
Knowing this is easy, let's make a server that sends the Toast message.
1, whichever version of VS you like to use, create a new Windows application, familiar with it, is WinForm.
2, then the interface, dizzy, can not upload the picture. Well, you throw a couple of textboxes at random and use them to fill RUI, the first value, the second value, the parameter, the response message. There are 5 in total, and the last one is used to display the sending result. It has a lot of content and is recommended to use multiple lines. Put another button, trigger its Click event, and send it immediately.
Ok, so I'm just going to post all the code, it's hard to explain, but I'm sure you'll be able to understand it if you learn the basics.
Using System;
Using System. Collections. Generic;
Using System.Com ponentModel;
Using System. The Data;
Using System. Drawing;
Using System. Linq;
Using System. The Text;
Using System. Windows. Forms;
Using System.Net;
Using System. IO;
The namespace SendToast
{
Public partial class Form1: Form
{
Public _click ()
{
InitializeComponent ();
}
Private void btnSend_Click(object sender, EventArgs e)
{
HttpWebRequest myRequest = (HttpWebRequest) WebRequest. Create (txtUrl. Text);
MyRequest. ContentType = "text/XML";
MyRequest. Headers. The Add (" X-ray theculmination - Target ", "toast");
/ *
* X-NotificationClass processing interval
* 2 - send immediately
* 12-450 seconds to send
* 22-900 seconds to send
* /
MyRequest. Headers. The Add (" X-ray NotificationClass ", "2");
// the content to be sent
String toastMessage = "<!--?</span--> The 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);
MyRequest. ContentLength = buffer. Length;
MyRequest. Method = "POST";
Using (Stream Stream = myRequest. GetRequestStream ())
{
Stream. Write (buffer, 0, buffer Length);
}
// receive responses
HttpWebResponse myResponse = (HttpWebResponse) myRequest. The method GetResponse ();
String headers = "";
Foreach (var hd in myResponse. Headers. AllKeys)
{
Headers += hd + ":" + myresponse. headers [hd] + "| ";
}
Headers + = "\ r \ n";
String MSG = "";
Using (Stream recStream = myResponse. GetResponseStream ())
{
StreamReader = new StreamReader(recStream, Encoding.UTF8);
MSG = reader. ReadToEnd ();
Reader. The Close ();
}
MSG + = "\ r \ n \ r \ n";
TxtResult. AppendText (headers + MSG);
}
}
}
Next, go to the WP client and create a new Silverlight for Windows Phone application with any version of VS you want. Some people are sensitive and don't know what's going on when they see the word Silverlight. In fact, it is just that not many people understand it, Silverlight actually has many advantages, slowly experience it, with an objective and impartial perspective to experience it.
Interface layout is easy to do, I directly go to XAML, if you don't understand, go back to review WPF.
"Grid x: Name =" ContentPanel "Grid. Row =" 1 "Margin =" 12,0,12,0 ">
The < TextBlock Name = "txtInfo TextWrapping =" Wrap "/ >"
< / Grid >
The code behind the post.
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System.Net;
Using System. Windows;
Using System. Windows. Controls;
Using System. Windows. The 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;
The namespace WPApp
{
Public partial class MainPage: PhoneApplicationPage
{
// constructor
Public MainPage ()
{
HttpNotificationChannel myChannel = null;
// push the name of the channel, just pick any one
String ChannelName = "ToastChannel";
InitializeComponent ();
// Find static methods look for channels by name
MyChannel = HttpNotificationChannel. Find (ChannelName);
// if not, create one
If (myChannel = = null)
{
MyChannel = new HttpNotificationChannel (ChannelName);
// register events
MyChannel. ChannelUriUpdated + = new EventHandler < NotificationChannelUriEventArgs > (myChannel_ChannelUriUpdated);
MyChannel. ErrorOccurred + = new EventHandler < NotificationChannelErrorEventArgs > (myChannel_ErrorOccurred);
MyChannel. ShellToastNotificationReceived + = new EventHandler < NotificationEventArgs > (myChannel_ShellToastNotificationReceived);
// open the channel
MyChannel. The Open ();
// bind Toast notifications so that they are displayed when the application is not in the foreground
// the notification bar at the top of the screen
MyChannel. BindToShellToast ();
}
The else
{
// if there is, register an event as well, since event bindings may be removed after the application is thrown into the background
MyChannel. ChannelUriUpdated + = new EventHandler < NotificationChannelUriEventArgs > (myChannel_ChannelUriUpdated);
MyChannel. ErrorOccurred + = new EventHandler < NotificationChannelErrorEventArgs > (myChannel_ErrorOccurred);
MyChannel. ShellToastNotificationReceived + = new EventHandler < NotificationEventArgs > (myChannel_ShellToastNotificationReceived);
// print the URL in the "output" window, because we are just testing, which makes it easier
System. Diagnostics. Debug. WriteLine (" channel URI is: {0} ", myChannel. ChannelUri. The ToString ());
}
}
Void myChannel_ShellToastNotificationReceived (object sender, NotificationEventArgs e)
{
String MSG = "";
Foreach (string key in e.cocop.keys)
{
MSG += key + ":" + e.ollection [key] + "\r\n";
}
The Dispatcher. The BeginInvoke (() = >
{
Enclosing txtInfo. Text = MSG;
});
}
Void myChannel_ErrorOccurred (object sender, NotificationChannelErrorEventArgs e)
{
The Dispatcher. The BeginInvoke (() = > MessageBox. Show (e.M essage));
}
Void myChannel_ChannelUriUpdated (object sender, NotificationChannelUriEventArgs e)
{
// when the URL changes, output again
// make sure we get the latest version of the URI
The Dispatcher. The BeginInvoke (() = >
{
System. Diagnostics. Debug. WriteLine (" channel URI: {0} ", e.c. with our fabrication: hannelUri. The ToString ());
});
}
// I don't need to tell you more about this method
Protected override void OnNavigatedTo (System. Windows. Navigation. NavigationEventArgs e)
{
Base. OnNavigatedTo (e);
If (NavigationContext. The QueryString. Either ContainsKey (" toastmsg "))
{
Enclosing txtInfo. Text = NavigationContext. The QueryString (" toastmsg ");
}
}
}
}
Ok, so, how do you test it? There is no doubt that both programs should be run at the same time. Copy RUI from the output window of VS into the text box corresponding to the sending program, fill in several parameters, such as the body of the title, etc. Then, you go back to the WP simulator and click the "start" button to keep the application out of the foreground.
Go back to the server side, click the send button, wait a minute, you will see the Toast prompt in the simulator.
There's no way to upload pictures. That's it.
Now, to sum up, push notifications are not hard, they are essentially HTTP communication, and two of the three are fixed formats. Open the MSDN example, copy it, same thing.
But to understand it is not so easy, remember to practice more, learning programming is no shortcut, the fastest shortcut is to do. You may ask: how are you familiar with these technologies?
Let me tell you something. I've written these push notification codes a dozen or 20 times. Do you think I can't understand them? If you don't believe me, write it ten times.