http://blog.csdn.net/nacl025/article/details/8998552
http://blog.csdn.net/nacl025/article/details/9037247
Open a push channel (if it does not already exist), or connect to an existing push channel. If your app created a push channel in a previous app instance, there might be a push channel. If the push channel is bound to a tile or Toast notification, the push channel still exists after the app exits, so that you can still receive these notifications even if the app is not running.
Apps that use raw notifications must implement the Httpnotificationreceived event to receive raw data in the notification. (Raw notifications cannot be received if the app is in a non-running state)
If your app is already running, the TOAST notification is ignored unless you register for the shelltoastnotificationreceived event. Your app can then determine how it wants to respond to Toast notifications.
Original notification
1. The original notification is the only notification type that can trigger a background task. Although toast, tile, and badge push notifications do not trigger background tasks, background tasks triggered by the original notification can update the tile and invoke Toast notifications through local API calls.
2. For each application, only one background task can be run at a time. If you trigger a background task for an app that is already running a background task, you must complete the first background task before you can run the new background task.
3. If the app is running, the notification delivery event takes precedence over the background task, and the app will have the first opportunity to process the notification. The notification delivery event handler can be set to true by setting the Pushnotificationreceivedeventargs.cancel property of the event To specify that the handler should not pass the original notification to its background task after exiting. If the Cancel property is set to false or not set (the default value is false), the original notification triggers the background task after the notification delivery event handler finishes its work.
Consider using a different communication method before selecting the background task that was triggered by the original notification. Most apps should not need to implement background tasks. To make your app use background tasks, the app must be one of the few apps that allows pinning to the lock screen. Your app will contend with these slots, and the user has ultimate control over the apps that occupy the slots. There is no guarantee that your application is one of them. By using other mechanisms that implement communication in your app, such as standard push notifications or Toast updates, you can prevent users from having to choose between your app and other apps they care about.
Some workarounds for background tasks: (Note that toast\title notifications can be run in the background, but cannot touch the background task)
- To attract users ' attention, send a Toast push notification.
- To update your tile, use a tile push notification.
4. Background task triggered by the original notification: the user must explicitly give the app permission to run background tasks, which is granted when the user adds the app to its lock screen. Only seven apps can have this right at a time. 5. When your app's cloud service sends notifications to Windows, your app has the opportunity to intercept and process the notification before displaying a Toast, updating a tile or badge, or sending the original notification to a background task. It can also block the display or update of these elements. Implementing a notification Send event handler is optional. When the app wants to process and block incoming (E. Cancel = True, the option is most useful when the Toast is not displayed to the user.
[CSharp]View Plaincopy
- Pushnotificationchannel channel = null;
- Channel. Pushnotificationreceived + = onpushnotification;
- Try
- {
- Channel = await pushnotificationchannelmanager.createpushnotificationchannelforapplicationasync ();
- }
- catch (Exception ex)
- {
- // ...
- }
- string content = null;
- Private async void Onpushnotification (pushnotificationchannel sender, Pushnotificationreceivedeventargs e)
- {
- String notificationcontent = String.Empty;
- switch (e.notificationtype)
- {
- Case Pushnotificationtype.badge:
- Notificationcontent = E.badgenotification.content.getxml ();
- Break ;
- Case Pushnotificationtype.tile:
- Notificationcontent = E.tilenotification.content.getxml ();
- Break ;
- Case Pushnotificationtype.toast:
- Notificationcontent = E.toastnotification.content.getxml ();
- Break ;
- Case Pushnotificationtype.raw:
- Notificationcontent = e.rawnotification.content;
- Break ;
- }
- E.cancel = true;
- }
WIN8 Push Notification Small note