Push Notification(推播通知)之推送 Tile 通知, 推送自訂資訊
介紹
與眾不同 windows phone 7.5 (sdk 7.1) 之推播通知
推送 Tile 通知
推送自訂資訊
樣本
1、推送 Tile 通知
用戶端
PushTile.xaml
<phone:PhoneApplicationPage x:Class="Demo.PushNotification.PushTile" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480" shell:SystemTray.IsVisible="True"> <StackPanel Orientation="Vertical"> <Button Name="btnRegister" Content="Get Channel Uri" Click="btnRegister_Click" /> <TextBox Name="txtUrl" /> <TextBlock Name="lblMsg" TextWrapping="Wrap" /> </StackPanel> </phone:PhoneApplicationPage>
PushTile.xaml.cs
/* * 示範推送 Tile 通知 * * HttpNotificationChannel - 推播通知 channel * HttpNotificationChannel.Find(string channelName) - 尋找並返回 channel,一個 app 只能有一個 channel * * ChannelUri - 推播通知的 channel URI * ChannelName - channel 的名稱 * ConnectionStatus - channel 的串連狀態,ChannelConnectionStatus.Connected 或 ChannelConnectionStatus.Disconnected * * ChannelUriUpdated - 擷取到了 channel URI 後所觸發的事件 * ErrorOccurred - 發生異常時所觸發的事件 * ConnectionStatusChanged - 串連狀態發生改變時所觸發的事件 * ShellToastNotificationReceived - 程式運行中如果收到 Toast 是不會顯示的,但是會觸發此事件。 * 什麼叫運行中:程式在前台顯示,且調用了 HttpNotificationChannel.Find(channelName)(沒有則建立一個),同時 channel 是在串連的狀態 * HttpNotificationReceived - 接收到自訂語音總機後所觸發的事件(僅在程式運行中才能接收到此資訊) * * Open() - 開啟 channel * Close() - 關閉 channel * * BindToShellToast() - 將此 channel 綁定到 Toast 通知 * BindToShellTile() - 將此 channel 綁定到 Tile 通知,只能引用本地資源 * BindToShellTile(Collection<Uri> baseUri) - 將此 channel 綁定到 Tile 通知,允許引用遠端資源(當使用遠程映像時,不能是https,要小於80KB,必須30秒內下載完) * baseUri - 允許引用的遠端資源的網域名稱集合(最大 256 個字元) * IsShellToastBound - 此 channel 是否綁定到了 Toast 通知 * IsShellTileBound - 此 channel 是否綁定到了 Tile 通知 */ using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; using Microsoft.Phone.Notification; using System.Text; using System.Diagnostics; using System.Collections.ObjectModel; namespace Demo.PushNotification { public partial class PushTile : PhoneApplicationPage { public PushTile() { InitializeComponent(); } private void btnRegister_Click(object sender, RoutedEventArgs e) { // 在當前應用程式中尋找指定的 channel string channelName = "myChannel"; HttpNotificationChannel channel = HttpNotificationChannel.Find(channelName); // 允許引用的遠端資源的網域名稱集合 Collection<Uri> allowDomains = new Collection<Uri>(); allowDomains.Add(new Uri("http://www.cnblogs.com/")); allowDomains.Add(new Uri("http://images.cnblogs.com/")); if (channel == null) // 未發現則建立一個 channel { channel = new HttpNotificationChannel(channelName); channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(channel_ChannelUriUpdated); channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(channel_ErrorOccurred); channel.Open(); channel.BindToShellTile(allowDomains); } else // 已存在則使用這個已存在的 channel { channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(channel_ChannelUriUpdated); channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(channel_ErrorOccurred); if (channel.ConnectionStatus == ChannelConnectionStatus.Disconnected) channel.Open(); if (!channel.IsShellTileBound) channel.BindToShellTile(allowDomains); // 擷取通知 uri txtUrl.Text = channel.ChannelUri.ToString(); Debug.WriteLine(channel.ChannelUri.ToString()); } } void channel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e) { Dispatcher.BeginInvoke(() => { // 擷取通知 uri txtUrl.Text = e.ChannelUri.ToString(); Debug.WriteLine(e.ChannelUri.ToString()); }); } void channel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e) { Dispatcher.BeginInvoke(() => MessageBox.Show(e.Message)); } } }