Windows 8 Store Apps學習(67) 背景工作: 推播通知

來源:互聯網
上載者:User

介紹

重新想象 Windows 8 Store Apps 之 背景工作

推播通知

樣本

1、用戶端

BackgroundTask/PushNotification.xaml

<Page    x: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// 查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/            // 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: // 快顯通知                    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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.