[WP 8.1 Development] use of custom (raw) Notifications

Source: Internet
Author: User

Continue to the previous topic or push notifications. The raw notification Push left in the previous article is not presented to you. It is special to stay here. It is not anything else. It is just interesting to play with this raw notification. The raw notification is translated into "original notification" in the official documentation. The official translation is used here.

Before getting started, let's talk about the key points related to push notifications.

Some people say that if I have 22222222 clients, wouldn't I have to get the channel URL of each mobile client for pushing? Yes. So some people think of the so-called "Aurora push", and the "AURORA" is obviously stolen. We have to make it clear under what circumstances we will consider using push.

Push is like a "private conversation" between the server and the mobile client. That is, when we want to send personalized messages to each customer, it is called Push. To put it bluntly, that is, the messages received by each user are different. For example, QQ is the case, and the chat records of each user are different. If you want to send the same information to all customers, you should not use push, use socket communication that is more simple to process, or simply use the Web/WCF Service to put messages to the service, each client automatically reads data. You can combine background tasks and special background plans to control the frequency of information updates (for example, once a day), get the updated information, and then remind the user through toast or tile.

========================================================== ======================================

Well, the nonsense is over. The following is the text.

Raw notifications are flexible. Unlike toast, tile, and screen lock notifications, raw notifications must strictly abide by the fixed XML format. You can define the raw notification content or structure by yourself, then post the notification content to the push channel URL, it is recommended to use UTF-8 encoding when sending, in this case, Chinese characters will not receive garbled characters in the client, don't say it will definitely turn into gibberish, but being cautious is definitely not a bad thing.

 

On the mobile client, we generally receive raw notifications based on background tasks. The advantages of this solution are:

1. Background tasks can be run independently, in a planned and conditional manner without affecting the front-end ui or system performance. As we all know, WP mobile phones must be smooth and smooth, so it is not good because the applications we develop make the system stop refreshing.

2. With backend reception, even if the application is not running, the notification can be received only if the Network is available.

 

Well, let me think about it. What kind of demonstration will you give? In this case, in order to make the example easier to understand, assume that my server is an e-commerce platform that specializes in selling seven forest men's clothing in shanzhai. Whenever a new Shanzhai is put on shelves, the Service will automatically notify the specified customer of discounts, and we will use raw notifications.

1. Start Vs and create a WP 8.1 application.

2. Add a "Windows runtime component" project to the solution, such.

Note: It is a Windows runtime component, not a class library. Do not create a class library project.

You can name the project by yourself, for example, "backend monster. This windows runtime component project is used to define our background tasks.

 

3. Declare a class, and the class must implement the windows. ApplicationModel. Background. ibackgroundtask interface. This interface contains a run method. We need to implement this method.

Public sealed class implements ibacktask: Windows. ApplicationModel. Background. ibackgroundtask {public void run (Windows. ApplicationModel. Background. ibackgroundtaskinstance taskinstance) {// write background processing code here }}

The following is the background implementation code.

 

Public void run (windows. applicationModel. background. ibackgroundtaskinstance taskinstance) {// write the background processing code here windows. networking. pushnotifications. rawnotification notification = taskinstance. triggerdetails as windows. networking. pushnotifications. rawnotification; If (Notification! = NULL) {// get the raw notification content string message = notification. content; // because the content is | as the separator, we need to obtain the title and content string [] items = message. split ('|'); // save data in windows. storage. applicationdata. current. localsettings. values ["title"] = items [0]; windows. storage. applicationdata. current. localsettings. values ["content"] = items [1]; // updates the tile to Windows. data. XML. dom. xmldocument Doc = windows. UI. communications. tileupdatemanager. gettemplatecontent (windows. UI. communications. tiletemplatetype. tilesquare150x150text02); var nodes = Doc. selectnodes ("Tile/visual/binding/text"); If (nodes. count> = 2) {// modify the XML value (xmlelement) nodes [0]). innertext = items [0]; (xmlelement) nodes [1]). innertext = items [1];} // updates the tile tileupdatemanager. createtileupdaterforapplication (). update (New tilenotification (DOC); // By The Way, send a toast notification to xmldocument doctoast = toastnotificationmanager. gettemplatecontent (toasttemplatetype. toasttext02); var txtnodes = doctoast. getelementsbytagname ("text"); If (txtnodes. count> 1) {// modify the XML value (xmlelement) txtnodes [0]). innertext = items [0]; (xmlelement) txtnodes [1]). innertext = items [1];} // sends toastnotificationmanager a toast notification. createtoastnotifier (). show (New toastnotification (doctoast ));}
}


When the run method is called, an object instance that implements the ibackgroundtaskinstance interface is passed in through parameters, which indicates the instance of the background task being executed. Then, you can access the taskinstance. triggerdetails attribute to obtain an object associated with triggering the current background task.

This background task is triggered by the arrival of raw notifications. Therefore, the object referenced by the triggerdetails attribute is a rawnotification instance, and the raw notification content can be obtained through the content attribute.

During the test, a "|" symbol is used to separate the title and body. Therefore, the application must split the content characters after receiving the notification, to get the title and body.

Finally, save the received content to the local storage, send a toast reminder to the user, and update the tile.

 

4. Return to the WP application project and add a reference to the background task.

5. Open the configuration file, switch to the "Declaration" tab, and add a background task. The trigger is "push notification". The entry point is the class name of the background class just defined, contains the namespace name. For example.

 

6. Modifying the configuration file only allows a background task. To make the background task really run, you also need to register it in the code.

String taskname = "back_notifi"; // background Task Name string entrypoint = "rawicationicationbackgroundtask. notifibacktask "; // entry point // check whether a background task var result = await backgroundexecutionmanager is allowed. requestaccessasync (); If (result = backgroundaccessstatus. allowedmayuseactiverealtimeconnectivity) {// check whether the background task var task = backgroundtaskregistration has been registered. alltasks. values. firstordefault (t) => T. name = taskname); // if not, register if (task = NULL) {backgroundtaskbuilder TB = new backgroundtaskbuilder (); TB. taskentrypoint = entrypoint; TB. name = taskname; // trigger is the trigger for pushing notifications TB. settrigger (New pushnotificationtrigger (); // The running condition is that the Network is available TB. addcondition (New systemcondition (systemconditiontype. internetavailable); // register TB. register ();}}

VaR task = backgroundtaskregistration. alltasks. values. firstordefault (t) => T. name = taskname); one line of code is to check whether the background task has been registered. If it has already been registered, do not register it again. The background task name must be unique, it cannot be the same as other background tasks.

 

Remember: to synchronize the identifiers in the list with the information in the App Store, refer to the previous article.

 

7. Now, run the test server we developed earlier as an administrator. Select "Custom" for the notification type ".

 

 

After the notification is sent, you will receive the corresponding notification on your mobile phone. Please refer to the following figure.

 

 

 

Sample Code download: http://files.cnblogs.com/tcjiaan/RawNotificationWPClientApp.rar

 

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.