In the previous article, we have completed the server side for sending notifications, and then we will use this service to test it.
Before we start testing, we need to do a WP application that receives notifications.
1. Start vs Express for Windows, create a new project, and select "Blank application (Windows Phone)" In the project template.
2, since to receive the notice, it must be toast, tile this kind of common notice, so we have to prepare some pictures.
In Solution Explorer, double-click to open the manifest file, switch to the Visible Assets tab, this "asset" refers to the number of your bank card deposits, but some of your applications in the film, music and other resources, can not be very good translation, according to the word literal translation, anyway, you know what it is OK.
We see that the sizes required for the icons are already noted, noting that each type of icon has three proportions, 100%, 140%, and 240%, respectively. So, for each kind of icon, theoretically we need to prepare three pictures separately , assuming that the image name is ABC, the picture used for 100% scale can be named abc.scale-100.png; for 140% proportions of the picture, Can be named Abc.scale-140.png ... One more scale-xxx,xxx in the middle is the proportional value.
If you're in trouble, you can just prepare a picture for a percentage, like I only have 100% of all the pictures, but we all know that the image will become blurred when zoomed in, but it will not blur when zoomed out. Therefore, we use the most proportional (240%) standard to prepare picture files.
For example, the initial screen, from the window prompts us to see that the size of the 240% scale required for 1152x1920, we design a picture of this size, Named Xxx.scale-240.png, preferably with a PNG image, because it allows the background to be transparent, usually we should consider using a transparent background, the background color of the first screen can be set separately. As shown, in order to support the cause of environmental protection, I changed the background color of the splash screen to green.
This example is used to test, do not have to design too seriously, open PS, scribble a few pictures on the line. Why use PS? Some people say that the system comes with the drawing is not OK? But, you know, the paint program that comes with the system is not transparent when it draws a PNG image, but PS can choose a transparent background when creating new content, as shown in.
Some people will ask, PS is what? PS is the abbreviation for Photoshop.
3, we can simply use the lock screen, switch to the "Application" tab, and then find the "Notification" section, turn on Toast notification, and change the lock screen notification to badge and tile text.
4, turn on the lock screen reminder, you must set background tasks, this we will later pull, this shows that we do not need background tasks, but also can not be empty there, switch to the "Declaration" tab, add a background task, the type of support to tick "push notifications"; because we didn't develop background tasks, So fill in the entry point with the name of the app class in the current app, including the name of the namespace.
Save and close the manifest file.
5. Open the Code view of the MainPage page and add the following code to the Onnavigatedto method.
protected Async Override voidonnavigatedto (NavigationEventArgs e) {Pushnotificationchannel channel=awaitPushnotificationchannelmanager.createpushnotificationchannelforapplicationasync (); //If there is no key in the local settings, it is the first time that the//the URL needs to be stored and sent to the server//if (Windows.Storage.ApplicationData.Current.LocalSettings.Values.ContainsKey ("url") ==false)//{ //windows.storage.applicationdata.current.localsettings.values["url"] = Channel. Uri; //Sendurl (channel. Uri); //} //Else//{ //string savedurl = windows.storage.applicationdata.current.localsettings.values["url"] as String; // //when the URL changes, it is sent back to the server//if (savedurl! = Channel. Uri)// { // //save Local Settings again//windows.storage.applicationdata.current.localsettings.values["url"] = Channel. Uri; //Sendurl (channel. Uri); // } //}System.Diagnostics.Debug.WriteLine (channel. Uri); Sendurl (channel. Uri); }
Call the Createpushnotificationchannelforapplicationasync method to create a push channel, and then send the URL of the channel to our own written server, the server is based on this to send push to the phone.
Note that the code is commented out, the function is to save the obtained channel URL to the local settings, if the new URL obtained and the local settings in the same URL, the URL has not changed, you do not have to send the URL to the server.
The function of the Sendurl method is to send the channel URL to the server with the following code:
Private Async voidSendurl (stringURL) { using(HttpClient client =NewHttpClient ()) { byte[] data =System.Text.Encoding.UTF8.GetBytes (URL); Bytearraycontent content=Newbytearraycontent (data); Try { awaitClient. Postasync ("http://192.168.1.100:85/svr/", content); } Catch { } } }
Well, the most important thing we have left is to set up the application's manifest file. Remember the Sid,app ID that we got when we created the app in the previous article?
On the server side, use this information to apply for access token, in the WP application we also need to write this information to the manifest file, so that push the server to push, if the identity does not match, will not push, to prevent some ulterior motives of people, everywhere push spam love letter, Cause information pollution.
Do we open the manifest file and change it one by one? It doesn't matter if you have the intention. However, we are 21st century tall, do not need mechanical labor, below I show you how to intelligently synchronize the store app information to the manifest file (if your app has been uploaded to the store, you do not need to do so, but in the testing or learning phase, do not upload).
In Solution Explorer, right-click the project name, choose Store from the shortcut menu, associate the app with the store, as
Alternatively, in the VS menu bar, execute the store > Associate app with Store menu.
A wizard will appear, click Next.
You will need to enter the developer account to verify.
After validation succeeds, you are connecting to the store.
Then, in the app list, select the app you want to sync, and here I'm going to choose the sample app that I created last time.
And then the next step, until it's done. The application manifest file is automatically modified.
================================================================
Here we can test, first remember to run as an administrator of the server we wrote earlier. Enter the SID and client key to get access token.
Run the WP Mobile client app and get to the channel URL and send it to the server.
Test Toast Notifications
For WP, toast only ToastText02 This template is available, even if you use other templates, it still enforces the use of ToastText02 templates.
Modify the XML template, text with ID 1 to set the toast title, the caption will appear in bold, the text element with ID 2 as the content, and display as normal font.
After clicking Send, on the phone you will see the miracle happen.
If the X-wns-suppresspopup header is used and the TRUE,TOAST notification is not popped, it is thrown directly into the Action Center queue.
Test the tile
To see the tile notifications, first pin the app to the start screen.
Then select a template, modify the XML content, and then send.
At this point, you will see that the tile on your phone's start screen has been updated.
You can also try wide tiles.
Test lock Screen Notifications
First to the system settings, add the application to the lock screen sequence.
Then put the phone lock screen, you can test it.
Please look at the bottom of the screen.
SOURCE Download: Http://files.cnblogs.com/tcjiaan/NotificationWPClApp.rar
For raw notifications, it can be combined with a background task that is received in the background by a background task. About this, our next article is a piece of crap.