Windows 8 Store Apps学习(67) 后台任务: 推送通知2014-09-02 cnblogs webabcd介绍重新想象 Windows 8 Store Apps 之 后台任务推送通知示例1、客户端BackgroundTask/PushNotification.xaml
<Pagex:Class="XamlDemo.BackgroundTask.PushNotification"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="using:XamlDemo.BackgroundTask"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d"><Grid Background="Transparent"><StackPanel Margin="120 0 0 0"><TextBlock Name="lblMsg" FontSize="14.667" TextWrapping="Wrap"/><Button Name="btnCreateChannel" Content="create the channel" Margin="0 10 0 0" Click="btnCreateChannel_Click"/><TextBox Name="txtUri" Margin="0 10 10 0"/><Image Source="wns.png" Margin="0 10 0 0" HorizontalAlignment="Left" Width="800"/></StackPanel></Grid></Page>
BackgroundTask/PushNotification.xaml.cs
/* * 演示如何接收推送通知 ** 注: * 需要在 Package.appxmanifest 中增加后台任务声明,并勾选“推送通知” * 在 win8 商店创建了应用后,需要将此 app 的商店中的 identity 复制到 Package.appxmanifest 的 identity 节点 * 不能在模拟器中运行 * 每次新建的 channel 有效期为 30 天 ** 另: * WNS - Windows Push Notification Service * 推送通知的服务端参见:WebServer/PushNotification 内的文件 */using System;using Windows.ApplicationModel.Background;using Windows.Networking.PushNotifications;using Windows.UI.Notifications;using Windows.UI.Popups;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;namespace XamlDemo.BackgroundTask{public sealed partial class PushNotification : Page{public PushNotification(){this.InitializeComponent();}private async void btnCreateChannel_Click(object sender, RoutedEventArgs e){// 当收到推送的 raw 通知时,如果 app 在锁屏,则可以触发后台任务以执行相关的逻辑(PushNotificationTrigger)BackgroundAccessStatus status = BackgroundExecutionManager.GetAccessStatus();if (status == BackgroundAccessStatus.Unspecified){status = await BackgroundExecutionManager.RequestAccessAsync();}if (status == BackgroundAccessStatus.Denied){await new MessageDialog("请先将此 app 添加到锁屏").ShowAsync();return;}// 创建一个推送通知信道,每个新建的 channel 有效期为 30 天,所以建议每次进入 app 后都重新建一个 channel(但是需要注意间隔较短的话,则会复用之前的 channel)PushNotificationChannel channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();// 接收到通知后所触发的事件channel.PushNotificationReceived += channel_PushNotificationReceived;// channel.Close(); // 关闭 channel// // channel.ExpirationTime; // channel 的过期时间,此时间过后 channel 则失效// channel 的 uri 地址,服务端通过此 uri 向此 app 推送通知txtUri.Text = channel.Uri.ToString();}void channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args){switch (args.NotificationType){case PushNotificationType.Badge: // badge 通知BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(args.BadgeNotification);break;case PushNotificationType.Raw: // raw 通知// 当收到推送的 raw 通知时,如果 app 在锁屏,则可以触发后台任务以执行相关的逻辑(PushNotificationTrigger)string msg = args.RawNotification.Content;break;case PushNotificationType.Tile: // tile 通知TileUpdateManager.CreateTileUpdaterForApplication().Update(args.TileNotification);break;case PushNotificationType.Toast: // toast 通知ToastNotificationManager.CreateToastNotifier().Show(args.ToastNotification);break;default:break;}args.Cancel = true;}}}
Package.appxmanifest
<?xml version="1.0" encoding="utf-8"?><Package xmlns="http://schemas.microsoft.com/appx/2010/manifest"><!--以下借用“贪吃的蛇”的 Identity,以便演示推送通知--><!--Identity Name="10437webabcd.173815756DD78" Publisher="CN=27514DEC-C708-4EDB-BFEA-F956384483D0" Version="1.0.0.0" /--><Identity Name="webabcd.win8.XamlDemo" Publisher="CN=wanglei" Version="1.0.0.0"/></Package>
2、服务端WebServer/PushNotification/OAuthToken.cs